I'm trying to cerate a function who toggle a block of code into comments. I did this:
" Commenting blocks of code. filetype on augroup commentgroup autocmd FileType c,cpp,java,scala let b:comment_leader = '//' autocmd FileType sh,ruby,python let b:comment_leader = '#' autocmd FileType conf,fstab let b:comment_leader = '#' autocmd FileType tex let b:comment_leader = '%' autocmd FileType mail let b:comment_leader = '>' autocmd FileType vim let b:comment_leader = '"' augroup END fun! TestFunc() if (synIDattr(synID(line("."), col("."), 0), "name") =~ 'comment\c') exe ':s/^'escape(b:comment_leader,'\/')'//e' else exe ':s/^/'escape(b:comment_leader,'\/')'/e' endif endfun map <C-t> :call TestFunc() <CR>
The problem is, it add spaces around the
escape(...) part, and in
C language if the
// is not at the first column, it's not considered as commented. So, this function is always commenting, and never uncommenting.
I thought to use a variable like:
let var=escape(b:comment_leader,'\/') exe ':s/^/var/e'
But it's considered as text, not as a variable. And I don't know if we can use a variable in a command like this.
So, is there a way to achieve what I'm trying to do ?