Search code examples
vim

How to stop Vim from automatically inserting the comment leader when Enter is pressed?


If I'm typing a comment in gVim like this

// this is a comment

and I hit ENTER, it will automatically start the next line with //, so it looks like this:

// this is a comment
//

But usually I don't want to write more comments when using this commenting style. Can I stop gVim from automatically doing this, while still keeping the auto-completing of the /* .. */ commenting style?


Solution

  • Another answer from mine since you don't want to have this triggered for /**/ comments.

    Use:

    inoremap <expr> <enter> getline('.') =~ '^\s*//' ? '<enter><esc>S' : '<enter>'
    

    For o and O:

    nnoremap <expr> O getline('.') =~ '^\s*//' ? 'O<esc>S' : 'O'
    nnoremap <expr> o getline('.') =~ '^\s*//' ? 'o<esc>S' : 'o'