Ubuntu: Deleting input back to the last forward slash


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 /


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

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


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.


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.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »