vim shows on every line ending ^M
How I do to replace this with a 'normal' linebreak?
How can I remove ^M characters from text files? A. ^M are nothing more than carriage return, so it is easy to use the search and replace function of vim to remove them. That will do the job.
Show activity on this post. The character that vim displays as ^M is CR (carriage return, ASCII character 13). Windows uses both CR and LF (new line, ASCII character 10) to encode line breaks in text files. Linux/Unix use only LF to encode line breaks, and Mac OS uses only CR .
If you're already in vi, you can use the goto command. To do this, press Esc , type the line number, and then press Shift-g .
Substituting by \n inserts a null character into the text. To get a newline, use \r .
:%s/<Ctrl-V><Ctrl-M>/\r/g
Where <Ctrl-V><Ctrl-M>
means type Ctrl+V then Ctrl+M.
:%s
substitute, % = all lines
<Ctrl-V><Ctrl-M>
^M characters (the Ctrl-V is a Vim way of writing the Ctrl ^ character and Ctrl-M writes the M after the regular expression, resulting to ^M special character)
/\r/
with new line (\r
)
g
And do it globally (not just the first occurrence on the line).
On Linux and Mac OS, the following works,
:%s/^V^M/^V^M/g
where ^V^M
means type Ctrl+V, then Ctrl+M.
Note: on Windows you probably want to use ^Q
instead of ^V
, since by default ^V
is mapped to paste text.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With