The incredible vim-commentary plug-in developed by tpope.
As someone who doesn’t use vim frequently, I often forget to take advantage of its features. This post serves as a handy memo to remind myself (and others) about the usefulness of this plug-in.
The Basic Concept of Modes in Vim:
Before we delve into the practical usage of the vim-commentary plug-in, it’s essential to grasp the concept of modes in Vim. Vim operates in three primary modes that determine how we interact with the text:
|Default; for navigation and delete or duplicate, etc.
|For edit text
|For operations like saving, exiting, or work with plug-in, etc.
Use the vim-commentary Plug-in:
The different ways to utilize the vim-commentary plug-in in each mode.
In normal mode, entering “
gcc” comments or uncomments a single line. Repeating the command will toggle the comment status.
The vim-commentary plug-in also allows us to comment multiple lines at once.
By using the command “
gc” followed by a
digit numberand the up or down arrow keys (
k), we can effortlessly comment or uncomment as many lines as we want.
gc” followed by the up or down arrow keys (
k) allows us to comment or uncomment two lines simultaneously, with the indicator adjusting accordingly.
gcap” in normal mode comments or uncomments an entire paragraph or block of text efficiently.
Command Line Mode:
This command lets us comment or uncomment lines 107 to 113. Executing the command again will undo the action.
By using this command, we can comment or uncomment lines that match a specific pattern, such as “alias " Repeating the command will revert the action.
The other modes, Vim also offers a Visual Mode. In this mode, we can select lines and use the “
gc” command to comment or uncomment them effortlessly.
File Type Supported
These filetypes include TOML, plain text, C, C++, C#, Java, Kotlin, YAML, and Apache configuration files.
Insert the command in the
cat >> ~/.vimrc << EOF
autocmd FileType toml,txt,c,cpp,cs,java,kotlin,yaml,apache setlocal commentstring=#\ %s
The vim-commentary plug-in significantly enhances text editing efficiency.
By familiarizing ourselves with its capabilities, we gain the ability to seamlessly comment on or uncomment lines, paragraphs, and even specific patterns of text.