views:

59

answers:

4

I recently made the insanely long overdue switch from tcsh to bash. The only thing I miss is tcsh's ESC-p feature: Start typing a command and then hit ESC-p (I actually found the equivalent ctrl-[p easier to type) and it jumps to the most recent command in your history that starts with what you've typed so far.

Perhaps the best answer is to just get used to bash's ctrl-r but so far I don't like it as much. I often start typing a command and then it occurs to me that I've issued it before. With tcsh's feature I could then do ESC p [enter] to re-issue it. It's so quick that I'd usually never use up-arrow for anything more than 2 commands ago.

An example of where I found it especially nice: Long commands often start with a dot, because they're of the form

./myprogram.pl -lots -of -args -and -switches

In tcsh I would issue a command like that, then maybe ls, less, tail, whatever, and then to reissue the long command, 4 keys: dot, escape, p, enter.

How can I do that in Bash? Or, to make it concrete, what's the fewest number of keystrokes in bash to say "repeat the last command that started with a dot"? Can it match or beat tcsh's 4?

+2  A: 

Well, you can do

!.

Which is three characters (including the Enter). Of course, in the general case, you can replace the dot with the unique identifying prefix of your choice.

ire_and_curses
Oh, wow, right, thank you! But I don't actually like it as much as tcsh's version because (a) it requires the forethought to start with the bang instead of just typing the beginning of the command (or ctrl-a ! of course), and (b) it executes immediately. I'd like to jump to that command but have the chance to edit it or make sure it's the one I wanted. You did nail the concrete version of the question though! (Note that this is 3 characters, not 2, since I was including the final Enter.)
dreeves
@dreeves - Yes, you're right - this doesn't handle the forethought problem, just your concrete rephrasing. Looks like you've got a good answer from Dennis Williamson for that though. I've added the enter to match your description.
ire_and_curses
+3  A: 

Add this to your ~/.inputrc file:

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

This will make PageUp act like tcsh's Esc-p and PageDown will go forward through the list.

You can bind \ep instead. If you use PageUp/Down, you may need to see what character sequence your keyboard/terminal produces. Just press Ctrl-V then PageUp and you'll see ^[[5~ if it's the same as \e[5~.

Dennis Williamson
+2  A: 

I was in the same boat as you, needing to switch to bash from tcsh.

I just created a new ~/.inputrc file as follows and everything works great!

$ cat ~/.inputrc
"\ep": history-search-backward
"\en": history-search-forward
Foo
A: 

Personally I prefer ctrl-r - it's interactive search through history - check it, perhaps you'll like it. Subsequent ctrl-r presses jump to next match.

depesz
I'm certainly willing to be persuaded on that point. How many keystrokes are needed for my "repeat last command that started with a dot" example, using ctrl-r? (I'm thinking that since tons of commands since then will *contain* dots that this is kind of a nonstarter for ctrl-r, but perhaps there's a trick I don't know.)
dreeves