简体   繁体   English

如何在gvim中交换C / C ++赋值语句的左手边和右手边?

[英]How do I swap the left hand and right hand sides of a C/C++ assignment statement in gvim?

To be honest, I actually have a solution for this, but Google search finds so many great tips for me from this site, that I had to contribute something back. 老实说,我确实有解决方案,但是Google搜索从该站点为我找到了很多很棒的提示,以至于我不得不做出一些贡献。 Here is what I came up with. 这是我想出的。 For a single line: 对于单行:

s/^\\(\\s\\+\\)\\(.*\\) = \\(.*\\);/\\1\\3 = \\2;/ s / ^ \\(\\ s \\ + \\)\\(。* \\)= \\(。* \\); / \\ 1 \\ 3 = \\ 2; /

For multiple lines starting at the current line, add .,.+<line count> . 对于从当前行开始的多行,请添加.,.+<line count> For example: 例如:

.,.+28s/^\\(\\s\\+\\)\\(.*\\) = \\(.*\\);/\\1\\3 = \\2;/ 。,。+ 28s / ^ \\(\\ s \\ + \\)\\(。* \\)= \\(。* \\); / \\ 1 \\ 3 = \\ 2; /

will substitute on the current line and the following 28 lines. 将替换当前行和随后的28行。 This should also work for Java and Perl. 这也应适用于Java和Perl。 For Python, omit the ending semicolon from the pattern and substitution (unless you're the sort who uses the optional semicolon). 对于Python,请省略模式和替换中的结尾分号(除非您是使用可选分号的排序者)。

After typing all that, I find I do have a question. 输入所有内容后,我发现我有一个问题。 Is there a way to simplify it so I don't have so many escape characters? 有没有一种方法可以简化它,使我没有那么多的转义字符?

Use 'very magic': add \\v to the expression. 使用'very magic':在表达式中添加\\v See :help magic . 参见:help magic Basically, it mean that all non-alphanumeric characters have special (ie regular expression operator meanings) unless escaped, which means that they do not need to be escaped in your usage above. 基本上,这意味着所有非字母数字字符都具有特殊的含义(即正则表达式运算符含义), 除非对其进行了转义,这意味着在上面的用法中,无需将其转义。

Using \\v at the start of your regex can help make it more readable. 在正则表达式的开头使用\\ v可以使它更具可读性。 \\v means "very magic", that all characters have are special except those in the sets '0'-'9', 'a'-'z', 'A'-'Z' and '_'. \\ v表示“非常神奇”,除字符集“ 0”-“ 9”,“ a”-“ z”,“ A”-“ Z”和“ _”中的字符外,所有字符都有特殊含义。

So your first example could be converted like so: 因此,您的第一个示例可以这样转换:

s/\v^(\s+)(.*) \= (.*)\;/\1\3 = \2\;/

The = and the ; =; now need to be escaped to identify them as literals but all the other high-ASCII chars don't. 现在需要转义以将其标识为文字,而其他所有高ASCII字符则不需要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM