In this article, we will learn in vi / vim editor how can we remove all the spaces at the beginning or end of the file. Recently, we have encountered the problem where contents where copied from one file to another file with new file extension. There we found too many uneven spaces in each line at beginning and end.
Let’s read further and solve this problem.
Remove space at the beginning of each line in a file
1. Open the file with vi or vim editor
vi <file-nmae>
OR
vim <file-name>
2. Press ESC key, write down below given command and hit enter
:%s/^s+//
3. Save and Exit from file by pressing ESC key and typing
:wq!
Remove space at the end of each line in a file
1. Open the file with vi editor
vi <file-name>
or
vim <file-name>
2. Press ESC key, write down below given command and hit enter
:%s/s+$//
3. Save and Exit from file by pressing ESC key and typing
:wq!
NOTE: Here, s finds space or tab and + finds one or more occurrences inside the file.
This does not seem to work.
Correct replace is
:%s/^\s+