Whitespace

Introduction

Here is how you can clean up whitespace.

Remarks

See vimcast 4 transcript

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.

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

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