Whitespace

Other topics

Remarks:

Delete trailing spaces in a file

You can delete trailing spaces with the following command.

:%s/\s\+$//e

This 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
  • \s whitespace 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.
  • e suppress 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 :
  • g is 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 d deletes 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 noexpandtab

Then

: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.

Contributors

Topic Id: 8288

Example Ids: 26597,26598,26599

This site is not affiliated with any of the contributors.