Comment an Entire File in Vim in Seconds (Two Methods Every Developer Should Know)
Whether you're debugging, testing a new implementation, or temporarily disabling code, there are times when you need to comment out an entire file in Vim. While many developers rely on plugins, Vim already provides powerful built-in commands that make this incredibly fast. Here are two efficient approaches.
Method 1: Highlight & Command Mode (Recommended)
This approach is straightforward because you can visually confirm that the entire file is selected before applying the comment.
Step 1: Enter Normal Mode
Press: Esc
Step 2: Select the Entire File
Type: ggVG
Here's what each command does:
ggโ Jump to the beginning of the file.Vโ Enter Visual Line Mode.Gโ Extend the selection to the last line.
At this point, the entire file should be highlighted.
Step 3: Add the Comment Prefix
Press: :
Vim automatically inserts: :'<,'>
This means "apply the following command to the selected lines."
Now complete the command:
:'<,'>s/^/# /
Press Enter. Every line in the file now begins with #.
Use the Appropriate Comment Symbol
Replace # with the correct comment syntax for your language.
| Language | Comment Prefix |
|---|---|
| Python | # |
| Shell | # |
| JavaScript | // |
| TypeScript | // |
| C/C++ | // |
| Java | // |
| Go | // |
| Rust | // |
| Lua | -- |
| SQL | -- |
For example, to comment every JavaScript line:
:'<,'>s/^/\/\/ /
Method 2: Visual Block Mode
This method leverages Vim's powerful column-editing capability to insert text at the beginning of multiple lines simultaneously.
Step 1: Go to the Beginning
gg
Step 2: Start Visual Block Mode
Press: Ctrl + V
Step 3: Select Every Line
Press: Shift + G
The block selection extends to the end of the file.
Step 4: Insert the Comment Character
Press: Shift + I
This enters Insert Mode at the start of the selected block. Now type your comment prefix, for example: # or //
Step 5: Apply It Everywhere
Press: Esc Esc
After the second Esc, Vim inserts the comment prefix on every selected line simultaneously. It's one of those Vim features that feels like magic the first time you see it.
Uncomment Everything
Need to restore the file? If every line starts with #, simply run:
:%s/^# //
This searches every line (%) for # at the beginning (^) and removes it.
For JavaScript:
:%s/^\/\/ //
For Lua:
:%s/^-- //
The same principle works for any comment prefix.
Which Method Should You Use?
Both methods are useful, but each has its strengths.
- Highlight & Command Mode is easy to understand, visually confirms your selection, and is ideal for search-and-replace operations.
- Visual Block Mode is faster once you're comfortable with Vim and is perfect for inserting text at the same column across multiple lines.
If you're just getting started with Vim, I recommend learning Method 1 first. Once you're comfortable with Visual Block Mode, you'll find yourself using it for much more than commenting - it becomes invaluable for editing structured text, logs, configuration files, and source code.
Mastering these native Vim techniques means you can work efficiently without relying on plugins, making your editing experience faster and more portable across environments.
Thanks for reading. Happy Coding!
Comments
No comments yet. Start the discussion.