本文介绍了在Vim中注释c ++代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我要映射\c来注释vim中的当前行(在第一个非空行之前添加'//'到行),并且\d删除行开头的'//' (取消注释)。 我在vimrc中添加了这两行: imap \c< Esc>:s / ^ / \ / \ /< CR> j $ a imap \d< Esc>:s / ^ \ / \ / /< CR> j $ a 行: for(int i = 0; i > 但我想转换to: // for(int i = 0; i 我想要的一个原因是,当我想让vim缩进这一行,它会添加一个额外的标签并将其转换为: // for(int i = 0; i 我该怎么办?我认为^指的是行的第一个非空白字符,但它不是真的。 PS:我找到了这两个命令,但我不知道他们在干什么。你能请你向我解释(因为我需要记住他们,他们更容易记住,当我知道他们是什么)。如果它们的任何部分不必要请告诉我: imap \c< ESC>:s,^ \ \s * \)[^ / \t] \ @ =,\1 //,e< CR> j $ a imap \d ESC ,^ \(\s * \)// \s\ @ !, \1,e< CR> j $ a 解决方案关于您找到的这些命令:让我们从模式匹配开始,与:s / ^ / \ / \ /< CR> j $ a 。 :s,完全等价于: s / - 你可以选择一个非标准的分隔符,使表达式更容易阅读,因此这里使用,而不是 表示更少的转义,并且注释 // 和分隔符之间没有混淆 ^(\s *)匹配行的开头(与yours相同)和任何前导空格,捕获空格以便稍后重复使用 [^#\t] \ @ = 只需要一些空格,制表符或 在行结束之前,但不捕获它(即,该字符不会被替换) ,\1 //,用(\ c $ c)捕获的任何内容替换与前导空格( \1 s *) group),其后是 // 例如: ^ void foo 应该匹配: ^(\s *)[^# \t] \ @ = - > ^()(v @ =)void foo(); 前导空格和下一个表达式匹配但不消耗 v 从 void 。 然后, \1 // 扩展为: ^ // ,最终替换结果为: ^ // void foo();` (我已经标记了 ^ 的开头,因为缩进是不明确的) I want to map \c to comment the current line in vim (add '// ' to before the first non-blank to the line) and \d to delete the '// ' at the beginning of line(uncomment).I've added these two lines in my vimrc:imap \c <Esc>:s/^/\/\/ <CR>j$aimap \d <Esc>:s/^\/\/ /<CR>j$abut when I call this in this line: for (int i = 0; i < n; i++)then it is converted to:// for (int i = 0; i < n; i++)but I want to convert it to: // for (int i = 0; i < n; i++)one of the reasons I want this is that when I want vim to indent this line, it will add an extra tab at the beginning of the line and convert it to: // for (int i = 0; i < n; i++)what should I do? I thought that ^ refers to first non-blank character of line but it wasn't true.P.S: I've found these two commands, but I don't know what are they doing. could you please explain them to me (because I need to remember them and they're easier to remember when I know what are they). if any part of them is unnecessary please tell me:imap \c <ESC>:s,^\(\s*\)[^/ \t]\@=,\1// ,e<CR>j$aimap \d <ESC>:s,^\(\s*\)// \s\@!,\1,e<CR>j$a 解决方案 About those commands you found: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) 这篇关于在Vim中注释c ++代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!