Does Vim have an automatic comment function based on file syntax?

I'm not sure if this is possible, but I am interested in making this happen.
Ideally, I would like to map this function to SHIFT + CTRL + 3 .

I am looking for a way to get Vim to enter a comment (single line) that matches the syntax of the file I'm editing. If there are several one-line comment styles, Vim can either automatically select one or give me a choice. If a single-line comment has two parts (for example, /* and */ ), then pressing SHIFT + CTRL + 3 for the first time will start the comment, and the second time will close the comment.

Examples:

  • Python: #
  • JavaScript: //
  • C, C ++: /* c */ or //

I know that there are scripts that will insert comments for you, but I have not seen those that will do this based on the file syntax.

+6
syntax comments vim autocomplete
source share
5 answers

Sorting! I do not believe vim will do this out of the box, but you can install plugins that will make pretty intelligent comments (using the navigation keys, highlighting lines visually, etc.) that are specific to the file type being edited. You can disconnect these plugins from vim.org, and you should be able to create your own key mappings in your .vimrc file if you don't like the ones they come with.

tComment is pretty well regarded and worked for me.

I heard that EnhCommentify might be better, but I did not use it myself.

+5
source share

I highly recommend NERD Comment .

+6
source share

Looks like a similar question: How to comment on vim while indenting?

Use the comment plugin for nerd: http://www.vim.org/scripts/script.php?script_id=1218

+3
source share

See: this script , which provides a function for commenting on a selected area in visual mode.

You want to run the comment in insert mode so that your function looks more:

 fun CommentLines() exe ": s@ ^@".g:Comment."@g" endfun 
+2
source share

Not exactly what you are looking for, but effective, and I suppose you know which comment to use. (all in command mode) Place the cursor on the first line you want to comment on. Then we will set a marker named a (valid names are az, a single character) by typing

 ma 

place the cursor on the last line, then set the marker named b by typing

 mb 

Then comment out the entire block (by searching for a new line and inserting a comment character (note the use of "#" as a search separator, because otherwise we need to avoid "/")

 :'a,'bs#^#//# 

or for Python:

 :'a,'bs/^/#/ 

Disarmament:

 :'a,'bs#^//## 

How we make comment lines, it does not matter if we have other comments already in the file, they will be saved.

-one
source share

All Articles