1

I'm trying to allow using backspace in normal mode. My best attempt is adding the following to my vimrc:

nnoremap <BS> i<BS><ESC>l.

This works great except for two situations:

  • I'm at the second character in the line
  • I delete a tab which is the first tab in the line.

In either case, after deleting, my cursor is on the second character of the line, not the first, as one would expect/desire. I get why this happens, but am not sure if there's a better way to get backspace functionality without having these problems. Any solutions?

4

I'm glad I can share a command I just learned after few years of vim ! The X command in normal mode does what you want. You can remap <BS> to it if you want :

nnoremap <BS> X

Edit

Indeed, if you have spaces in place of tabs, it will delete the space. In the same vein you already achieved, you can do

nnoremap <BS> i<BS><Esc>`^

the ^ mark is

the position where the cursor was the last time when Insert mode was stopped

Sidenote : I suggest you beware of mashing the <BS> key and use normal mode motions to delete things backward, because one <BS> is one "chunk" of editing (pressing u will undo just 1 character deletion). Try db, dge, d^, d0, dF{char}, dT{char} d?{pattern} and more generally d{motion}

  • The problem with this is that it doesn't delete a tab the way backspace does in insert mode. It may be useful to know that I have tabstop, shiftwidth, and softtabstop set to 4 and expandtab turned on. – Simon Alford Jul 8 at 19:06
  • Thank you! I mainly use this for reformatting tabs. (I have <TAB remapped as well in normal mode). I figure since Tab and BS aren't mapped to anything in normal mode I might as well map them do something. – Simon Alford Jul 8 at 22:54
  • Your point about mashing BS is still taken well—is there a way to remove/add tabs in normal mode without adding these remappings? db, for example, does not work well in this situation. – Simon Alford Jul 8 at 22:56
  • 2
    yes, it is intuitivelly << and >> :) And you can use >{motion} to shift blocs. For example >2j indent 3 lines, >ap indent "a paragraph", or gg>G indent the whole file. You're right to remap <Tab> and <BS>, you can also map <CR>. Some people recommend map <BS> gg and map <CR> G, I recently map <Tab> za to toggle fold. But anyway, you can map <Tab> and <BS> to >> and << if you like. Note also the operator = to automatically indent lines, use == for the current line or ={motion}. All this also works in visual mode. – perelo Jul 9 at 0:22
  • perfect, thank you! – Simon Alford Jul 9 at 13:15

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.