Commenting c++ codes in Vim

梦想与她 提交于 2019-12-04 16:48:13

About those commands you found:

imap \c :s,^(\s*)[^# \t]\@=,\1// ,ej$a

Let's start with the pattern match, comparing it with your :s/^/\/\/ <CR>j$a.

  • :s, is exactly equivalent to :s/ - you can choose a nonstandard seperator where it makes the expression more readable, so here using , instead of / means less escaping, and no confusion between the comment // and the seperator
  • ^(\s*) matches the beginning of the line (same as yours) and any leading whitespace, capturing the whitespace so we can reuse it later
  • [^# \t]\@= just requires some character other than space, tab or # before the line end, but doesn't capture it (ie, that character won't be replaced)
  • ,\1// , replaces the matched string with the leading whitespace (\1 is whatever was captured by the (\s*) group) followed by //

For example:

^    void foo();

should match like so: ^(\s*)[^# \t]\@= -> ^( )(v@=)void foo();, ie, the first group matches the leading whitespace, and the next expression matches but doesn't consume the v from void. Then, \1// expands to:

^    // 

and the final substitution result is:

^    // void foo();`

(I've marked the start of line with ^ since the indentation is ambiguous otherwise)

Even though the ^ normal mode command goes to the first non-blank, ^ in a pattern (so in the :s, :g, :v, :sort commands) means start of line. See :help /^

Use I to insert at first non-blank, and A to append after last character.

Use:

inoremap \c <c-o>I//<esc>jA
inoremap \d <esc>^2xjA

There are, however, good plugins that do the job. commentary by Tim Pope, or the NERD Commenter. See their documentation.

I recommend using the Enhanced Commentify plugin. You can then add this to your ~/.vimrc file:

let g:EnhCommentifyRespectIndent = 'yes'

and it will give you the behaviour you are after. In visual mode, select the area of code that you want to comment and then type \c. To uncomment use \C.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!