21

I will very often be flitting between several files on a server. I will run the following example commands in sequence:

nano /web/site/path/to/file
/web/site/script.py
nano /web/site/path/to/anotherFile
nano /web/site/path/to/yetAnotherFile
/web/site/script.py
nano /web/site/path/to/etc

To go from one nano command to the next I currently press Up and Backspace to replace the last segment of the path. The problem with this is I'm really, really lazy.

Is there a keyboard shortcut (or Bashism) that can either:

  • Get replaced by the last path used (with an extra bit on the end)
  • Delete in the current line (eg after pressing Up) back to the last /
5
  • 1
    I'm not sure exactly what you mean, but I think for the first part you can use alt+.?
    – Sparhawk
    Oct 20, 2014 at 22:51
  • @Sparhawk Alt + . is used for something else. Oct 21, 2014 at 9:23
  • @RaduRădeanu Yeah, perhaps I misunderstand the question. I thought "Get replaced by the last path used (with an extra bit on the end)" meant what alt+. does.
    – Sparhawk
    Oct 21, 2014 at 9:26
  • @Sparhawk Again, one of those shortcuts that's really useful about once a month that I just don't seem able to commit to memory. In combination with alt-backspace, it's pretty handy for running similar files through different commands.
    – Oli
    Oct 21, 2014 at 9:31
  • @Oli I tend to use it a fair bit, but I always forget how to move the other way!
    – Sparhawk
    Oct 21, 2014 at 9:34

3 Answers 3

25

I think you're looking for the Alt+Backspace shortcut.

After pressing Up this should delete back to the previous / or space character.

5
  • 8
    NOW I AM UNSTOPPABLE! (Edit: I swear I learn and forgot this shortcut at least ten times a year)
    – Oli
    Oct 20, 2014 at 15:27
  • 3
    What should this do? On my terminal it doesn't seem to do anything different than plain backspace.
    – Bakuriu
    Oct 21, 2014 at 10:52
  • 1
    @Bakuriu This shortcut should be the default one. If you want to find out what it's mapped to on your system run bind -P | grep '^backward-kill-word' Oct 21, 2014 at 11:08
  • 1
    @Bakuriu Same here, I'm on Mac.
    – laike9m
    Oct 28, 2014 at 5:31
  • note Alt+Ctrl+h or ^[+h will also work Feb 22 at 15:49
10

To detect the readline binding that kill a word backward as you wish you can use the following command in your terminal:

bind -p | awk '/kill/ && /word/ && /backward/'

In a default Ubuntu installation, the output could be:

"\e\C-h": backward-kill-word
# shell-backward-kill-word (not bound)

The second line seems without importance at this point, so let's try to see what means first line. From what I understand, the backward-kill-word readline function which in fact delete all characters untill the previous special character (/, ;, , etc.) is bound to the \e\C-h key sequence. Now, in this sequence \e represents the Esc key, \C - the Ctrl key, and \C-h stands for Ctrl+h which is equivalent in this case with Backspace key.

So, you are searching for Esc+Ctrl+h keyboard shortcut which is equivalent with Esc+Backspace and which, because of xterm's behaviour that make Alt key to act as a meta character and meta characters are converted into a two-character sequence with the character itself preceded by Esc (see man xterm), is equivalent with Alt+Backspace.

Now, if you don't like it and you continue to forget it, you can use the following command to create a new shortcut, let say Esc+w, for your purpose:

bind '"\ew": backward-kill-word'

To make this new shortcut persistent all the time for all commands that uses readline, add the following line line to your ~/.inputrc file:

"\ew": backward-kill-word

See help -m bind | sensible-pager for more info.

2
  • 2
    Good answer. More commonly you'd enclose the whole setting in single quotes with the bind command; bind '"\ew": backward-kill-word', but that's nitpicking. And it may be worth mentioning that you can also put "\ew": backward-kill-word in ~/.inputrc, in which case other commands that use readline will also have that binding, e.g. if you enable readline in the python interactive shell, that binding will also apply there.
    – geirha
    Oct 21, 2014 at 7:21
  • @geirha Yes, I am aware what you said, but I thought that the OP knows how to manage this. Anyway, now I realize that my answer is not so useful for the OP (he just wanted to remember the shortcut), but I hope to be useful for other users of this site and for this I edited my answer by adding your observation. Thank you. Oct 21, 2014 at 9:13
1

Another key combination is:

Alt+B then Ctrl+K

Alt + B     Move cursor backward one word on the current line    
Ctrl + K    Clear the line after the cursor

Alt+B then Alt+D

This removes last word without deleting special char between words.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .