Best ways to comment in Vim
In Vim, there are a few ways to comment multiple lines depending on the filetype and whether or not you have certain plugins installed.
As a Site Reliability Engineer, I often find myself working on remote servers where my personal Vim configurations aren't available. Installing plugins on these servers, or cloning my Vim setup, isn’t practical. For this reason, I prefer using Vim’s built-in functionality whenever possible.
In this article, we’ll focus on the most effective ways to comment and uncomment lines in Vim without relying on plugins. I’ll cover some of the best plugins for commenting in a future post..
Comment and uncomment lines without plugins
The comment symbol varies depending on the file type you're working with. For instance, in Python, it's a pound sign (#
), while in JavaScript, it's double slashes (//
). Naturally, you'll need to know which symbol is used for commenting in the specific language you're editing. When using a plugin, this isn’t an issue, as it can automatically detect the file type and insert the correct comment character for you.
Method 1 - Visual Mode with :s
Command (Substitution)
This method works by visually selecting multiple lines and applying a substitution to prepend the comment character for your specific language (e.g., #
for shell, Python, etc., or //
for C-style languages).
Steps:
Enter visual line mode: Press
V
.Use
j
ork
to select the lines you want to comment.Press
:
to enter the command-line mode. The prompt will automatically populate with the range:'<,'>
.Use the
s
(substitute) command to add a comment character::'<,'>s/^/#/
Explanation:
The ‘<,’> is added by Vim and you can ignore it while typing your command
The text replacement syntax is s/original-text/replaced-text/.
Here we have s followed by ^ that indicates start of the line, In other words, we are replacing starting of the line with #.
You can substitute ranges without using visual mode too. For exampe:
:10,20s/^/#/
This command comments lines 10 to 20.
More details on how to substitute text in Vim on my previous post here.
Uncomment Using Visual Mode with :s
Command (Substitution)
This method uses visual mode and a substitution command to remove the comment characters from multiple lines.
Steps to Uncomment:
Enter visual line mode: Press
V
and select the lines to uncomment usingj
ork
.Press
:
to enter the command-line mode. The prompt will automatically populate with:'<,'>
.Use the following
:s
command to remove the comment character (in this example,#
):
:'<,'>s/^#//
Explanation:
:’<,’>
is what you see when you enter command mode after selecting visual blocks
is command for substitution^#
indicates anything starting with#
Noticed no character is inside
//
which means replace them with nothing (remove them).
Method 2 - Insert Mode with Block Insert
Vim allows you to insert text at the beginning of multiple lines using block insert mode.
Steps:
Enter visual block mode: Press
Ctrl-V
.Use
j
ork
to select the first column of the lines you want to comment.Press
I
(capital "i") to enter insert mode at the beginning of all selected lines.Type your comment character (e.g.,
#
,//
, etc.).Press
Esc
to apply the change to all selected lines.
This will prepend the comment character to all selected lines.
Uncomment Using Visual Block Mode
If you have commented multiple lines by prepending characters using visual block mode (Ctrl+V
), here's how you can undo that:
Steps to Uncomment:
Enter visual block mode: Press
Ctrl+V
.Use
j
ork
to highlight the comment characters at the beginning of each line (e.g.,#
).Press
x
to delete the highlighted comment characters from all the selected lines.
Method 3: Using Normal Mode with :normal
Command
The :normal
command deserves it’s own post and I will write about it in the upcoming newsletters, but here is a quick explanation of what it can do.
The :normal command allows to apply any Vim normal mode command to Visual Mode selection.
Let’s see if in action.
Steps:
Enter visual mode: Press
V
to select multiple lines.Run the
:normal
command to insert the comment character at the beginning of each line:
:'<,'>normal I#
This will insert #
at the start of each selected line. It’s similar to method 2, but this time we are using normal mode in the Vim command prompt.
Uncomment Using Normal Mode with :normal
Command
The :normal
command can be used to execute normal-mode actions across a range of lines. Here's how to use it for uncommenting.
Steps to Uncomment:
Select the lines in visual mode (
V
).Run the
:normal
command to move to the beginning of each line and delete the comment character.
:'<,'>normal ^x
Explanation:
^
moves to the first non-blank character of the line.x
deletes the character at the cursor (the comment character).
This will remove the comment characters from all the selected lines.
You can use ‘norm’ instead of ‘normal’ too.
Summary
In this post, we’ve explored the most effective methods for commenting and uncommenting in Vim without relying on plugins. In a future post, we’ll dive into how to handle multiple lines using plugins in Vim.
If you know of any better techniques, feel free to share them in the comments.