change up arrow behavior with bash history
Is it possible to change how up arrow works in bash? I would like to be able to browse through the bash history with the up arrow like it is now, but in addition to that, if I've started writing something, I would like it to only browse through the commands which start with the same letters. (If anyone is familiar with the command line interface in Matlab, that's the behavior I'm looking for).
Is this already implement somewhere, or can I make my own script somehow? Cheers |
If you do "set -o vi" then you can use standard vi commands to search and edit your history. (e.g. /ls would locate all occurrences of the ls command with any options).
Another nice thing about this is the ability to just hit "v" when looking at shell history. This will put whatever command is currently displayed into a full screen vi session. This is a good way to build scripts once you've worked out a complex command line you wish to use within a script (say in a for or while loop). |
But would /ls also locate all commands containing ls, not only the ones starting with ls? E.g. would alsamixer also show up in that list?
EDIT: And where do you set that option? In .bashrc or something? |
To get the functionality you desire without setting bash to "vi" mode (which seems to come as a culture shock for most!) you need to play around with your .inputrc file to adjust readline's behaviour. (Bash uses readline for the command line editing and history interaction.)
Ubuntu's default .inputrc has the following lines commented out by default. I just un-commented them and they appear to make pg-up and pg-dn do what you want the up arrow to do. You just need to replace "\e[5~" with whatever code relates to the up arrow. At a guess that might be "\e[A" but I'm not sure. Code:
# alternate mappings for "page up" and "page down" to search the history |
In answer to the question:
Yes you could set it in .bashrc or .bash_profile. /ls would find it anywhere in the line. To find it at the beginning of the line you would do /^ls - the caret is regexp for start of line. As to culture shock - I guess for people that originally learned it the bash way maybe. For us old time Unix folks that learned it the ksh way the bash way of doing things is rather lacking. I'd rather have all the keystrokes allowed by vi/vim. Full screen edit alone makes it a worthwhile setting IMHO. |
Yes its A and B and in general you put it in ~/.inputrc (btw /etc/inputrc has commented examples)
Do you know Ctrl-R in bash? Try it, IMO its better than anything for moving in history. |
Excellent, the uncomment thing worked like a charm!
Thanks for the other suggestions as well, I'll probably look more into them as well. They sound a lot more powerful than the arrows, however for speed nothing beats typing one/two letters and hitting the up arrow to find almost any line you want :). |
Yes but ctrl R can also search for terms in a middle of a line:
<ctrl-R>start will bring you for example: /etc/init.d/apache start <ctrl-R> again will bring you /etc/init.d/sshd start .. Just wanted to let you know this. |
ctrl-R vs /
I guess so long as you have both hands free the former is OK... :p |
Quote:
|
You need two hands to type a single character (/)?
Using a Cyrillic keyboad where you have to do an alt compose sequence to create a / maybe? :rolleyes: Actually I guess on the average keyboard with Ctrl keys on both the left and right you can type Ctrl-R with one hand but it takes 2 fingers and is a bit of a stretch. |
Train yourself with emacs :)
|
eyucks...
I usually slap developers around when they ask me to install Emacs for them. There was a time I worked on emacs for an application but I never grew to love it as so many do. The only thing I'm sure emacs can do that vi doesn't is multiple windows and since we can open as many windows as we want with X windows/Putty or whatever this has never been a big selling point to me. |
Quote:
I think the reason that I found vi-mode to be a culture shock when I tried it some time ago was that I kept finding myself in command mode and not knowing it or visa-versa. (I didn't spend a lot of time on it..) I should try it again though, because I am much more familiar with vi's commands than I am with the bash short cuts. There is a whole wealth of functionality in the shell command line editing and history interaction that could make my life easier but which I don't use because I don't know the short-cuts. |
Right. Just like the tar vs cpio debate its a matter of "religion".
|
All times are GMT -5. The time now is 02:26 AM. |