Live Chat!

regex in vim to clear the trailing character

          0 votos

December 6th, 2006 mysurface Posted in Text Manipulation, vi, vim | Hits: 6082 |

Sometimes, the file created at windows may tag alone the trailing character at the end of every line, where user press enter. It may shows as ^M. It is very irritating! to clear it off is very easy in vim.

:%s/\s*\r*$//

%s is global search, change effect on every lines, \s is space \s* is to search for one or more space or no space, \r* is one or more or none carry return, $ indicate \s*\r* must appear at the end of the line, // means nothing. So searching for trailing character which we don’t want and replace with nothing.

It can be written as

:%s#\s*\r*$##

Donno vim? Learn it up, refers to this post. It is very to learn up, the requirement is you have to learn it and use it often.

Leave a Reply