All comments can be removed from the source using the following command:
:%s/\/\*\_.\{-}\*\/\n\{,1}\|^\s*\/\/.*\n\|\s*\/\/.*//
This can be undone with the u command, assuming that no other actions should be undone.
Since vi regexes can be extremely cryptic, the following is an explanation of each part. The entire regular expression is divided into three parts, separated by the OR ( \| ) operator.
\/\*\_.\{-}\*\/\n\{,1}
This corresponds to the comments of the form block /* ... */ . It matches the string '/ *' ( \/\* ), followed by zero or more of any character, including a new line, but as small as possible ( \_.\{-} ), followed by zero or one new line ( \n\{,1} ). The reason that it matches zero or one new line is to handle both the case when there is code in the same line as the comment and the case when the comment is in the line by itself.
^\s*\/\/.*\n This corresponds to the comments of the form //... , where the comment is on the same line. It matches a zero or more whitespace character starting at the beginning of the line ( ^\s* ), followed by the line "//" ( \/\/ ), then zero or more of any character ( .* ) Ending with a new line ( \n ).
\s*\/\/.* This corresponds to the comments of the form //... , where the comment follows the code. It matches any number of spaces ( \s* ), followed by the line // ( \/\/ ), and then any number of characters that are not newline characters ( .* ).
This is the best I can think of at the moment, if I can come up with a way to hide rather than delete comments, I will update this post.
Update: A possible way to simply βhideβ comments may be to color them in the same way as the background. That would make them invisible. However, I currently do not know how possible it is that an idea can be, or how well it will generalize. I don't know enough about colors in vim to write a script to execute this.
cledoux
source share