Saturday, January 16, 2010

Shell shortcuts

In Bash and zsh, you can access the command history by using several shortcuts.  For example, by typing !![Enter], the previous command will be executed.  If you are not sure of what the previous command was, you could do !!:p[Enter] and the previous command will be displayed, but not executed.  Now, if you like what you see, you can do !![Enter], and the shell will execute that command.

Another ! shortcut is !$.   This will give you the end of the previous command.  So if my previous command was:

grep fred /etc/passwd

doing !$ would give me /etc/passwd.  This can be handy.  Say for example the user fred exists on the system, and grep returns his record from /etc/passwd.  If there is something I see in one of the fields that I wish to change, all I need to do (provided that grep was the last command I did) is:

vi !$

This will expand to

vi /etc/passwd

If you are not sure how !$ will expand for you (ie., you cannot remember for certain what your last command was exactly), you can use the :p option:

vi !$:p

No comments:

Post a Comment