Snapshot of part of a file in Vim: hide comments and blank lines

How to temporarily show a snapshot of a file, hiding comments and an empty line?

The goal would be to get a catch of a C ++ function (for example), in a tight page layout.

The way to do this is to use a global command to filter comment lines

:g!/\/\/ 

but then the syntax highlight and move elements disappear.

The Vim fold function is a way to do this job, but it only reduces a few consecutive comments on one line that is visually intrusive.

In any case, bending is still useful for hiding long C-like comments, for example:

 :set foldmethod=marker :set foldmarker=/*,*/ 

But I was not able to easily collapse both words C (/.../) and C ++ (// ...) in one command.

Perfect trick ::

  • hide (and not just dump on one line) comments and empty lines,
  • it is possible to specify hidden rows with a character in the first column,
  • handles both C and C ++ comments,
  • keeping syntax highlighting
  • and, ideally, the ability to view the code,
  • makes it easy to return to the view you’ve viewed.

To convert the following code:

 /** Let say hello * and do not forget anybody */ void hello( int arg ) { // OMG an hello world! std::cout << "hello" << std::endl; } // where is the world? 

To the following buffer:

 void hello( int arg ) { std::cout << "hello" << std::endl; } 
+6
c ++ c comments vim
source share
1 answer

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.

+4
source share

All Articles