3

How do I customise Bash so that up and down keys are the same as in cmd?

E.g.

$echo command1
$echo command2
$echo command3
$echo command4

In both Bash and cmd, pressing , , , ENTER will run echo command2.

However, subsequently pressing the down-key in cmd will go straight to the command afterward, i.e. echo command3. Pressing the down-key in Bash does nothing. cmd keeps point-in-time history (similar to the Back and Forward buttons in a web browser), but Bash doesn't.

Similarly with cmd, pressing the up-key twice in cmd brings back echo command1, whereas doing this in Bash returns echo command3.

There are many times when I wish to excuse some chain of commands which I previously entered in order. It's really handy in cmd that I can scroll back through history to the first of the sequence and then, , ENTER, , ENTER, , ENTER...

However I cannot find a similar way to do this in Bash. Is it possible to modify ~/.inputrc or some other file in some way to achieve this?

2

Although personally I would not want to do it as I've got used to the bash history, but I can see why people would want to. I have looked into this before, for a colleague at work, who was new to *nix and wanted DOS-like functionality in an xterm, and then, as now, there does not appear to be a solution.

I have just done another extensive search, and can't find anything obvious, apart from:

Strangely, there are plenty of ways to make cmd more bash like, for example, Bash History-like feature for Windows Command Prompt, but sadly not the other way around.

The main problem is, to put it (overly) simply, that the cursor key functionality (and how it interacts with the history) is built into the Bash source code, and scripting, aliases and shell functions can not override that fact1. What you would require is a rewritten bash, with different [switchable?] cursor key functionality built in to it, a sort of "DOSBash", if you will.

Maybe there is the opportunity of a niche market being catered for, and someone will eventually fulfill that need, but I wouldn't hold your breath.


1 If I am incorrect, and it is possible to override the cursor key functionality by a simple resource file change, I would be delighted if someone could put me straight.

  • Thats a good edit, it makes this a much more useful answer :) Don't forget to tag users in a comment when you edit after there comments, if you don't we might forget to come back and change our votes. – Mark Kirby Mar 31 '16 at 21:59
0

The trick I use, such as it is, is to simply repeat the same number of :

$ echo command1
$ echo command2
$ echo command3
$ echo command4

If I want to repeat the sequence from echo command 2 to echo command4, I just hit 3 times to get to echo command2, and then 3 times again to get to echo command3 and 3 times again for echo command4.

So, if you've gone back N items in your history by hitting N times, hitting it N times again will get you to the next command each time because the new one you ran has been inserted.

  • The trick I use, such as it is, is to simply repeat the same number of ↑: – deejbk Apr 4 '16 at 10:07
  • >The trick I use, such as it is, is to simply repeat the same number of ↑: Yes, while that is a valid method, it can be very annoying having to do that if the sequence was last run like 27 commands ago, or even 100 commands ago etc >The main problem is, to put it (overly) simply, that the cursor key functionality (and how it interacts with the history) is built into the Bash source code, and scrip... I understand that modifying /etc/inputrc can change the functionality of ↑ to allow partial search to be done, a very hand feature which I like, is there a similar modification perhaps? – deejbk Apr 4 '16 at 10:13
  • @deejbk yes, this is not a solution, only a limited workaround. Bash has all sorts of nifty features for this though. They just don't involve using the up key. Have a look at spsneo.com/blog/2009/09/19/bash-history-tips-and-tricks and howtogeek.com/howto/44997/… for example. Do those help? – terdon Apr 4 '16 at 10:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.