12

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?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
dreeves
  • 26,430
  • 45
  • 154
  • 229
  • I needed the opposite so that I can make the tcsh environment I'm stuck with at work functional. Now I know: \ep. And I can use bindkey to set it back to shift-up-arrow like I'm used to. Thanks :). – Chinasaur Jun 27 '11 at 21:13
  • Try the solution here: http://superuser.com/questions/351543/bashs-equivalent-of-tcshs-escp Worked for me. –  Feb 20 '14 at 15:05
  • I have access to `esc-p` in bash, although you have to think about using it first or you have to type the data again... – Alexis Wilke Jul 29 '16 at 16:28

4 Answers4

11

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
  • 111
  • 2
  • 2
    This is close but not quite there because tcsh moves the cursor to the end of the line but bash/readline leave it in place. – George Sep 11 '11 at 20:37
10

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 / PageDown, 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~.

Misha Moroshko
  • 166,356
  • 226
  • 505
  • 746
Dennis Williamson
  • 346,391
  • 90
  • 374
  • 439
3

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.

  • 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 Jun 22 '10 at 05:24
2

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
  • 68,372
  • 23
  • 116
  • 141
  • 1
    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 Jun 21 '10 at 17:24
  • @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 Jun 21 '10 at 17:46