LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 01-17-2007, 02:16 PM   #1
galle
Member
 
Registered: Sep 2003
Location: Bergen, Norway
Distribution: Debian testing
Posts: 69

Rep: Reputation: 16
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
 
Old 01-17-2007, 03:21 PM   #2
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 5,995
Blog Entries: 5

Rep: Reputation: 782Reputation: 782Reputation: 782Reputation: 782Reputation: 782Reputation: 782Reputation: 782
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).
 
Old 01-17-2007, 03:52 PM   #3
galle
Member
 
Registered: Sep 2003
Location: Bergen, Norway
Distribution: Debian testing
Posts: 69

Original Poster
Rep: Reputation: 16
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?

Last edited by galle; 01-17-2007 at 03:53 PM.
 
Old 01-17-2007, 04:37 PM   #4
Vrajgh
Member
 
Registered: Aug 2005
Posts: 65

Rep: Reputation: 31
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
 
Old 01-18-2007, 08:25 AM   #5
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 5,995
Blog Entries: 5

Rep: Reputation: 782Reputation: 782Reputation: 782Reputation: 782Reputation: 782Reputation: 782Reputation: 782
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.
 
Old 01-18-2007, 08:30 AM   #6
nx5000
Senior Member
 
Registered: Sep 2005
Location: Out
Posts: 3,307

Rep: Reputation: 52
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.
 
Old 01-18-2007, 08:45 AM   #7
galle
Member
 
Registered: Sep 2003
Location: Bergen, Norway
Distribution: Debian testing
Posts: 69

Original Poster
Rep: Reputation: 16
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 .
 
Old 01-18-2007, 08:56 AM   #8
nx5000
Senior Member
 
Registered: Sep 2005
Location: Out
Posts: 3,307

Rep: Reputation: 52
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.
 
Old 01-18-2007, 08:59 AM   #9
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 5,995
Blog Entries: 5

Rep: Reputation: 782Reputation: 782Reputation: 782Reputation: 782Reputation: 782Reputation: 782Reputation: 782
ctrl-R vs /

I guess so long as you have both hands free the former is OK...
 
Old 01-18-2007, 09:07 AM   #10
galle
Member
 
Registered: Sep 2003
Location: Bergen, Norway
Distribution: Debian testing
Posts: 69

Original Poster
Rep: Reputation: 16
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
 
Old 01-18-2007, 09:31 AM   #11
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 5,995
Blog Entries: 5

Rep: Reputation: 782Reputation: 782Reputation: 782Reputation: 782Reputation: 782Reputation: 782Reputation: 782
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?

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.
 
Old 01-18-2007, 09:35 AM   #12
nx5000
Senior Member
 
Registered: Sep 2005
Location: Out
Posts: 3,307

Rep: Reputation: 52
Train yourself with emacs
 
Old 01-18-2007, 09:53 AM   #13
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 5,995
Blog Entries: 5

Rep: Reputation: 782Reputation: 782Reputation: 782Reputation: 782Reputation: 782Reputation: 782Reputation: 782
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.
 
Old 01-18-2007, 09:54 AM   #14
Vrajgh
Member
 
Registered: Aug 2005
Posts: 65

Rep: Reputation: 31
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.
 
Old 01-18-2007, 09:59 AM   #15
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 5,995
Blog Entries: 5

Rep: Reputation: 782Reputation: 782Reputation: 782Reputation: 782Reputation: 782Reputation: 782Reputation: 782
Right. Just like the tar vs cpio debate its a matter of "religion".
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Shift-arrow and Alt-arrow keys don't work in xterm Aviv Hurvitz Linux - General 2 09-30-2006 03:43 PM
up arrow history leads to first letter snay1 Linux - General 1 03-15-2006 05:53 PM
telnet up-arrow command history eastsuse Linux - Newbie 8 12-04-2004 09:18 AM
Line wrap errors with ssh / xterm / bash and up-arrow to edit brycen Linux - Software 0 09-22-2004 06:01 PM
Enabling arrow keys for history recall subu_s General 1 06-18-2004 07:30 AM


All times are GMT -5. The time now is 01:38 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration