While coding, we usually like to give a lot of space between blocks and other elements so as to make it easier for us to read and interpret the code. However, when you do it too many times on a single block, it gets messier. When navigating between lines, it slows you down and brings you fatigue. So what’s the solution? Simple. Remove the empty lines.
Before compiling your code, you may also wish to remove the unnecessary empty lines to reduce the space it takes up. But what if your file spans across thousands of lines? Would you ever dream of removing them line by line, manually? Of course not!
In Vim, we have the power of g which will allow us to remove all the empty lines without dropping a sweat.
Using the G command
In the whole buffer
The g command in Vim has one of the nicest ways of removing empty lines in a file, which you may possibly never file elsewhere in other text editors! (as long as they don’t use any Vim emulations that have implemented this feature).
The g command is capable of a horde of different text manipulation tasks, one of the most prominent of which is to remove empty lines in a file. To remove all the empty lines in your current buffer, type the following in Vim’s Normal Mode:
Pretty cool and straight forward, isn’t it? But how’s this even working?
The g command is short for global in Vim, which searches for a pattern in the file. When it finds a match, it executes a given command on all the lines of the matching pattern, across the file. If you specify a range of line numbers, it’ll only execute the commands on those specified lines. Anyways, let us break down the above commands to get a deeper insight:
1. The “:” is pressed to enter command mode in Vim
2. The letter g that comes right after is a way to tell Vim that I want to search for a pattern in the file and execute a command on every matching lines. You can also type “:global” instead of just “:g” if you wish.
3. We then type a “/” to insert a pattern. If you’re wondering how the actual syntax looks like, this is how it does:
The range is not necessary if you wish to modify the whole file.
4. After that, we specify the pattern type we want to execute the command on, which in our case is “^$“, a regex (regular expression). The “^” means beginning of the line and “$” means the end of the line. As we’re not providing a string as the pattern, this regex targets the lines which don’t contain any characters or whitespaces, this essentially means that it’s a blank line.
5. We end the pattern search with another “/” to pass a regular command.
6. The last one is the letter “d“, which, in normal mode, deletes stuff. Here in our case, as it is passed with the global command, we just type d without needing to specify a motion for it, and it’ll delete all the lines which match the given pattern.
This way, we can remove all the empty lines in our file. But hold one, there’s a problem.
In the above example, all the empty lines contain only Carriage Returns, which means they are just newlines without any other characters on those lines. If you now press the spacebar on your keyboard for a few times when your cursor in on those blank lines, you’ll be inserting what we call whitespaces. In this scenario, if you use the above given global command to remove empty lines, it won’t work exactly as expected. Take a look:
If you notice properly, the lines where I inserted whitespaces, those lines didn’t properly get deleted. This is probably be a common scenario for you as well, because a lot of times while coding, you may essentially insert whitespaces unknowingly. But not to worry! The g command is powerful enough to recognize these too! Use the following syntax to remove blank lines that contain whitespaces:
This should hopefully remove all the blank lines in your entire buffer. However, if you want to remove lines in a specified range only, refer to the next section.
Within a Range
To delete blank lines in a specified range only, use the following command:
Here num1 and num2 are the line numbers between while you’d like to remove the blank lines from.
To make things easier, you can also visually select lines using the Vim Visual Mode, and then press “:” to specify a range without typing the numbers:
This pretty much covers all the regular ways you could use the g command to remove blank lines in Vim.
Using the V command
The v command is equivalent of the g command, which is basically an alternate expression to the “g!” command in Vim, which can also be called as “reverse global“. It does just the opposite of the regular g command, which is to execute the given command with all the lines where the pattern does not match. Some of you may also find it easier to type.
The following is one of the simplest examples and yet very much all you’d need to delete blank lines in your file:
Here are two things you’d need to note. Firstly, this command won’t help you clear blank lines with whitespaces, so refer back to the g command in case you see blank lines after using the command. I intentionally inserted a few whitespaces on the first line to help demonstrate. Otherwise, this is probably easier to type and remember.
Secondly, if you’re unable to remove the highlighting on the text, which in my case is a green overlay above the characters, simply press Ctrl + L on your keyboard to erase them.
Here you go, fellas! You should now be able to remove empty lines in your file line a pro with the above given commands. If you have friends who use other editors, make sure to give them a sight of the power of g command in Vim. Its just sooo good, many developers have chosen Vim for the sole reason of this command, despite the learning curve which even a lot of senior developers tend to call difficult to learn. So why not give them a taste of what Vim is all about?