Skip to Content
Mastering Vim
book

Mastering Vim

by Ruslan Osipov
November 2018
Beginner
330 pages
7h 21m
English
Packt Publishing
Content preview from Mastering Vim

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! commenter#Comment()  let l:line = getline('.')  call setline('.', '# ' . l:line)endfunctionnnoremap gc :call 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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Mastering Vim

Mastering Vim

Damian Conway
Vim Masterclass

Vim Masterclass

Jason Cannon
Modern Vim

Modern Vim

Drew Neil

Publisher Resources

ISBN: 9781789341096Supplemental Content