benjamin.althu.es open source projects | blog

Remove ^M in text-files using Vim

  • Vim
  • Feb. 14, 2016, 1:55 a.m.

These are instructions on how to get rid of ^M in text-files with the use of Vim.

In Linux/Unix by pressing:

:%s/^M//g

Note that ^M is inserted by pressing the keystoke Ctrl+V Ctrl+M. This is because Ctrl+V will give the escaped form of the key pressed after this. You’ll see that when you press Ctrl+V <RETURN> it is actually the same. So yes, you can insert return/enter by pressing Ctrl+M in Unix.

In the Windows version of Vim the instruction will not work but you can accomplish the same by using:

:%s/\t//g

The substitute command

  • The % means that this aplies on all the lines in the file.
  • The s/old/new/ is a substitute command where old gets replaced by new.
  • The g on the end means to this mutliple times if found more then once in the same line.

For more info, you can use the help function of Vim by entering:

:help substitute
back to home

blog

projects

about me

I am a computer enthusiast and software developer, passionate about free and open source software and Unix-like operating systems.

Copyright © 2015-2016 Benjamin Althues
The code for this website is released under an ISC License

dispass.org | simphplist.org | cloud.babab.nl