Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatic documentation comments creation in Vim

Is there to make Vim create a comment blurb based on the filetype when I open a new file?

I am new to Vim. Here is the functionality I am looking for. When I do:

$ vim hello.py

I want the file to start with:

#Date Created: 24 May 2012
#Last Modified: (This is optional, really)
#Summary: (enter short summary of program here) 
#Author: My Name
#License: ...

etc. I have searched around but I cannot find a solution to do this.

like image 946
yayu Avatar asked May 24 '12 22:05

yayu


People also ask

How do I make comments in Vim?

Hit Ctrl + q in GVIM or Ctrl + v in VIM, then go down to select first character on the lines to comment out. Then press c , and add the comment character.

How do I comment all lines in vi?

Commenting Multiple Linesuse the down arrow to select multiple lines that you want to comment. Now, press SHIFT + I to enable insert mode. Press # and it will add a comment to the first line. Then press ECS and wait for a second, # will be added to all the lines.

How do I comment multiple lines in Vim python?

Using the up and down arrow key, highlight the lines you wish to comment out. Once you have the lines selected, press the SHIFT + I keys to enter insert mode. Enter your command symbol, for example, # sign, and press the ESC key. Vim will comment out all the highlighted lines.

How do you comment out a block of code in Linux?

You start your editor, navigate down to the beginning row of the block you want to comment out. You hit i to go into insert mode, enter // to comment, hit ESC to go back to command mode, hit j to navigate down to the next row, and then repeat until all the rows are commented out.


1 Answers

You could do this without skeleton files using the following:

 autocmd BufNewFile *.py exe "normal O#Date Created: " . strftime("%d %b %Y") . "\r#Last Modified:\r#Summary:\r#Author:\r#License:\r"
 autocmd BufWritePre *.py exe "%s/^#Last Modified:.*$/#Last Modified: " . strftime("%d %b %Y (%T)") . "/e"

Put those in your vimrc.

A potential problem is that the autocmd BufWritePre will append the current time to all lines beginning with:

#Last Modified:
like image 193
pb2q Avatar answered Sep 21 '22 12:09

pb2q