Introduction

Is typing the worst part of your administration tasks?

Cannot type fast, effectively, properly?

Then learning the bash command history recall and editing is the way to go.

I come from the old sh, ksh, etc (yes, old, right?) where we did not have any fancy command recall etc. At best we could edit commands in a notepad and copy|paste them later. Now moved on to getting more hands-on with bash command recall (and others) it’s good to make the life of a systems administrator easier. So use it.

Disclaimer: even though I have listed and used the options below in my day-to-day work, I tend to execute ‘vi’ as the bash editor alternative. More below.

 

The Basics

Start by some general command line (CLI) commands.

Displaying your command history:

$ history

    1  uname -a

    2  history

    3  ped

    4  pwd

    5  ls -l

    6  who -r

    7  find -print

    8  history

 

Or clear your bash history

$ history -c     

 

<TAB>  auto completion of file or command in $PATH

 

$ !!                execute last command

$ !10              execute command in line number 10 from history

$ !ssh            execute the last command starting with ssh

$ !ssh:p        recall the last command starting with ssh but do not execute

 

$ <command>!$          execute <command> but use the arguments from the previous command

 

$ touch filename1  create an empty file called ‘filename1’

$ vi !$                      will run ‘vi’ command against the same (last) argument used with previous command

 

$ ping google.com -c 4

$ ping !^                  will run ping against the first argument and not the last argument of previous command

$ ping !*                  will fill in all the arguments used in the previous command

 

Short Cuts

The following are keyboard short cuts for editing command line

 

<CTRL>p / Arrow Up            Recall Previous Command

<CTRL>n / Arrow Down        Next Command

 

<CTRL>r          Recall the last command matching the characters you type

<CTRL>o          Run a command you found with Ctrl+R.

<CTRL>g          Leave history searching mode without running a command.

 

<ALT>r            Revert any changes to a command

 

<CTRL>a          Move cursor to start of line

<CTRL>e          Move cursor the end of line

 

<ALT>b            Move left (back) one word

<CTRL>b          Move left (back) one character

<ALT>f            Move right (forward) one word

<CTRL>f          Move right (forward) one character

 

<CTRL>] x                  Moves the cursor forward to next occurrence of x

<ALT><CTRL>] x        Moves cursor to the previous occurrence of x

 

<CTRL>d or <Delete>          Delete the character under the cursor

<CTRL>H or <Backspace>    Delete the character before the cursor

 

<ALT>d            Delete one word after the cursor

<CTRL>w          Cut (delete) the word on the left or cursor and adding to clipboard

<CTRL>k          Cut (delete) from the cursor until the end of the line and adding to clipboard

<CTRL>u          Cut (delete) from the cursor until the start of the line and adding to clipboard

<CTRL>y          Paste (from clipboard after <CTRL>U,K or W)

 

<ALT>t            Swap the current word with the previous word

<CTRL>t          Swap the last two characters before the cursor with each other

<CTRL>_          Undo your last key press. Can repeated to undo multiple key presses

 

<CTRL>xx        swap between the start of line and current cursor position

 

Hot Keys

The following are other miscellaneous hot keys.

 

<CTRL>l          Clears the terminal screen ie ‘clear’ command

 

<CTRL>s          Stops command output to the screen (XOFF)

<CTRL>q          Resumes output to the screen (XON)

 

<CTRL>c          sends kill (interrupt) signal to currently executed process

<CTRL>d          ends shell

 

What’s Next?

And there’s many many more options which I do not use often enough.

Instead, if you are proficient using ‘vi’ then use the ‘vi’ editor options to navigate and manipulate your command line.

$ set -o vi

– then use the following to recall your last command … and start your normal ‘vi’ commands to go up and down, left and write, search, change, etc.

$ <ESC>k

Yes, I will write a page on that too. Stay tuned.

Check out our other Cheat Sheets and Blogs and if you would like us to write a cheat sheet for you, for FREE, (and we find it suitable) Contact Us.