Showing posts with label zsh. Show all posts
Showing posts with label zsh. Show all posts

Monday, January 18, 2010

Shell shortcuts part deux.

In my last post, I demonstrated how you can use the !! and !$ features of your Bash & zsh history to get your work done more quickly.   I use the former all the time, but  the latter?  Well, not so much.  This is because I use something even better for interactive shell use.  It is M-.  That is Meta plus period.  All you Emacs zealots users will know what I mean by Meta.  :)  The Meta-key is Alt on your computer, or sometimes Esc.  Both work on my machine, but I prefer Alt over Esc, because it is less work.

Try doing M-. in your shell (Press and hold the Alt key, and then press the period key).  Like !$, M-. will give you the last part of the last command entered, but this time you can see it immediately.  Better still, you can cycle through all of your commands.  Just keep hitting M-. and the shell will display the last part of each command, in order, from newest to oldest.

Note:   If you use the ESC key for your 'Meta' key, then you need to release the ESC key each time.  Otherwise, if you hold it down and tap the . (period) key, it will return the end of the last command only once, and then start echoing the period after it.

As an example, say you had just finished a

grep fred /etc/passwd

You are ready to use the command line history feature:

vi M-.

Which would expand to (immediately):

vi /etc/passwd

Then, if it looks right, just press Enter.

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