Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert the ^M linebreak to 'normal' linebreak in a file opened in vim?

vim shows on every line ending ^M

How I do to replace this with a 'normal' linebreak?

like image 524
ByteNirvana Avatar asked May 01 '09 12:05

ByteNirvana


People also ask

How do I get rid of M in vim?

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.

What is the M character in vim?

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 .

How do I go to the first line in vi mode?

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 .

What is new line character in vim?

Substituting by \n inserts a null character into the text. To get a newline, use \r .


2 Answers

Command

:%s/<Ctrl-V><Ctrl-M>/\r/g 

Where <Ctrl-V><Ctrl-M> means type Ctrl+V then Ctrl+M.

Explanation

:%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).

like image 145
LeopardSkinPillBoxHat Avatar answered Oct 12 '22 02:10

LeopardSkinPillBoxHat


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.

like image 39
Paul Tomblin Avatar answered Oct 12 '22 04:10

Paul Tomblin