Sometimes in insert mode I hit <CR> by mistake and I have to hit the backspace multiple times (depends on the indentation) to get back where I was.


  <p> This is some text []</p>

The cursor is [] and I hit <CR> :

  <p> This is some text 

Now I have to hit backspace twice to get back to the previous line (this case isn't so bad but it doesn't have a lot of indentation).

Do you know a better way to do ?

  • 1
    ctrl+w twice seems to work and be pretty easy – reergymerej Sep 19 '16 at 13:41
up vote 10 down vote accepted

How about:

inoremap <C-\> <C-o>:left 0<Cr><BS>

<C-o> will execute a normal mode command (:left 0), which will remove all indent, this will also put your cursor at the start of the line. <BS> will get you back to the last line.

This is a bit like a "backwards J".

Unfortunately, mapping Shift+Enter or Ctrl+Enter doesn't seem possible, so I choose <C-\>, as being "near the enter".

  • "Near the enter" depends on your keyboard: UK ones have `` next to the left-hand shift :-) – TripeHound Jul 7 '15 at 15:58
  • 2
    Why not use <C-u> instead of <C-o>:left 0<CR>? – jamessan Sep 1 '15 at 16:14
  • @jamessan This works as a "backwards J": So foo<CR>bar<C-\> will leave you with foobar. IMHO it's more useful, but YMMV. – Martin Tournoij Sep 1 '15 at 17:09
  • Right, but what I'm saying is that <C-u> (i.e., i_CTRL-U) should be the same as <C-o>:left 0<CR>. – jamessan Sep 1 '15 at 17:13
  • 1
    @jamessan Well, that depends on the cursor position. Typing foo<CR>bar<C-u> will leave you with foo<CR> (+ any auto-indent that Vim may have added). <C-o>:left 0<CR> will always behave the same regardless of the cursor position. – Martin Tournoij Sep 1 '15 at 17:32

I have this in my vimrc, the second part is relevant:

inoremap <expr> <silent> <cr> pumvisible() ? "<c-y>" : "<c-g>u<cr>"

What this does: everytime you hit Enter in insert mode, it will also "break the undo sequence, start new change" (see h: i_CTRL-G_u). This means, if you hit Enter by mistake, you can now undo your change without removing previous inserted lines, either with ESC u a, or with Ctrl-o u, as Statox proposed. Basically, each line can be undone separately.


EDIT/UPDATE: If you happen to use Vim emulation in Emacs, a.k.a. evil-mode, you can use this package for a similar effect.

  • Now I'm curious about the first part and how <c-y> relates to the pop-up menu. – 8bittree Jul 7 '15 at 13:16
  • 2
    See this answer: . Ctrl-y in insert mode will complete the current popup selection, and the above mapping will allow Enter to complete too. – VanLaser Jul 7 '15 at 13:19
  • Nice trick. However, I'd probably replace the last part "<c-g>u<cr>" by &paste ? "<cr>" : "<c-g>u<cr>", otherwise next time you paste by mistake 200 lines of HTML you'll have a lot of fun undoing it. :) – lcd047 Jul 7 '15 at 14:51
  • 2
    ... and the explanation: when paste is on, Insert mode mappings are disabled (as per Vim's help) – VanLaser Jul 31 '15 at 18:34
  • 1
    I like this solution better because the other solutions relying on Backspace, Ctrl-W, and Ctrl-U might not work depending on how the backspace option is set. – doubleDown Nov 1 '15 at 10:50

The way I do it is Ctrl-wBackspace.

Ctrl-w to delete the last word (I forgot if this is standard vim or not), and since it's just spaces it will take me back to the beginning of the line. Then Backspace will take me back to the previous line where I was.

  • Sweet, I didn't know about the Ctrl-wuse in insert-mode. But the Carpetsmoker♦ is still quicker. Thus the default binding of `Ctrl-\`doesn't seems that useful. – nobe4 Jul 8 '15 at 7:44
  • 1
    Ctrl+w is "standard Vim" ;-) See: :help i_CTRL-W. And pressing it twice seems to do the trick as well? – Martin Tournoij Jul 8 '15 at 7:44
  • 1
    I just saw on the help page that <C-u> may be more usefull as it deletes all the characters on the new line – nobe4 Jul 8 '15 at 7:46

My way out of this is (from insert mode) UpCtrl-oJDel. Possibly not the most efficient combination, but it's what I'm doing without needing to think about it.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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