code snippets

# Linux snippets: using xclip to pipe to the system clipboard

A lot of times I write scripts to generate code, specifically in the case where I have to generate a large amount of SQL column names. If I want to then paste this into a file in the appropriate place, I can either copy and paste from the terminal (which is cumbersome, especially on Linux) or pipe it to a file, and then copy and paste it (which is also a bit unwieldy).

Instead, we can save a step by piping directly to the system (X) clipboard using xclip.  To get it on Ubuntu, we can install it from the repositories:

sudo apt-get install xclip


The default behavior of xclip is not to put its input onto the system clipboard (it puts text in the X clipboard, so you'll be able to middle click to paste in X applications, but not your IDE), so I created an alias in my .bashrc (or .zshrc) file:

alias xclip='xclip -selection c'


Then, you can pipe to the system clipboard with:

cat long_file.txt | xclip

Now you can paste the output of cat long_file.txt with the system paste command into any other application.

# Search Bash history by first few characters (like MATLAB)

One convenient feature of the MATLAB interpreter is the ability to type in the first few characters of a previous command and press the "up" arrow to search for all previous commands that begin with those characters. Bash doesn't enable this behavior by default - you can use ctrl+r to search anywhere in a command, but not by the first few characters. If you want to add this functionality to Bash, add the following to your ~/.inputrc file (from this helpful askbuntu.com post):

## Search backwards with the up arrow
"\e[A":history-search-backward
## Search forwards with the down arrow
"\e[B":history-search-forward


Now just type the first few characters of a command in your history, and press the up arrow to search backward or the down arrow to search forward.

AsyncTask is a relatively pain-free way to thread a background task in an Android application. However, you may want to discourage the user from performing any interaction with the application while the task is running. Or you may want to simply let them know that something is happening in the background, e.g. their file is actually downloading and pressing the button again won't make it happen any faster!

In any case, here is short example of how to show a ProgressDialog while your AsyncTask is running:

private class BackgroundTask extends AsyncTask <Void, Void, Void> {
private ProgressDialog dialog;

public BackgroundTask(MyMainActivity activity) {
dialog = new ProgressDialog(activity);
}

@Override
protected void onPreExecute() {
dialog.setMessage("Doing something, please wait.");
dialog.show();
}

@Override
protected void onPostExecute(Void result) {
if (dialog.isShowing()) {
dialog.dismiss();
}
}

@Override
protected Void doInBackground(Void... params) {
try {
} catch (InterruptedException e) {
e.printStackTrace();
}

return null;
}

}


You should replace MyMainActivity in the ProgressDialog constructor with the name of the calling activity. Note also that this example doesn't actually do anything - it just sleeps for 5 seconds and then finishes. To start the task, you can use the following:

BackgroundTask task = new BackgroundTask(MyMainActivity.this);