You are not logged in.

#1 2010-07-23 16:19:06

arkara
Member
Registered: 2010-05-02
Posts: 35

Vim comment and uncomment blocks of code

I am trying to configure my .vimrc in order to enable code block comment and uncomment
by ,/ and ,<Space> combinations but it can't seem to work.

Any ideas?

I like this one cause, i can add an if for every filetype
that i want to enable this feature.

if &filetype == "vim"
   map ,/ :s/^/"/<CR> 
   map ,<Space> :s/^"//<CR>
elseif &filetype == "c" 
   map ,/ :s/^/\/\//<CR>
   map ,<Space> :s/^\/\///<CR>:nohlsearch<CR>
elseif &filetype == "cpp"
   map ,/ :s/^/\/\//<CR>
   map ,<Space> :s/^\/\///<CR>:nohlsearch<CR>
endif

Last edited by arkara (2010-07-23 16:20:09)

Offline

#2 2010-07-23 16:20:33

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Vim comment and uncomment blocks of code

There are plugins like nerdcommenter that help w/ such tasks.
http://aur.archlinux.org/packages.php?ID=27698

Offline

#3 2010-07-23 16:30:42

arkara
Member
Registered: 2010-05-02
Posts: 35

Re: Vim comment and uncomment blocks of code

Awesome. But is there a way to change the mappings?

Offline

#4 2010-07-23 18:12:49

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,799
Website

Re: Vim comment and uncomment blocks of code

i've got a function from rson's vimrc that works great.  it toggles comments on/off of a visual block.  you can define start and end comment strings ('//' and '' or '<!--' and '-->' for example) globally or by filetype.

here's some snippets:

" default comment symbol
let g:StartComment="#"
let g:EndComment=""

" example of changing it for a filetype
au FileType c,cpp  let g:StartComment = "//"

" call the function on ,c (my local leader is ',')
vmap <LocalLeader>c :call CommentLines()<CR>

" and the function itself
function! CommentLines()
  try
    execute ":s@^".g:StartComment."@\@g"
    execute ":s@".g:EndComment."$@@g"
  catch
    execute ":s@^@".g:StartComment."@g"
    execute ":s@$@".g:EndComment."@g"
  endtry
endfunction

Offline

#5 2010-07-23 20:43:27

egan
Member
From: Mountain View, CA
Registered: 2009-08-17
Posts: 273

Re: Vim comment and uncomment blocks of code

Block insert works for me. ^V to select a column and I to insert, in this case a comment character.

Offline

#6 2010-07-23 20:49:24

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,799
Website

Re: Vim comment and uncomment blocks of code

egan wrote:

Block insert works for me. ^V to select a column and I to insert, in this case a comment character.

shit, never knew that (X to remove, in this case a comment character, too).  epically useful in general, thanks.

Offline

#7 2010-07-23 22:20:24

bernarcher
Forum Fellow
From: Germany
Registered: 2009-02-17
Posts: 2,281

Re: Vim comment and uncomment blocks of code

brisbin33 wrote:
egan wrote:

Block insert works for me. ^V to select a column and I to insert, in this case a comment character.

shit, never knew that (X to remove, in this case a comment character, too).  epically useful in general, thanks.

One more solution:

X command is ok to remove properly lined up comment character columns.

To insert multiple comment characters I use the substitution command. E.g. select a visual block of lines to comment (V command), then insert the comment characters by "substituting"  the beginning of those lines:

:'<,'>s#^#// #

Or for standard C comments repeat this for the end of line as well with this comment sequence:

:'<,'>s#^#/* #
gv
:'<,'>s#$# */#

Certainly you can remove this by backward substitution over the same visual selection:

:'<,'>s#/\* ##
gv
:'<,'>s# \*/##

Which is useful if the comment characters happen to be not properly lined up.


To know or not to know ...
... the questions remain forever.

Offline

#8 2010-07-24 09:15:34

moljac024
Member
From: Serbia
Registered: 2008-01-29
Posts: 2,676

Re: Vim comment and uncomment blocks of code

Or use emacs, just highlight the lines you want and press M-; tongue

Last edited by moljac024 (2010-07-24 09:16:53)


The day Microsoft makes a product that doesn't suck, is the day they make a vacuum cleaner.
--------------------------------------------------------------------------------------------------------------
But if they tell you that I've lost my mind, maybe it's not gone just a little hard to find...

Offline

#9 2010-07-24 09:22:11

bernarcher
Forum Fellow
From: Germany
Registered: 2009-02-17
Posts: 2,281

Re: Vim comment and uncomment blocks of code

moljac024 wrote:

Or use emacs, just highlight the lines you want and press M-; tongue

SACRILEGE!!!
This is a vim only topic.

(But if I'm on emacs, I certainly use this feature...)


To know or not to know ...
... the questions remain forever.

Offline

#10 2010-07-24 13:42:52

rson451
Member
From: Annapolis, MD USA
Registered: 2007-04-15
Posts: 1,233
Website

Re: Vim comment and uncomment blocks of code

brisbin33 wrote:
egan wrote:

Block insert works for me. ^V to select a column and I to insert, in this case a comment character.

shit, never knew that (X to remove, in this case a comment character, too).  epically useful in general, thanks.

You obviously didn't see my first post about comments tongue


archlinux - please read this and this — twice — then ask questions.
--
http://rsontech.net | http://github.com/rson

Offline

Board footer

Powered by FluxBB