I am trying to write some functions in my .vimrc
to comment and uncomment my code files. My program works successfully except with .ttl
files.
The functions i use are the following:
" Add comment and uncomment
map qq :call Comment()<CR>
map ww :call Uncomment()<CR>
function! Comment()
let ft = &filetype
if ft == 'php' || ft == 'ruby' || ft == 'sh' || ft == 'make' || ft == 'python' || ft == 'perl'
silent s/^/\#/
elseif ft == 'c' || ft == 'cpp' || ft == 'java' || ft == 'javascript' || ft == 'go'
silent s:^:\/\/:g
elseif ft == 'ttl'
silent s/^/;//
elseif ft == 'vim'
silent s:^:\":g
endif
endfunction
function! Uncomment()
let ft = &filetype
if ft == 'php' || ft == 'ruby' || ft == 'sh' || ft == 'make' || ft == 'python' || ft == 'perl'
silent s/^\#//
elseif ft == 'c' || ft == 'cpp' || ft == 'java' || ft == 'javascript' || ft == 'go'
silent s:^\/\/::g
elseif ft == 'ttl'
silent s/^\;/
elseif ft == 'vim'
silent s:^\"::g
endif
endfunction
Remove the last slash: silent s/^/;/
and it will work. By the way, there are lots of plugins that already do the same. However if you still want to write your own script, i'd suggest you to check :he commentstring
and use only nonrecursive mappings , ie nnoremap
instead of just map
.
As Andy Rk mentionned it some plugins exists to do that: If you're not creating these functions for learning purpose, using the plugins would save you a lot of work.
I personnaly use NERDCommenter which is really good I think it is really worth a look:
The installation is really simple: If you use a plugin manager you simply have to add a line in your .vimrc
. (For example with vim-plug just add Plug 'godlygeek/tabular'
to your .vimrc
, run :PlugInstall
and voilà it's ready to run).
A large number of filetype are supported : The plugin works with 300+ types of files out of the box. Think to all to work that would save you.
The behavior is pretty sensible : A lot of different actions are possible, for example with a .c
file you can comment a selection in different ways, just visually select some line of code and see the result:
Leader + c + c
/*int main()*/
/*{*/
/*printf("Hello World");*/
/*return 0;*/
/*}*/
/*int main()
{
printf("Hello World");
return 0;
}*/
/*
*int main()
*{
* printf("Hello World");
* return 0;
*}
*/
A lot of other options are available as described in the README .
If your don't like visual selection you can also prefix use [count]mapping
to apply the action to the [count]
lines after your cursor.
Also you can simply uncomment any commented line with Leader + c + u .
Maybe you should give it a try and then see if you still need to write your custom functions.
I would highly recommend commenting plugin like commentary (which I use), Nerd Commenter , EnhCommentify , tComment , ..., etc . There is no shame in using a plugin. Especially since comment toggling is deceptively more complicated than expected.
Simply set 'commentstring'
to your comment style for your filetype. Most often this is already setup for you if Vim recognizes your filetype. See :h 'commentstring'
.
Example of changing 'commentstring'
for php. Add the following to ~/.vim/after/ftplugin/php.vim
:
set commentstring=#\ %s
Note: I use commentary this may not work for other comment plugins. As always, read the documentation.
:h 'commentstring'
:h :set
:h filetype
:h new-filetype
:h ftdetect
:h ftplugins
:h after-directory
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.