0

I've added the following line un my .vimrc to add a line break when I press enter in normal mode:

"" insert line break in normal mode on Enter
nmap <S-Enter> O<Esc>
nmap <CR> o<Esc>

This works fine except when I want to comment the current line in normal mode by pressing cmd+/ where it comments the current line and add a line break which is also commented. How can I fix this?

Many thanks

Spearfisher
  • 8,445
  • 19
  • 70
  • 124
  • Show the output of `:map `, please. – Michael Foukarakis Sep 17 '14 at 11:02
  • NERDCommenterToggle – Spearfisher Sep 17 '14 at 11:16
  • This is default behavior when adding lines after comments. It can be disabled via `set formatoptions-=o`. See `:help 'formatoptions'` and `:help fo-table` – OhleC Sep 17 '14 at 12:21
  • 1
    You may want to alter you mappings to not use insert mode. I personally would use Tim Pope's unimpaired plugin but a good vanilla approach is to use `:put` with the black hole register instead. You can see some examples of these mapping in this post: [VIM - How to map Shift-Enter](http://stackoverflow.com/questions/16359878/vim-how-to-map-shift-enter/16360472#16360472) – Peter Rincker Sep 17 '14 at 16:31

1 Answers1

1

The comments together give the answer; here's the summary:

You see the default formatting behavior when inserting a new line after a commented one. It's caused by the o value in 'formatoptions'. You could modify your mapping to set formatoptions-=o

But there are alternative approaches for inserting a new empty line:

nnoremap <silent> <S-Enter> :put! _<CR>
nnoremap <silent> <CR> :put _<CR>

(PS: You should use :noremap; it makes the mapping immune to remapping and recursion.)

Also, there are plugins that provide this (and several related mappings):

Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324