简体   繁体   English

wc输出在vim内部/外部不同

[英]wc output differs inside/outside vim

I'm working on a text file that contains normal text with LaTeX-style comments (lines starting with a % ). 我正在处理一个文本文件,其中包含带有LaTeX样式注释(以%开头的行)的普通文本。 To determine the non-comment word count of the file, I was running this command in Bash: 为了确定文件的非注释字数,我在Bash中运行以下命令:

grep -v "^%" filename | wc -w

which returns about the number of words I would expect. 返回有关我期望的单词数的信息。 However, if from within vim I run this command: 但是,如果从vim内部运行此命令:

:r! grep -v "^%" filename | wc -w

It outputs the word count which includes the comments, but I cannot figure out why. 它输出的字数包括注释,但我不知道为什么。

For example, with this file: 例如,使用此文件:

%This is a comment.
This is not a comment.

Running the command from outside vim returns 5, but opening the file in vim and running the similar command prints 9. 从vim外部运行命令返回5,但在vim中打开文件并运行类似命令将显示9。

I also was having issues getting vim to prepend a "%" to the command's output, but if the output is wrong anyways, that issue becomes irrelevant. 我也遇到了让vim在命令的输出前加上“%”的问题,但是如果输出仍然是错误的,则该问题变得无关紧要。

The % character is special in vi. %字符在vi中是特殊的。 It gets substituted for the filename of the current file. 它代替了当前文件的文件名。

Try this: 尝试这个:

:r! grep -v "^\%" filename | wc -w

Same as before but backslash-escaping the % . 与之前相同,但对%反斜杠转义。 In my testing just now, your example :r! 在我刚才的测试中,您的示例:r! command printed 9 as it did for you, and the above printed 5. 命令会像为您打印9一样,上面打印5。

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

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