Mod:Hunt Research Group/searchbash

From wiki
Jump to navigation Jump to search

How to... comfortably search trough your shell history

If you use the bash, you are probably aware of the possibility to search through your old commands by pressing the 'up' or 'down' button. A little less well known is the possibility to press 'Ctrl + R' and then start typing the beginning of a command. Then pressing 'Ctrl+R' repeatedly lets you cycle through all commands starting with the letters you entered. To make this a bit more clear: If you press 'Ctrl+R' and then enter vi , pressing 'Ctrl+R' again and again will cycle through all files you opened with VI. To actually execute the command you have to press enter. Be aware that this will show any command from your history starting with what you entered, so qs would produce every 'qsub' as well as 'qstat'.

This can be further simplified by opening (or creating) the file ~/.inputrc and adding the following two lines to it (You might want to check, that the two keys are not already mapped to something else. Just search for the first few letters to the colon):

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

Now log out and back in. If you are logged in from a PC, just start typing a command, like qs and try pressing page-up or page-down. It should do exactly what 'Strg+R' does, but without messing up your command prompt. You can also move the cursor back and forth in your command and start searching something else. So if you had the files 'file_a.com', 'file_b' and 'file_c' and they had all been opened with 'VI' at some point, along with a million other files, I would have to type vi and the cycle to the first instance of 'file*'. I would then move the cursor to the right up to the '_' and if I now pressed page-up or -down I would just be going through all the commands starting with vi file_, so only my files 'a', 'b' and 'c'. It may sound pointless in this example, but once you actually start using it, you wouldn't want anything else...

If you happen to be using a MAC, you can NOT simply press page-up or page-down. You have to press 'Shift+File:Menusym-pageup.gif' or 'Shift+File:Menusym-pagedown.gif'.