The basics

Let's start simple: let's get our plugin to add a key binding that comments out the current line by prefixing it with a Python-style comment (#).

Let's start in ~/.vim/pack/plugins/start/vim-commenter/plugin/commenter.vim:

" Comment out the current line in Python.
function! g:commenter#Comment()
let l:line = getline('.')
call setline('.', '# ' . l:line)
endfunction

nnoremap gc :call g:commenter#Comment()<cr>

In the previous example, we've created a function that inserts # in front of the current line (.) and maps it to gc. As you might remember, g, while having some mappings assigned to it (see :help g), is effectively a free namespace for the user to fill, and c stands for "comment".

Another popular prefix for custom mappings is the comma (,), which is a rarely used command.

Save the file and load it (either using :source, or by restarting Vim). Let's open a Python file and navigate to some line we'd like to comment:

Now, try running gc:

Success! Well, kind of. First, the comment begins at the beginning of the line and not at the current indentation level, as the user might want. Also, the cursor hasn't moved from the current position, which might be a little annoying for the user. Let's fix these two issues.

You can get the indentation level of the line (in spaces) using the indent function:

" Comment out the current line in Python.
function! g:commenter#Comment()
let l:i = indent('.') " Number of spaces.
let l:line = getline('.')
call setline('.', l:line[:l:i - 1] . '# ' . l:line[l:i:])
endfunction


nnoremap gc :call g:Comment()<cr>
let s:comment_string = '# '

" Comment out the current line in Python.
function! g:Comment()
let l:i = indent('.') " Number of spaces.
let l:line = getline('.')
let l:cur_row = getcurpos()[1]
let l:cur_col = getcurpos()[2]
call setline('.', l:line[:l:i - 1] . s:comment_string . l:line[l:i:])
call cursor(l:cur_row, l:cur_col + len(s:comment_string))
endfunction

nnoremap gc :call g:Comment()<cr>

Let's go back to our file:

Now, run gc to comment out an indented line:

Wonderful! But now, we'll probably need a way to uncomment the line as well! Let's change our function to g:ToggleComment():

let s:comment_string = '# '

" Comment out the current line in Python.
function! g:ToggleComment()
let l:i = indent('.') " Number of spaces.
let l:line = getline('.')
let l:cur_row = getcurpos()[1]
let l:cur_col = getcurpos()[2]
if l:line[l:i:l:i + len(s:comment_string) - 1] == s:comment_string
call setline('.', l:line[:l:i - 1] .
l:line[l:i + len(s:comment_string):])
let l:cur_offset = -len(s:comment_string)
else
call setline('.', l:line[:l:i - 1] . s:comment_string . l:line[l:i:])
let l:cur_offset = len(s:comment_string)
endif
call cursor(l:cur_row, l:cur_col + l:cur_offset)
endfunction

nnoremap gc :call g:ToggleComment()<cr>

Let's give it a shot! Reload the script, and go back to our file:

Hit gc to comment the line:

And hit gc again to uncomment it:

Let's make sure we covered corner cases! Let's try to comment out the line without indentation. Move your cursor to an un-indented line:

Hit gc to run our function:

Oh no! Looks like our script is not working well for when there's no indentation. Let's fix it:

let s:comment_string = '# '

" Comment out the current line in Python.
function! g:ToggleComment()
let l:i = indent('.') " Number of spaces.
let l:line = getline('.')
let l:cur_row = getcurpos()[1]
let l:cur_col = getcurpos()[2]
let l:prefix = l:i > 0 ? l:line[:l:i - 1] : '' " Handle 0 indent cases.
if l:line[l:i:l:i + len(s:comment_string) - 1] == s:comment_string
call setline('.', l:prefix . l:line[l:i + len(s:comment_string):])
let l:cur_offset = -len(s:comment_string)
else
call setline('.', l:prefix . s:comment_string . l:line[l:i:])
let l:cur_offset = len(s:comment_string)
endif
call cursor(l:cur_row, l:cur_col + l:cur_offset)
endfunction

nnoremap gc :call g:ToggleComment()<cr>

Let's save, reload, and run the script using gc:

And run gc again to test uncommenting:

Wonderful! The very basic version of our plugin is complete!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.154.171