Monday, April 14, 2008

History

Most people know you can use the Unix history command to see what's been done on the command-line recently.

travis@travis-desktop:/home/travis% history
9985 ls
9986 grep -ri pidgin
9987 grep -ri pidgin *
9988 cd ..
9989 ls
9990 cd .gnome2
9991 grep -ri pidgin *
9992 cd ..
9993 cd .purple
9994 ls
9995 vim prefs.xml

What some people don't know, is you can execute anything in the history list very easily. Just use !number where number is the item number on the left-hand side of the history output.

!9995 would execute `vim prefs.xml'.

7 comments:

Nate said...

Awesome, I never knew that. That will save a lot of repetitive typing.

I did stumble across a neat history trick for those of us who havent switched to zsh yet.

First we enable our .inputrc in .bashrc if we havent already done so:

export INPUTRC=$HOME/.inputrc

And then in the .inputrc file we add:

"\e[5~": history-search-backward
"\e[6~": history-search-forward

This allows me type a past command, for instance:
vim
and then by hitting pageup or pagedown on my keyboard I can scroll through all past commands that started with vim.

Thanks Travis for all the awesome tips, keep them coming!

Travis Whitton said...

Ooh, good tip. Thanks for the info. One thing though, the history tip works in bash and zsh ;-).

Anonymous said...

another cheap history trick

$ !?pidgin?

will excute last command that matches that case, eg. 9991 grep -ri pidgin *

Michael A. Plikk said...

Another neat little trick with the history is putting a whitespace character in front of the command. That way the command won't be stored.

I usually use this when I'm searching through the history output, ie
$ ' ' history | grep 'perl'
(remove aphostrophes around the space)
to find all perl commands without adding the history+grep command to the history listing

Travis Whitton said...

Very cool. Thanks for all the great tips guys.

dave42 said...

You can also use negative numbering for more convenient numbers:
!-1 executes the most recent command
!-2 executes the second most recent etc.

iandexter said...

Using the following only prints out the command:

!command:p

Useful when you need to place other params or arguments.

In ksh, use 'r'.