vim: create function to remove white spaces

In the .vimrc file, add the following lines ( here ^M is a special character for the return carriage created using CTRL+V):

#remove tabs
function removeM()
function removeWhiteSpace()
%s/^ \+ $//
%s/^ $//
%s/ \+$//
%s/ $//

those functions call then be called within vim typing:

call removeWhiteSpace()

some Explanation of the function removeWhiteSpace().

  1. first line replaces a line that starts and ends with white spaces
  2. second line replaces a single white space that is alone on a line.
  3. third line replaces the end of a line that finishes with trailing spaces
  4. same as 3 but for the single white space case
Please follow and like us:
This entry was posted in Linux and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *