0

I would like to have the delete key work similarly in normal mode as it does in insert mode, and it's been a bit tricky to map this. Here is what I have so far:

:nnoremap <Backspace> i<BS><Esc>

However, this doesn't work too well and letters accumulate to the right of the cursor. What would be a better mapping for this? I think the main issue is in pressing the esc in that the cursor floats to the right a bit.

2

I'll post this as an answer since it seems to have worked for you:

:nnoremap <expr> <Backspace> col('.') == 1 ? 'kgJ' : 'X'

Usually this does the same as the X Normal-mode command, that deletes one character to the left.

But then it uses an <expr> mapping to check whether we're executing it on the first column of the line, in which case it uses k to move up one line followed by gJ to join lines without adding any spaces between them.

(This mapping has shortcomings, for instance, it won't work with a count.)

My personal recommendation is that you're better off without such a mapping. I find that this kind of mapping will encourage you to use inefficient movements (such as hitting backspace repeatedly) rather than more efficient movements and text objects comprised of words, sentences, paragraphs, blocks, etc.

Instead of a backspace mapping (or even abuse of the X command), try to use more powerful and efficient motions to select and move around your text in Vim.

| improve this answer | |
  • 2
    +1 for the last two paragraphs – Ralf Apr 29 at 9:45

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.