When editing c
and cpp
files, this is the behavior I want to achieve:
- When typing a comment line of the form
// Line...
, pressing<CR>
should not add the comment header//
in the next line. - However, if while typing the
// Line...
, the line length exceeds thetw
I set, auto wrap should break the line, and should insert//
in the next line. - The auto comment insertion behavior of
/* * * */
style comments should not change. That is, after typing in/*
, both explicitly hitting<CR>
and auto wrap should insert*
into the next line, produce the single space indentation, and if I type/
, it should close the comment block with*/
, properly indented.
I have fo=tcrqn
and comments=s1:/*,mb:*,ex:*/,://
set for c
and cpp
files.
I tried the following:
- Following the suggestion in this superuser answer, modified for
inoremap
instead ofnnoremap
:
inoremap <silent> <cr> <esc>:set paste<cr>o<esc>:set nopaste<cr>:startinsert<cr>`
This does not work because although the desired behavior for //
style single line comments are achieved, it destroys the behavior of /* * * */
style block comments, preventing auto insertion of *
characters when I press <cr>
in a comment block.
- Then I tried modifying it to the following:
inoremap <silent> <cr> <esc>:set comments-=://<cr>:set comments+=f://<cr>o<esc>:set comments-=f://<cr> set comments+=://<cr>:startindent!<cr>
This mostly works, except that when the *
is inserted by pressing <cr>
in the block /* * * */
comments, the inserted indentation is now missing. That is, representing the cursor position with |
, after pressing <cr>
I want something like:
/*
* Line ... here I press <cr>
* | <- Notice space between cursor and *
*/
But what I get is:
/*
* Line ... here I press <cr>
*| <- No space between cursor and *
*/
- I tried to fix the missing indentation problem by adding and deleting a space character:
inoremap <silent> <cr> <esc>:set comments-=://<cr>:set comments+=f://<cr>o<Space><BS><esc>:set comments-=f://<cr> set comments+=://<cr>:startindent!<cr>
This does fix the indentation issue, but now I have a new problem: typing /
does not close the comment bock, but instead inserts an /
character. This is what I want:
/*
* ...
* | <- cursor is here. I type '/', and the line becomes:
*/ <- Notice no space between * and /
But I get:
/*
* ...
* | <- cursor is here. I type '/', and the line becomes:
* / <- Notice there is a space between * and /, comment block is not closed.
At this point I am out of ideas. Any help is much appreciated.
fo=tcrqn
for/**/
, andfo=tcqn
for//
, right?o
instead of<CR>
when you don't want a comment to continue.fo=tcrqn
for/**/
andfo=tcqn
for//
is what I want. I want to avoid having to use a different key for ending and continuing comments, if possible.