Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In Dos we can input the first several characters to filter command history and find proper one rapidly. But how to do the same thing in Linux ?

for example when I am testing a local server:

sudo /etc/init.d/vsftpd start
wget ...
emacs ...
sudo /etc/init.d/vsftpd restart
sudo /etc/init.d/vsftpd stop

In Dos you can easily type sudo and switch among the three commands beginning with it using arrow keys. But in Linux, is below command the best we can do ?

historty | grep sudo

I don't like it, because history can easily become a mess, and it also need mouse action.

share|improve this question

migrated from Feb 1 '11 at 14:41

This question came from our site for professional and enthusiast programmers.

up vote 5 down vote accepted

Put the following in your ~/.inputrc:

"\e[A": history-search-backward
"\e[B": history-search-forward

Then after typing sudo, Up will search for commands starting with sudo.

(You will have to restart bash.) (Unsure if this works in csh.)

share|improve this answer
The above works for anything using readline (python, gnuplot, gdb, ...). See – pixelbeat Feb 1 '11 at 14:29
@pixelbeat: maybe you should put the "$include /etc/inputrc" at top? Otherwise the system settings could override your customizations. – maxelost Feb 1 '11 at 17:06

In Bash (and most readline-based applications like it) you can press Ctrl-R to bring up a history search function:

$ date
Tue Feb  1 15:40:06 EET 2011
(reverse-i-search)`d': date

By pressing Enter here I get:

$ date
Tue Feb  1 15:40:06 EET 2011
$ date
Tue Feb  1 15:40:52 EET 2011


You can see the full list of history-related Bash keystrokes here.

You can see the current list of history search keyboard bindings:

$ bind -P | grep search | grep history
forward-search-history can be found on "\C-s".
history-search-backward can be found on "\e[5~".
history-search-forward can be found on "\e[6~".
non-incremental-forward-search-history can be found on "\en".
non-incremental-forward-search-history-again is not bound to any keys
non-incremental-reverse-search-history can be found on "\ep".
non-incremental-reverse-search-history-again is not bound to any keys
reverse-search-history can be found on "\C-r".

In my case Page-Up/Down can also be used to search for commands that start with whatever I have already typed, as configured in my /etc/inputrc:

# Page Up/Down cycles through history only for matching entries
"\e[5~": history-search-backward       # Previous
"\e[6~": history-search-forward        # Next
share|improve this answer
Don't forget to mention that by repeatedly pressing CTRL-R you search trough the list. – Felix Feb 1 '11 at 13:47
It seems this command only searches backward, right? – gerry Feb 1 '11 at 13:52
@gerry, yes Ctrl-R only searches backwards – thkala Feb 1 '11 at 14:02
@gerry: Ctrl-S searches forward, but you may need to do stty -ixon to enable it (and disable Ctrl-S and Ctrl-Q from stopping and starting data flow). – Dennis Williamson Feb 1 '11 at 16:32
@Dennis Thank you. Your supplement is helpful. Now this answer may be the most complete usage of Ctrl-R on the web :) – gerry Feb 1 '11 at 17:49

In addition to Ctrl+R, you can type !sudo + Enter in bash and it will execute the last command starting with sudo.

share|improve this answer
Quite useful... until that time where the last command is something along the lines of sudo rm -rf *. I know quite a few people that have been burnt like this. – thkala Feb 1 '11 at 13:44
@thkala: Maybe sudo is one of the commands that should not be used with this ;) – Felix Feb 1 '11 at 13:48
I find that shopt -s histverify histreedit helps me a lot. – Dennis Williamson Feb 1 '11 at 16:29

Enter the first letter like sudo, press Ctrl and then or .

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .