banner

For a full list of BASHing data blog posts see the index page.  RSS


Put an editable command at the next prompt

I audit data files that vary a lot in structure, and I often have to modify a command to suit a particular file's complement of fields. The "base" command to be modified can be fairly long, there can be several parameters to be adjusted within it, and there's more than one kind of "base" command.

Typing and modifying those long commands gets pretty tedious. For this reason I built a pick-list of "base" commands called "sniplist" that lives in my ~/scripts directory. I call up the list, pick the "base" command I want and edit it at the next prompt. There are two very different ways to do this job.


Uncool, but it works. The simplest solution is to print "sniplist" to screen with the alias "snips", highlight the command you want with the mouse and middle-click-paste it at the next prompt, ready for editing:

sniplist:
 
(1) To deplexify any spindingles:
long_and_complicated_command_1
(2) To florm and caponitate the allopheme:
long_and_complicated_command_2
 
alias snips='cat ~/scripts/sniplist'

snip1

UnaBASHedly cool. A keyboard-only solution (the one I actually use) is based on the scripts "snips":

#!/bin/bash
echo
cat ~/scripts/sniplist
echo
read -p "Select a command number: "
echo
foo=$(awk -v line="$REPLY" 'NR==(line*2)' < ~/scripts/sniplist)
stty -echo &&
xdotool type "$foo" &&
stty echo
exit

The script prints "sniplist" to the screen and asks me to pick a command number. AWK then prints the selected command (whose line number in "sniplist" will be twice the command number) into the shell variable "foo". To print "foo" at the next prompt I use xdotool type, and to avoid printing the "base" command before the script exits, I bracket xdotool type with stty commands that first turn off echo, then reinstate it.

snip2

Last update: 2021-09-08
The blog posts on this website are licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License