Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

:g showing functions and comments below

Tags:

vim

I'm not sure if this can be accomplished with regex, so here goes and hoping for the best.

If in vim I do,

:g/function

I get a list of all function rows.

Now, I'd like that but with comments (!) below until the first non comment row, so I get something like:

3 function MyFunction()
4 !This is a comment
5 !This is also a comment
23 function MyOtherFunction()
24 !This is a comment
25 !This is also a comment

Something like that possible ?

like image 416
Rook Avatar asked Feb 15 '10 19:02

Rook


People also ask

What are the functions of comments?

In computer programming, a comment is a programmer-readable explanation or annotation in the source code of a computer program. They are added with the purpose of making the source code easier for humans to understand, and are generally ignored by compilers and interpreters.

Do comments go above or below code?

As with the above two posters, a short comment about an individual line goes alongside, and larger comments go above. It's extremely uncommon to find comments below a section of code, and if I came across such a thing it would probably take me a moment to figure out exactly what was going on.

What is a comment code?

Code commenting is the practice of sprinkling short, normally single-line notes throughout your code. These notes are called comments. They explain how your program works, and your intentions behind it. Comments don't have any effect on your program, but they are invaluable for people reading your code.

How do you enter a comment in a program?

A comment starts with a slash asterisk /* and ends with a asterisk slash */ and can be anywhere in your program. Comments can span several lines within your C program. Comments are typically added directly above the related C source code.


1 Answers

Yes


:g/^func/.;/^[^!]/-1 print


Update


An explanation was suggested...so here goes... vi(1) is powerful in part because it is a cursor-addressing extension to Ken Thompson's original line-oriented ed(1) editor. (ed(1) and its spinoff ex is still available on Linux after all these years, albeit in clone form like vi itself.) ed and its early-unix siblings were the first programs anywhere to use regular expressions.

Ok, create a file with 26 or so lines, one for each letter of your alphabet and start vi, ed, or ex. (For ed or ex, leave out the : characters.) Try:

:1;/m/p

The general form of a vi command is: addr, addr2 commmand

In my example the command is just p for print. addr1 and addr2 are usually a line number or a regular expression using /re/ to search downward or ?re? to search upward. Try /c/;/g/p which prompts me to explain: the ; causes the editor to switch to the line found by the first address before it evaluates the second address. It doesn't always matter in the default wrapscan mode but if you type :set nows (not in ed) then search patterns won't wrap and the difference between , and ; becomes bigger.

The most important line mode command wasn't used in my example but it should be mentioned here: :s/pattern/replacement/ or :s/pattern/replacement/g. This command can of course take addresses so a typical command is 1,$s/old/new/g The $ identifies the last line. The default address for most commands is the current line but for the global or g command it defaults to 1,$ and has the general form

addr1, addr2 g /pattern/ any_linemode_command

For example, say I'm Jeff but I want to blame Joel for different types of critical errors in the logs. I need to be stealthy and not change the Jeff's on mere warning lines, so I need:

g/critical.*error/s/Jeff/Joel/

That will run the substitute command on every line of the file that has the pattern "critical anything error" and then just change Jeff to Joel.

So now the answer should be fairly clear. The command works as follows: on every line of the file, check to see if the line begins with /^func/ (func at the beginning of the line) and if it does, start with . (the current line) then, resetting the current address to that line (;) search for a line that does NOT begin with !, and if it's found, subtract one from the line number found (back up slightly to the last actual comment) and then just run the print command.

like image 95
DigitalRoss Avatar answered Sep 22 '22 01:09

DigitalRoss