2

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:

cd 
sudo /etc/init.d/vsftpd start
wget ...
ls
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.

0

4 Answers 4

6

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.)

2
  • The above works for anything using readline (python, gnuplot, gdb, ...). See pixelbeat.org/settings/.inputrc
    – pixelbeat
    Feb 1, 2011 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, 2011 at 17:06
6

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

EDIT:

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
5
  • 2
    Don't forget to mention that by repeatedly pressing CTRL-R you search trough the list.
    – Felix
    Feb 1, 2011 at 13:47
  • It seems this command only searches backward, right?
    – gerry
    Feb 1, 2011 at 13:52
  • @gerry, yes Ctrl-R only searches backwards
    – thkala
    Feb 1, 2011 at 14:02
  • 2
    @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). Feb 1, 2011 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, 2011 at 17:49
3

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

3
  • 1
    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, 2011 at 13:44
  • 2
    @thkala: Maybe sudo is one of the commands that should not be used with this ;)
    – Felix
    Feb 1, 2011 at 13:48
  • I find that shopt -s histverify histreedit helps me a lot. Feb 1, 2011 at 16:29
2

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

You must log in to answer this question.

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