Giter Club home page Giter Club logo

vim-commentary's Introduction

commentary.vim

Comment stuff out. Use gcc to comment out a line (takes a count), gc to comment out the target of a motion (for example, gcap to comment out a paragraph), gc in visual mode to comment out the selection, and gc in operator pending mode to target a comment. You can also use it as a command, either with a range like :7,17Commentary, or as part of a :global invocation like with :g/TODO/Commentary. That's it.

I wrote this because 5 years after Vim added support for mapping an operator, I still couldn't find a commenting plugin that leveraged that feature (I overlooked tcomment.vim). Striving for minimalism, it weighs in at under 100 lines of code.

Oh, and it uncomments, too. The above maps actually toggle, and gcgc uncomments a set of adjacent commented lines.

Installation

Install using your favorite package manager, or use Vim's built-in package support:

mkdir -p ~/.vim/pack/tpope/start
cd ~/.vim/pack/tpope/start
git clone https://tpope.io/vim/commentary.git
vim -u NONE -c "helptags commentary/doc" -c q

Make sure this line is in your vimrc, if it isn't already:

filetype plugin indent on

FAQ

My favorite file type isn't supported!

Relax! You just have to adjust 'commentstring':

autocmd FileType apache setlocal commentstring=#\ %s

Self-Promotion

Like commentary.vim? Follow the repository on GitHub and vote for it on vim.org. And if you're feeling especially charitable, follow tpope on Twitter and GitHub.

License

Copyright (c) Tim Pope. Distributed under the same terms as Vim itself. See :help license.

vim-commentary's People

Contributors

chaoren avatar gsauthof avatar habamax avatar maxbrunsfeld avatar mg979 avatar mmontu avatar tpope avatar zeertzjq avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

vim-commentary's Issues

How do you get it to work?

I've installed commentary, read the help file, set a commentstring and I can use range:Commentary e.g. :7,14Commentary . So I think everything is set up, but typing gc or gcc on a line does nothing.

Is there some special key sequence I have to use before 'gcc'

Incidentally, in the help file it says "Relies on 'commentstring' to be correctly set", but it doesn't say what 'correctly set' means. So I had to guess a few times and followed the example on the github page for apache. A example of what 'correctly set' means would be useful.

Support multiple commentstrings

It would be great when commentary could recognize multiple ways of how a comments can be done in a language. This should only work to comment lines out; commenting should use a single commentstring.

As an example see the following comment in C before calling commentary:

// no comment

After using gcc on that line:

no comment

After using gcc a second time:

/* no comment */

At the moment, using gcc on the first comment ends in:

/* // no comment */

Is there a way to put the comment character in the first column?

I tried ^#\ *%s, but I guess it's not a regex. I'm in Python, and having # characters at a mix of indentation levels is a little confusing. I've been using a pair of mapped regexes to do this for awhile, but I like commentary's toggle abilities and operator-pending mode.

Unable to uncomment unless there's a <space> after comment symbols

Toggling a line comment (e.g., gcc) does not uncomment a line if that line is missing a space between the comment character and the content. Here's an example:

//console.log

Turns into:

// //console.log

It would be nice if Commentary recognized the comment symbols with or without a for padding. This isn't a big deal if you're the only one working in a file as Commentary always adds the padding, but it becomes problematic when you're trying to manipulate someone else's differently formatted comments.

commenting in html/jsp file

Is it possible to configure commentary to use // or /**/ inside <script> tags in html files and <!-- --> elsewhere? If so, it'd be nice to have this kind of thing in jsp/php/erb files as well.

inline comment

is there any way to comment inline like this, in C code for example?

code ; /* comment */

Would a gc text object even make sense?

A few times I've caught myself absent-mindedly doing digc to delete the commented block I'm in. This makes as little sense as digq or dig?, but it did make me wish for a text object in commentary that allowed me to refer to the commented-out section I'm in. I realize it wouldn't be symmetrical; there can't be a text object for pre-commented or post-uncommented code, but I think it would be nice to do other things to code that is commented, beyond simply uncommenting it. Thoughts?

I would also like [gc and ]gc for hopping between commented blocks, but I don't want to get too pushy in a single issue.

Plugin-specific variable instead of &commentstring

I have commentstring overriden for vim-commentary

autocmd FileType php setlocal commentstring=//\ %s

but for foldtext I must locally set it back to the original value:

set foldtext=MyFoldText()
function! MyFoldText()
  let l:commentstr = &commentstring
  if &filetype == 'php'
      let l:commentstr="/\\*%s\\*/"
  endif
  echo l:commentstr
  let l:line = getline(v:foldstart)
  let l:regex = substitute(commentstr, '%s', '\\(.*\\)', '')
  let l:line = substitute(l:line, l:regex, '\1', '')
  let l:regex = substitute(&foldmarker,",","\\\\|",'')
  let l:sub = substitute(l:line, l:regex, '', 'g')
  return v:folddashes . l:sub
endfunction

What do you think about adding plugin-specific commentstring variable (&commentstring by default, if not manually set)?

Breaks indentation

Hi,

while using commentary to comment on javascript files, I have noticed that we add spaces instead of tabs after the commentstring, which breaks linters (jshint) for javascript.

Percent comments

It seems that the plugin gets confused with % comments (like e.g. LaTeX uses). Or maybe I am just confused. Anyway, if I :set commentstring=%%%s, then it comments properly. However, in order to uncomment I have to press \\\ twice: the first time it adds another % before the comment, the second time it removes two %, thereby uncommenting the line.

Am I perhaps setting commentstring wrong or is this a legitimate issue? I've tried \ escaping the % but this just results in an error message and the line getting cleared (I even tried escaping the \).

Can the `\` maps be configured?

The README says one can map other keys to the \ maps, but I would prefer to replace the \ maps altogether.

(I use \ as my leader and already have \\ mapped to something I use all the time (\be -- bufexplorer). Currently vim-commentary clobbers this.)

My workaround is simply to change the maps in commentary.vim, which is fine with me, but I was wondering whether this could be configured.

Anyway, thanks for another top plugin. I even have a chance of following the code because it's fewer than 3,000 lines ;)

Issue with HTML files

For some reason vim-commentary wraps my HTML tags with /* ... */ instead of <!-- ... -->. I'm going with the default configurations, no changes. What am I doing wrong?

Comments in Go are incorrect

Go has two types of comments:

// Single line
/* Multiple
Line */

However, vim-commentary only makes use of the multiple line comment, and uses it as though it were a single line comment.

/* Just */
/* like */
/* this. */

//-comments in a C file

i remember using a different commenting plugin which normally would insert // and had a 'multiline' operator that would insert /* and */ instead. can haz something like that? or do i just need to tweak something in my .vimrc?

Support of block comment

@tpope
Could commentary support block comment in visual mode?
additional information could be provided with buffer vars such as, b:block_cms, b:block_enabled

This is extremely useful when dealing with file types like css (the line based comment of css is way too disturbing for a block).
For example:
1bq2e

Regards

Jade support?

vim-commentary currently comments as:

-# .group-detail.gf-container

while Jade comments look like the following:

// .group-detail.gf-container

filetype invalid after installing the YCM

hello,everyone! i meet a question about this plugin.

I use the pathogen to manage my plugin, and put the all plugins in the bundle dir. After installing the YouCompleteMe plugin, run the gcc in any type of file, will be annotated into /**/

like this picture:
commentary_ques

comment or uncomment all lines in a fold

I would like to comment all lines in a fold when it is closed. I have tried the gcc and Visual}gc
combinations but it does not work in neovim nor vim. Is there any way to comment a closed fold?

No space between comment and code

When I use gcc in a visual selection I get something like the following:

// var example = "hi";
// var anotherExample = "hello";

Could this be changed or given as a setting so that I could have this instead?:

//var example = "hi";
//var anotherExample = "hello";

This change would mainly be because of how I like to differentiate between a // useful comment and a commented out block of code.

On the other hand, if it could support block level comments, that would be better. ;)

Html/XML commenting

Hi!

Would it be possible to escape existing comments in html/xml differently than it is implemented now?
The problem is that the generated xml code is invalid if comments already exist in the file, because -- characters in a commented part are invalid.

Example:
<!-- something -->
becomes
<!-- <1-- something --1> -->

It would be nice if the following happened:
Example:
<!-- something -->
becomes
<!-- <1__ something __1> -->

This renders the page invalid and invalidity completely denies rendering of the file.

How to force comment/uncomment instead of toggle?

In most cases toggling comments is what I want. In some cases I want to force uncommented lines to be commented and leave commented lines commented. I imagine something like this

:g/log.debug/CommentaryComment

would change

log.debug('context', JSON.stringify(context))
...
// log.debug('socket.rooms', JSON.stringify(socket.rooms)) 

to this, effectively commenting out all log.debug statements.

// log.debug('context', JSON.stringify(context))
...
//// log.debug('socket.rooms', JSON.stringify(socket.rooms)) 

Would it be possible to support an optional keybinding?

https://github.com/tpope/vim-commentary/blob/master/plugin/commentary.vim#L84

Currently you need to use the gc shortcut (or \ for legacy devs like me). Would you ever be willing to make this an optional "setting" so users could modify it in their vimrc? I find when I'm remote pairing w/ java or c# developers they commonly like a "jetbrains" set of shortcuts to stay productive. If I could optionally update this binding (or supply another alongside what I have with gc) it would go a long way.

Thanks for such a great plugin and all your awesome vim community work! This project (and vim itself) has helped make me personally more productive year after year :)

Setting commentstring using syntax?

This isn't an issue with commentary per se, but I'm wondering how to assign the commentstring variable for files without explicit FileTypes. For example, I edit R files in Vim and would love to use commentary with them, but adapting

autocmd FileType apache set commentstring=#\ %s

to what seems appropriate

autocmd FileType r set commentstring=#\ %s

has no effect because R doesn't have a Vim filetype. R does have a syntax file ("r.vim"), but the seemingly reasonable

autocmd Syntax r set commentstring=#\ %s

doesn't work. Any help would be most welcome!

Comment with and without suffix

I find useful to include a suffix (like "XXX") on part of the comments.
This can be helpful when commenting some lines for a quick test, or as a short time
TODO reminder. As an example:

for (int i = 0; i < count; i++)
{
   // XXX tempDisabled(); -> result of 'gxx'
   // if () -> result of 'gcc '
   {
      dfa
   }
}

I've tried to use commentary to do both regular and suffixed comments.
My first attempt was to change 'commentstring', execute the original commands and then restore using 'commentstring'. The original <Plug> mappings were used in order to avoid copy code from the plugin, preventing that mappings stop working due to plugin updates.

let g:commentary_suffix = ' XXX'
func! CommentarySetSuffix()
   exe "setl commentstring=" . escape(substitute(&commentstring, '\ze%s', 
            \ g:commentary_suffix, ''), ' ')
endfunc
func! CommentaryRemoveSuffix()
   exe "setl commentstring=" . escape(substitute(&commentstring, 
            \ g:commentary_suffix . '\ze%s', '', ''), ' ')
endfunc

Although this approach works for CommentaryLine and CommentaryUndo,

nmap gxx :call CommentarySetSuffix()<CR><Plug>CommentaryLine
         \:call CommentaryRemoveSuffix()<CR>
nmap gxu :call CommentarySetSuffix()<CR><Plug>CommentaryUndo
         \:call CommentaryRemoveSuffix()<CR>

, this can't be applied to Commentary, as it is not possible call CommentaryRemoveSuffix after the motion is entered.

In order to be able to execute a command after the motion it would be necessary to change the plugin code, adding the following at the end of s:go() function:

  if exists("g:Commentary_callback")
    call g:Commentary_callback()
  endif

On the .vimrc:

let g:commentary_suffix = ' XXX'
func! CommentarySetSuffix()
   exe "setl commentstring=" . escape(substitute(&commentstring, '\ze%s', 
            \ g:commentary_suffix, ''), ' ')
   let g:Commentary_callback = function("CommentaryRemoveSuffix")
endfunc
func! CommentaryRemoveSuffix()
   exe "setl commentstring=" . escape(substitute(&commentstring, 
            \ g:commentary_suffix . '\ze%s', '', ''), ' ')
   unlet g:Commentary_callback
endfunc

xmap gx  :<C-U>call CommentarySetSuffix()<CR><Plug>Commentary
nmap gx  :call CommentarySetSuffix()<CR><Plug>Commentary
nmap gxx :call CommentarySetSuffix()<CR><Plug>CommentaryLine
nmap gxu :call CommentarySetSuffix()<CR><Plug>CommentaryUndo

This change makes all but the xmap gx work.

I still didn't figured out why the xmap doesn't worked, but I'm not sure if this is the best approach. Do you have any ideas?

Wrong commentstring for vim in terminal

I am facing a very weird issue. For some reason :echo &commentstring prints different values in vim & gvim.

I tried :verbose set commentstring from both vim & gvim, interestingly I got different results. Following is the result of executing this for commentary.vim plugin file :

vim output:

commentstring=/*%s*/

gvim output:

commentstring="%s
    Last set from /user/share/vim/vim73/ftplugin/vim.vim

Immidately I fired up :Scriptnames from your plugin scriptease to list all loaded scripts, but to my surprise the above vim ftplugin is loaded in both vim & gvim.

I am guessing I am missing something ? I am sure you would know.

comment works, uncomment leaves partial comment

This has worked for me for a while, today I noticed a bug

starting with this:

tag {

some: css;

}

commenting changes it to this:

tag {

/* some: css; */

}

uncommenting changes it to this:

tag {

* some: css; *

}

When I noticed it, I updated my plugins, but it didn't help.

Erroneous comment string with xdefaults filetype

When editing either ~/.Xdefaults or ~/.Xresources, the filetype is set to xdefaults according to the output of :set ft?. By default, commentary uses CSS style comments, i.e. /* */. So I consulted the readme and added the following line to my .vimrc:

autocmd FileType xdefaults set commentstring=!\ %s

However, after re-sourcing my config and even restarting vim, I'm still getting the CSS-style comments. I was hoping you might be able to shed some light on this. Thanks in advance.

Do not distinguish 'c' for text-objects

Hi,

it's nice that you have implemented text-objects for comments.
However, the distinction between inner/all comment only for 'c' seems unnatural to me.
I think it would be better to have something like 'ac'/'ic' text-objects and let people choose whether they want only the text part or the whole comment.

Is it possible to change the comment style for some filetype?

I am annoyed that C/C++ files are always commented with /* .. */, while I hope there could be one line comment. Also some comments are not right set by default, for instance, I cannot get the right comment in elisp files(filetype:lisp). So is it possible to customize?

Comment out a portion of a line

The vim-commentary documentation states three keyboard mapping examples to comment out whole lines of code, but I'm curious there is any plan to extending this plugin to support portions of a line.

Such as, for example, "\iw" would comment out only single word, and "{Visual}" comment out only highlighted words.

I can attempt to tackle this feature myself, but I'm no Tim Pope when it comes to Vim plugins :)

Don't move cursor when commenting

Is it possible to NOT move the cursor when commenting/uncommenting a line.

Right now, when I use gcc, the cursor moved to the beginning of the line.

C block comments handled incorrectly if delimiters not followed/preceded by space

Hi,

I noticed that the plugin doesn't play well with a style of comments I have to work with. This C code:

/*-----------
|  comment
|-----------*/
foo();

is commented as:

/* /*----------- */
/* |  comment */
/* |-----------*/ */
/* foo(); */

which, as you can see, causes a syntax error at the end of the comment block. The problem occurs if there is any character other than a space after the left delimiter or before the right delimiter.

I was able to make it work without a syntax error (Vim highlights it as a syntax error but it compiles) by removing the spaces from the delimiters that are added in in s:surroundings() and changing the strlen check from 2 to 1. The result is functional but ugly due to lack of whitespace and Vim's syntax error:

/*/1*-----------*/
/*|  comment*/
/*|-----------*1/*/
/*foo();*/

My suggestion is that the pre-existing '/*' and '*/' should be replaced with ' /1* ' and ' *1/ ' i.e. add spaces around the modified delimiter, but I haven't figured out a way to implement it yet.

Commenting js code inside HTML files

In a HTML file, while commenting a code with gcc inside a <script></script> tag, it uses <!----> to mark comments instead of the JS style comment marker //.

Add `$#` syntax for .hamlet

Although html style <!-- --> works for .hamlet in the Shakespeare templating language hamlet, the ideal syntax is to simply prepend $# to a line. It would be great if vim-commentary would support that for .hamlet files.

Block Comment

When commenting more than one line, it should comment using a block style comment if available. For example in Java:

// public void static main(String[] args) {
//     System.out.println("Hello, world!");
// }

versus

/* public void static main(String[] args) {
    System.out.println("Hello, world!");
} */

Support comments, that have no spaces following...

Hi, this just came up at reddit.com
In summary, commentary doesn't toggle comments, that don't have a space in between. E.g. this:

<!--This is a comment-->

won't be properly toggled in a html file.

This could happen because of an application injecting comments or some other user adding comments (who is not using Vim).
I made a patch here

Visual selection on comment block deletes all text

Considering the following block:

# Comment
# Comment 2
# Comment 3
# Comment 4

Highlighting the selection and running gc causes the block to be deleted instead of uncommenting the section.

Edit: Somehow after restarting the terminal window unable to reproduce, closing for now

(un)commenting folds everything back to the set foldlevel

I use foldmethod=marker foldlevel=0 in my .vimrc and sometimes I want to activate/deactivate some vim plugins, so I use gcc to toggle the comment.

Unfortunately it folds my nested manual folds up completely and this is quite unexpected and annoying behaviour in my opinion. I tested it with just vim-commentary enabled so it seems not to be some side effect of other plugins.

Bug when commenting visually selected lines containing an empty commented line

I have encountered following bug when using commentary.vim to comment a block of python code:

x = 12
#
y = 12

Do a Visual line selection for the whole file (set ft=python) with ggVG and press gc. Nothing happens. Removing the # symbol without deleting the line gc works again.

This occurs as long as the commented line is empty or does only contain whitespace:

x = 12
# non empty line: everything works as expected
y = 12

I have tested this with gvim 7.4.873 on linux and neovim v0.1.1-15 under OSX.

commentary.vim fails noisily when 'commentstring' is empty

I was working on a file type I've never worked with and intuitively hit gcj two comment out two lines.

Result:

Error detected while processing function <SNR>40_go:
line    7:
E688: More targets than List items

This was a bit confusing because at first I did not expect commentary.vim to be the culprit.

Perhaps consider failing silently or provide context as to the reason of the failure.

Can't set commentstring for existing vim/ftplugin filetypes

In order to set a different commentstring for htmldjango files I had to create a ~/.vim/after/ftplugin/htmldjango.vim and set the commentstring from there because setting a filetype autocommand for it on vimrc would be overwritten by vim/ftplugin/html.vim

What am I missing?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.