简体   繁体   中英

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 % ). To determine the non-comment word count of the file, I was running this command in 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:

: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.

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.

The % character is special in 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! command printed 9 as it did for you, and the above printed 5.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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