A lesson in history.
One of my first experiments with the Raspberry Pi was to establish a serial connection with a Palm Personal Digital Assistant and use its terminal program to access the Pi’s login shell at boot up. The PDA provides a screen to display text and a virtual keyboard or graffiti pad to enter commands. The problem was that it lacks cursor keys, needed to recall the previous command, and I didn’t like having to retype text on its fiddly interface.
Having access to a command history is one of the great features of the shell. You can use the cursor up key to recall the last command, or repeat this until you find the command you want to execute or modify; for most commands this is much quicker than retyping the text. I discovered that shell history can also be navigated using Control key combinations: Ctrl + p will display the previous line. Fortunately, the Palm terminal program features a control icon so I was quickly zipping about my command history. Control codes are useful to learn even if you have cursor navigation on your keyboard. I have learned some new favourites while researching for this blog.
Ctrl + p – display the last line in the history list.
Ctrl + n – display the next line in the history list.
Ctrl + a – go to the beginning of the line
Ctrl + e – go to the end of the line
Ctrl + k – cut from the cursor to the end of the line.
Ctrl + u – cut from the cursor to the beginning of the line.
Ctrl + y – paste content cut with Ctrl+U or Ctrl+K
Ctrl + l – clear the screen and redraw the current line
Ctrl = i – autocomplete. Ctrl + ii – list autocomplete choices.
You may already use the history command to print a numbered list of previous commands and recall a command by using the ! shortcut: !1 for example, would repeat the cd ~ command from the following list.
1 cd ~ #change directory to home
2 ls #list directory
3 cd *1306 #use * wildcard to change directory to Adafruit_Python_SSD1306
4 cd ex*;ls #use * wildcard to change directory to examples; and list contents
5 python3 shapes.py #run shapes example
Use !! to repeat the last line . So sudo !! would give you sudo python3 shapes.py
USe !$ to repeat the last arguments. So less !$ would give you less shapes.py
When you have a large history of commands you might find it useful to pipe the output into another command to filter your results.
history | tail #will give you the last ten entries.
history | head #will give you the first ten entries.
history | grep string #will give you only those entries that contain the provided string.
I find the last option so helpful that I use an alias to help search the history.
alias past=’history|grep ‘
Now however, with my new familiarity of Control codes, I am starting to use
Ctrl + r – reverse search history
This gives you an interactive search, providing results as you type. Press Ctrl + r again to show an earlier match. Press Ctrl + m (or Enter) to execute the result. Ctrl + j (or cursor right) to select for editing. Ctrl + g (or cursor down) to abandon the search.
To help me remember the most useful Control keys I have written a short alias command
alias ctrl=’echo “Prev, Next, A<<, E>>, F>, B<, cUt<, Kut>, Yank, cLr, Recall MJG”
Bash (Bourne Again SHell) is the default shell on Raspberry Pi Linux distributions and there are plenty of useful guides to using bash history on the internet. Alternatively, if you run the command
it will list all the bash shortcuts; but I suggest that you filter it through grep to narrow the output down to the control codes! Then pipe it to less so that you can scroll through the results.
bind -p | grep C | less