|
| 1 | ++++ |
| 2 | +date = '2025-03-22T19:07:01+01:00' |
| 3 | +title = 'Annoying Linux: Ctrl-Backspace' |
| 4 | +summary = "Why doesn't it behave the same everywhere?!" |
| 5 | +tags = ["#annoyingLinux"] |
| 6 | ++++ |
| 7 | + |
| 8 | +In the majority of systems that I interact with, <kbd>Ctrl</kbd> - <kbd>←</kbd>/<kbd>→</kbd>, means |
| 9 | +*"move one word left or right"*. And naturally then, <kbd>Ctrl</kbd> - <kbd>⌫</kbd>/<kbd>Delete</kbd>, |
| 10 | +means *"delete previous word* or *"delete next word"*. |
| 11 | + |
| 12 | + |
| 13 | +On the graphical side of Linux systems, this seems to be the default behavior, but not when |
| 14 | +interacting with the terminal. |
| 15 | + |
| 16 | +In `bash` and `zsh`, <kbd>Ctrl</kbd> - <kbd>Delete</kbd> behaves as expected, but |
| 17 | +adding <kbd>Ctrl</kbd> to <kbd>⌫</kbd> doesn't change it's behavior at all. Instead, you'll notice |
| 18 | +that <kbd>Alt</kbd> - <kbd>⌫</kbd> does what you expect. This probably stems from the ancient |
| 19 | +history of emacs. |
| 20 | + |
| 21 | +And in vim, nothing works (surprised?). |
| 22 | + |
| 23 | +## How do we fix it? |
| 24 | + |
| 25 | +For `bash`, we can fix this by adding: `"\C-H":"\C-W"` to `~/.inputrc`. |
| 26 | + |
| 27 | +*What does that string even mean? And how does it work?* |
| 28 | + |
| 29 | +That's a tangent I don't want to get into right now. Checkout the |
| 30 | +[Further reading](#further-reading) section if you're interested. |
| 31 | + |
| 32 | +For `zsh`, we have to use `bindkey`. Add this line to your `~/.zshrc` file: |
| 33 | + |
| 34 | +```bash |
| 35 | +bindkey '^H' backward-kill-word |
| 36 | +``` |
| 37 | + |
| 38 | +And finally, vim. To fix <kbd>Ctrl</kbd> - <kbd>Delete</kbd>, you can simply add this to `~/.vimrc` |
| 39 | + |
| 40 | +```text |
| 41 | +" Make Ctrl-Delete delete the next word in "normal mode" |
| 42 | +nnoremap <C-Del> dw |
| 43 | +" ditto "insert mode" |
| 44 | +inoremap <C-Del> <C-o>dw |
| 45 | +``` |
| 46 | + |
| 47 | +But to fix <kbd>Ctrl</kbd> - <kbd>⌫</kbd>, I can't just ask you to copy paste some text, because |
| 48 | +the `^H` in the snippet below is just a visual representation of the actual value. |
| 49 | + |
| 50 | +**Open `~/.vimrc` with vim**. Then, instead of writing `^H` in the file, **in insert mode**, press |
| 51 | +<kbd>Ctrl</kbd> - <kbd>V</kbd> and then <kbd>Ctrl</kbd> - <kbd>⌫</kbd>. You should then see a `^H` |
| 52 | +appear. |
| 53 | + |
| 54 | +In the end, you should have this: |
| 55 | + |
| 56 | +```text |
| 57 | +" Equivalent for ctrl-backspace |
| 58 | +nnoremap ^H db |
| 59 | +inoremap ^H <C-o>db |
| 60 | +``` |
| 61 | + |
| 62 | +See you in the next one! |
| 63 | + |
| 64 | +## Further reading |
| 65 | + |
| 66 | +* `man 1 stty` |
| 67 | +* `man 3 readline` |
| 68 | +* <https://en.wikipedia.org/wiki/Control_character#In_ASCII> |
| 69 | +* <https://en.wikipedia.org/wiki/Caret_notation> |
0 commit comments