Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim: Remove comment character after double enter

Tags:

vim

In Vim, I like the feature that allows you to auto insert comments after you have started a comment block. How would you make it so that you get a comment character(s) after a newline, but upon getting a second newline that comment is removed and you start a newline without a comment character. This would be helpful if you are working on a comment block and don't want to make anymore comments after that (in my case I also want to apply the same thing to markdown lists, treating */- as comment characters).

For example

// typing a comment
// (cursor here after hitting enter once)

And

// typing a comment

(cursor here after hitting enter twice)
like image 553
EntilZha Avatar asked Oct 06 '14 18:10

EntilZha


1 Answers

This mapping checks if the current line contains only comment leaders before deciding if it does <C-u> or <CR>:

inoremap <expr> <CR> getline(".") =~ '^\s*\(\*\|//\|#\|"\)\s*$' ? "\<C-u>" : "\<CR>"
like image 53
romainl Avatar answered Oct 04 '22 07:10

romainl