How to comment a line in vim?

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

How to comment a line in vim?

Eric Luo
I want to comment a new line or comment an existing line at the end of
the current line. In Emacs, I have M-; to do the job.

 Could anyone tell me that weather the similar functionality existed?
Reply | Threaded
Open this post in threaded view
|

Re: How to comment a line in vim?

A.J.Mechelynck
Eric Luo wrote:
> I want to comment a new line or comment an existing line at the end of
> the current line. In Emacs, I have M-; to do the job.
>
> Could anyone tell me that weather the similar functionality existed?
>
>
A line starting with (zero or more whitespace and) a double quote is, in
a Vim script, a comment.

A comment can also be added after most (but not all) commands by adding
a double quote and text on the same line. The problem is that some
commands will regard the double quote and what follows it as part of
their arguments. In that case you can either move the comment to another
line, or wrap the command in an ":execute" statement followed by a bar
and the comment:

    " This is a comment
    set nobackup writebackup " Here we set the backup options
    exe "! ls" | " here is how to add a comment on the line of an
external command

See
    :help :quote
    :help :bar
    :help :execute


Best regards,
Tony.
Reply | Threaded
Open this post in threaded view
|

Re: How to comment a line in vim?

Charles E Campbell Jr
In reply to this post by Eric Luo
Eric Luo wrote:

> I want to comment a new line or comment an existing line at the end of
> the current line. In Emacs, I have M-; to do the job.
>
> Could anyone tell me that weather the similar functionality existed?


I have no idea what emacs does, and I suspect that there's quite a few folks
who use Vim have no idea, either.

In the vein of "give a man a fish, feed him once; teach a man to fish,
feed him forever":
do a search at http://vim.sf.net/ for "comment".  Perhaps one of the
plugins there will
address your wishes -- I got 84 hits when I just did it.

Regards,
Chip Campbell

Reply | Threaded
Open this post in threaded view
|

Re: How to comment a line in vim?

Kev-13
With about the same amount of keystrokes you could use :
a \t ;
a - for append to end of line then tab then whatever comment  
character you want.  If you record this to a macro you can use @
[char] to add the comment char to the end of any line no matter where  
you are in the line.
Kevin

On Jun 2, 2006, at 2:57 PM, Charles E Campbell Jr wrote:

> Eric Luo wrote:
>
>> I want to comment a new line or comment an existing line at the  
>> end of
>> the current line. In Emacs, I have M-; to do the job.
>>
>> Could anyone tell me that weather the similar functionality existed?
>
>
> I have no idea what emacs does, and I suspect that there's quite a  
> few folks
> who use Vim have no idea, either.
>
> In the vein of "give a man a fish, feed him once; teach a man to  
> fish, feed him forever":
> do a search at http://vim.sf.net/ for "comment".  Perhaps one of  
> the plugins there will
> address your wishes -- I got 84 hits when I just did it.
>
> Regards,
> Chip Campbell
>

Reply | Threaded
Open this post in threaded view
|

Re: How to comment a line in vim?

Pau-3
I have this in my vimrc to comment/uncomment lines or blocks of text
in visual with the characters - (comment) and _ (uncomment)

Have a look and you'll get the idea. You have to define the symbol for
the extension
of the file you're working on

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Per a comentar/descomentar blocs de text sencers
" "-" comenta i "_" el descomenta. Cal emprar-ho en
" mode "visual"

function! SimboletComentarZsh()
map - :s/^/# /<CR>:nohlsearch<CR>
map _ :s/^\s*# \=//<CR>:nohlsearch<CR>
set comments=:#
endfunction

function! SimboletComentarFortran()
map - :s/^/C /<CR>:nohlsearch<CR>
map _ :s/^\s*C \=//<CR>:nohlsearch<CR>
set comments=:C
endfunction

function! SimboletComentarLatex()
  map - :s/^/% /<CR>:nohlsearch<CR>
  map _ :s/^\s*% \=//<CR>:nohlsearch<CR>
  set comments=:%
endfunction

" AcĂ­ definim el tipus de "comment" segons el fitxer...

autocmd FileType zsh       call SimboletComentarZsh()
autocmd FileType python    call SimboletComentarZsh()
autocmd FileType perl      call SimboletComentarZsh()
autocmd FileType sh        call SimboletComentarZsh()
autocmd FileType octave    call SimboletComentarZsh()
autocmd FileType java      call SimboletComentarJava()
autocmd FileType fortran   call SimboletComentarFortran()
autocmd FileType tex       call SimboletComentarLatex()
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Reply | Threaded
Open this post in threaded view
|

Re: How to comment a line in vim?

Bertram Scharpf
In reply to this post by Eric Luo
Hi,

Am Freitag, 02. Jun 2006, 22:27:42 +0800 schrieb Eric Luo:
> I want to comment a new line or comment an existing line at the end of
> the current line. In Emacs, I have M-; to do the job.
>
> Could anyone tell me that weather the similar functionality existed?

Something like this:

  :noremap <F2> A<space><c-r>=printf(&commentstring,"\r")<cr><c-o>0$<esc>-$Jcf$<space>

I don't know how to map it to Alt-;.

If you're lucky, the filetype plugin did set the
'commentstring' option correctly.

Bertram


--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-scharpf.de