4

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?

3 Answers 3

11

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}

4
  • 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. Commented Jul 8, 2019 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. Commented Jul 8, 2019 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. Commented Jul 8, 2019 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
    Commented Jul 9, 2019 at 0:22
1

As well as X, another normal command dh does exactly that and can be preceded with a number to do it n times.

0

My favorite way of deleting things in normal mode is to press 'd' followed by a motion. This technique is transferable to more things like yanking and visual mode selection. I find the following motion maps useful:

" Go to beginning of line.
map H <C-[>OH

" Go to end of line.
map L <C-[>OF

" Go to beginning of previous word.
map W b

" Go to end of previous word.
map E ge

2
  • 2
    Note that these mappings override several useful builtin commands.
    – D. Ben Knoble
    Commented Jun 8, 2022 at 18:18
  • Good point. I find these so useful that I like having simple and intuitive maps for them. Commented Jun 8, 2022 at 22:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.