LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   change up arrow behavior with bash history (https://www.linuxquestions.org/questions/linux-software-2/change-up-arrow-behavior-with-bash-history-520242/)

galle 01-17-2007 02:16 PM

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

MensaWater 01-17-2007 03:21 PM

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

galle 01-17-2007 03:52 PM

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?

Vrajgh 01-17-2007 04:37 PM

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
"\e[5~": history-search-backward
"\e[6~": history-search-forward


MensaWater 01-18-2007 08:25 AM

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.

nx5000 01-18-2007 08:30 AM

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.

galle 01-18-2007 08:45 AM

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

nx5000 01-18-2007 08:56 AM

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.

MensaWater 01-18-2007 08:59 AM

ctrl-R vs /

I guess so long as you have both hands free the former is OK... :p

galle 01-18-2007 09:07 AM

Quote:

I guess so long as you have both hands free the former is OK... :)
Well, on my keyboard both ctrl-r and / needs two hands :)

MensaWater 01-18-2007 09:31 AM

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.

nx5000 01-18-2007 09:35 AM

Train yourself with emacs :)

MensaWater 01-18-2007 09:53 AM

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.

Vrajgh 01-18-2007 09:54 AM

Quote:

Originally Posted by jlightner
In answer to the question:
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.

It is definitely a case of personal experience and preference. As far as I can tell there is nothing that can be done in vi mode that cannot be done in the "normal" mode; it's just a case of which commands or short-cuts are most familiar each individual. For instance, a quick hunt through bash's documentation (I clearly don't have enough to do...) shows that full screen edit can be achieved with "C-x C-e" .

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.

MensaWater 01-18-2007 09:59 AM

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.