I am practicing some vimscript and, although there are very good plugins out there, I am writing a function for commenting/uncommenting code for the matter of experimenting.
So far, I wrote the following:
def g:MyComment(comment_symbol: string)
if trim(getline("."))[0] == comment_symbol
normal! ^2x
else
?????
endif
enddef
augroup commenting_blocks_of_code
autocmd!
autocmd FileType c,cpp,java. b:comment_symbol = '// '
autocmd FileType sh,vim,python b:comment_symbol = '# '
autocmd FileType tex b:comment_symbol = '% '
augroup END
The function works well for uncommenting one line.
However, I wish to add the string comment_symbol .. " "
at the beginning of the current line in position ^
but I cannot figure out how, i.e. I have to fill the ?????
line according to the mentioned requirements.
If possible, I would like to use builtin vim functions.
The next step would be to modify my function so that it can accept a start_line
and an end_line
as argument, i.e. I want to modify my function signature with def g:MyComment(start_line: number, end_line:number, comment_symbol: string)
so that I can comment/uncomment a bunch of lines picked e.g. by a selection in Visual mode or by explicitly specifying a range.
Then, I can throw everything and install vim-commentary
. :D