Spearfisher
Spearfisher

Reputation: 8773

Vim - line break/comment remapping

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

Upvotes: 0

Views: 84

Answers (1)

Ingo Karkat
Ingo Karkat

Reputation: 172510

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):

Upvotes: 1

Related Questions