Rigel Group

They shoot Yaks, don't they?

Frequently Used Commands

If you spend a lot of time at the *nix command line, you probably use your shell history quite a bit to avoid typing commands in over and over. Or maybe you define shell aliases for your most precious incantations. Shell history is great, and I have mine set to save a bajillion entries, ‘cause we have more than 640k these days. But even with a nearly infinite shell history, I find myself wanting to know what are the few important commands I usually need in the specific directory I happen to be in.

Frequently Used Commands (fuc)

So, this little shell script will let you save off the important commands to a .fuc file in the local directory, and then easily recall those commands in a little menu.

Let’s say you are working away, and you want to remember a particular command:

1
$ fuc gulp --require coffee-script watch

Which will save the command to the .fuc file, and also run it. Later, you can just type fuc from the same directory, and get this:

1
2
3
4
5
6
$ fuc
fuc - Frequently Used Commands
1) gulp --require coffee-script watch
2) echo "(╯°□°)╯ ┻━┻"
3) cat log/server.log | grep ERROR
Select command to run (q to Quit):

What I really wanted to do was put the selected command on the command line, ready to be edited, then hit enter to run it. But I couldn’t be bothered to figure out how to do that. So selecting a command just runs it immediately.

(I know there are a lot of much more complicated implementations that tweak your actual command history to be directory-specific, but I wanted to be able to specify which commands were the important ones, and just remember those.)

Here is the script. Put this somewhere in your path, and name it fuc, or if that’s too NSFW than I’m sure you can come up with a better name. Maybe dangit or something.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/bin/bash

# Tell cat that we want to break on newlines not spaces
IFS=$'\r\n'

PS3="Select command to run (q to Quit):"

if [ $# -eq 0 ]
then
  echo "fuc - Frequently Used Commands"
  cmdList=$(cat .fuc | sort -u)
  select cmdName in $cmdList; do
    if [ -n $cmdName ]; then
      echo $cmdName
      eval ${cmdName}
      break
    fi
  done
  exit 1
fi

echo Saving and executing command: $*
echo $* >> .fuc
eval $*