Friday, May 3, 2024
7
rated 0 times [  7] [ 0]  / answers: 1 / hits: 1791  / 2 Years ago, wed, september 14, 2022, 10:38:53

This strange (to me) behaviour has bamboozled me for years.


In bash, one can press up to refer to the previous command. Say then I accidentally press Ctrl+W and I wish to restore the previous command, but it seems to be gone. If I clear the command, and try again, it's still gone from the history.


E.g.



  1. Let's say I run the following command:


    $ ls /etc/sudoers.d/
    10_at-export 010_pi-nopasswd 010_proxy README


  2. Now press Up:


    $ ls /etc/sudoers.d/


  3. Now press Up then Backspace a few times:


    $ ls



Now how to return to ls /etc/sudoers.d/ from here?


For a long command, I resort to mouse copying and pasting of the above command #mouseisuncool.


More From » command-line

 Answers
4

Alt-r or Esc + r is the readline key-bind for exactly what you're looking for.

In the readline section of the bash man page:



revert-line (M-r)

Undo all changes made to this line. This is like
executing the undo command enough times to return the line
to its initial state.



You can use this even if you've up or down arrowed away from a history item and then come back to it again.


(M stands for Meta. On a normal keyboad / X11 / terminal setup, your Alt key will work as a "meta" for your terminal. Or equivalently you can manually press Esc before hitting another key to apply the same thing, if your terminal isn't set up nicely enough for alt to work.)


The undo command is bound to Ctrl-/, and it will undo the previous keystroke.


For control-w (unix-word-rubout (C-w)), you can also just paste back the data with Ctrl-y (yank), like in Emacs. Similarly for backward-kill-word (M-Rubout, which on a normal keyboard / terminal / X11 config is Alt+Backspace or Escape+Backspace), the killed text goes into the kill ring where you can yank it. (After a C-y, M-y cycles through the kill ring so you can still get at older kills.)


There are many handy line-editing commands, but the list of all available is huge and overwhelming. I definitely don't know most of them. Being able to move the cursor around by whole "words" at a time with ctrl+arrows or M-b / M-f, and kill forwards/backwards words (and yank) is enough most of the time. (I mentioned some of those in another answer)





If I press cancel and try again, it's still gone from the history.



That's not how Bash works if you pressed Ctrl + C


$ echo foo
foo
$ echo ^C # up arrow, C-w, C-c
$ echo foo # up arrow recalls the pristine command
foo

$ bash --version
GNU bash, version 5.1.16(1)-release (x86_64-pc-linux-gnu) # I'm on Arch Linux

Also, running the shortened command will revert the history changes and make a new entry.


(There's also a .inputrc setting you could use, revert-all-at-newline, that makes submitting a command revert all changes to all other history entries.)


[#754] Thursday, September 15, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
cocal

Total Points: 236
Total Questions: 111
Total Answers: 123

Location: Belarus
Member since Tue, Mar 14, 2023
1 Year ago
cocal questions
Tue, Oct 12, 21, 20:46, 3 Years ago
Sat, Oct 8, 22, 04:23, 2 Years ago
Sun, Dec 18, 22, 02:24, 1 Year ago
Wed, Jun 29, 22, 17:31, 2 Years ago
;