The Linux Rain Linux General/Gaming News, Reviews and Tutorials

How to insert code snippets on the command line without executing them

By Bob Mesibov, published 12/07/2015 in Tutorials


You can store a code snippet in a shell alias or a function, but when you type the alias or function name in a BASH shell and press 'Enter', the shell executes (or tries to execute) the snippet. That's no good if the snippet is incomplete or needs editing. A simple workaround is to paste the snippet at the next shell prompt, as explained below.

An example

I often work with tab-separated text tables using GNU AWK, and many of my AWK commands begin like this:

awk 'BEGIN {FS=OFS="\t"}

I've stored that code snippet in a shell function called 'tawk' in my .bashrc file:

tawk()
{
echo -en "awk 'BEGIN {FS=OFS=\"\\\t\"} " | xclip -selection clipboard
}

The 'tawk' function first echoes the snippet, using the '-e' option and backslash escapes to allow the double quotes and literal backslash to be printed. The echo command is also fed the '-n' option to suppress a trailing newline. The snippet is then loaded into the X clipboard with the xclip utility (available in all repositories).

Having entered tawk in a terminal, I can then paste my snippet at the next shell prompt. The snippet appears but isn't executed, and I can finish the command from the snippet, or edit it:

The snippet remains on the clipboard so I can paste it again later, for example after typing in another command, as part of a command chain:

Tweak and check

I use a keyboard shortcut to paste the snippet from the clipboard. My terminal is gnome-terminal and its default paste shortcut is Ctrl+Shift+V. That shortcut can be customised in the Preferences dialog for gnome-terminal.

If you prefer using the X primary clipboard and pasting with a middle-click, you can delete the '-selection clipboard' part of the 'tawk' function.

It's worth checking to make sure that your function name isn't already a command recognised by the shell. If it is, the which command will tell you its path. If it's a new command name that the shell hasn't seen before, which won't find it:

Lots of snippets

If you have a large-ish number of code snippets that you might want to paste, you can list them in a 'snippets' file and use a 'choose a snippet' dialog to pick the one you want loaded into the clipboard. Below is a shell function named 'snip' which sends a 'snippets' file in my 'scripts' directory to a YAD list dialog. The '2 > /dev/null' in the YAD command removes an annoying (and harmless) error message I get with YAD, namely Gtk-Message: GtkDialog mapped without a transient parent. This is discouraged.

Although the YAD dialog is a GUI and can be mouse-operated, you can make a snippet selection by using the up/down arrow keys on your keyboard and pressing 'Enter'. To cancel and close the dialog, hit the 'Esc' key.

snip()
{
foo=$(yad --center --width=500 --height=200 --list --separator="" --column="Snippet" 2>/dev/null < ~/scripts/snippets)
echo "$foo" | tr -d '\n' | xclip -selection clipboard
}

And after starting my command chain and doing Ctrl+Shift+V, I get:



About the Author

Bob Mesibov is Tasmanian, retired and a keen Linux tinkerer.

Tags: tutorials scripting bash awk xclip paste file clipboard code snippets
blog comments powered by Disqus