Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gVim - Pattern not found: ^M when trying to remove newline

Tags:

vim

newline

I am working with gVim in Windows, and when I open files edited by others I see ^M instead of newlines. I have tried using %s/^M/\n/g to replace all instances of ^M with a newline, but I keep getting the error: Pattern not found: ^M. I have also tried %s/^M/\r/g, but I receive the same error.

There are still several instances of ^M in the file, why can't Vim identify them?

like image 656
Zach Wulf Avatar asked Apr 11 '13 02:04

Zach Wulf


People also ask

What is the M character in vim?

The file in question was created in Windows and then copied over to Linux. ^M is the keyboard equivalent to \r or CTRL-v + CTRL-m in vim.

What is new line character in vim?

Use \r instead of \n . Substituting by \n inserts a null character into the text. To get a newline, use \r . When searching for a newline, you'd still use \n , however.

What is M in bash?

^M are the meta characters which entered your file when it was used in windows. the dos2unix command can fix this. dos2unix <filename> Follow this answer to receive notifications.


1 Answers

Press CTRLV before pressing CTRLM in your substitute command.

This allows you to escape the next control sequence properly.

Here are a couple links I found by googling: vim remove control m:

  • http://www.tech-recipes.com/rx/150/remove-m-characters-at-end-of-lines-in-vi/
  • http://dailyvim.blogspot.com/2009/01/removing-ctrl-m.html
  • gVim showing carriage return (^M) even when file mode is explicitly DOS
like image 110
jahroy Avatar answered Sep 28 '22 15:09

jahroy