Whitespace
Introduction#
Here is how you can clean up whitespace.
Remarks#
Delete trailing spaces in a file
You can delete trailing spaces with the following command.
:%s/\s\+$//eThis command is explained as follows:
- enter Command mode with
: - do this to the entire file with
%(default would be for the current line) - substitute action
s /start of the search pattern\swhitespace character\+escaped + sign, one or more spaces should be matched- before the line end
$ /end of the search pattern, beginning of replacement pattern/end of the replacement pattern. Basically, replace with nothing.esuppress error messages if no match found
Delete blank lines in a file
You can delete all blank lines in a file with the following command: :g/^$/d
This command is explained as follows:
- enter Command mode with
: gis a global command that should occur on the entire file/start of the search pattern- the search pattern of blank line is
^g /end of the search pattern- Ex command
ddeletes a line
Convert tabs to spaces and spaces to tabs
You can convert tabs to spaces by doing the following:
First check that expandtab is switched off
:set noexpandtabThen
:retab!which replaces spaces of a certain length with tabs
If you enable expandtab again :set expandtab then and run the :retab! command then all the tabs becomes spaces.
If you want to do this for selected text then first select the text in visual mode.