how to make vim show ^M and substitute it

RegexVim

Regex Problem Overview


my vim show tab as --->, but does not show windows ^M character.

And , how substitute it in vim.

renew ============

I check my vimrc it is set fileformat=unix but when I open a dos file set ff is dos

Regex Solutions


Solution 1 - Regex

Display CRLF as ^M:

:e ++ff=unix

Substitute CRLF for LF:

:setlocal ff=unix
:w
:e

Solution 2 - Regex

Vim does show ^M except in one case: if the fileformat=dos then it will not show a trailing crlf.

You can find out which format (unix or dos) you have by typing :set and you can get rid of the ^M in the crlf by just changing the format (:set fileformat=unix) and then writing out the file.

If you have a ^M in the middle of the line, then you should be able to see it, even in a fileformat=dos file, and you can pattern match it with \r. (Oddly, the syntax for subsituting a newline is a \r in the replacement part of the sub, so the way one changes ^M to ^N is by the not-at-all-a-noop :s/\r/\r/.)

Solution 3 - Regex

vim is autodetecting the fileformat and switching modes to match (take a look at set ff)

If you want to force it to open in a particular mode, toss a +ff=unix(to show the ^M) or +ff=dos in your command line to open it in that mode. If you're on a windows box, just try :e ++ff=unix after opening the file.

If you're trying to just strip those characters out, you can open it in one mode, set the ff to what you want, and then save the file. Check out :h ff for more details.

Solution 4 - Regex

To remove ^M characters from you vim: In command mode type

%s/

followed by

ctrl+v and Enter. 

It should then look like :

%s/^M

Lastly replace with blank character :

%s/^M//g

Solution 5 - Regex

You can view all terminal linefeeds and carriage returns by enabling the list feature: :set list.

ou can type them literally into match and substitution commands with ^V: e.g. to convert all ^Ms to CR, you could do: :%s/^V^M/CR/g (type a literal ^V followed by a literal ^M).

Solution 6 - Regex

Old fashioned way - works even in vi:

    vim -b filename
    :%s/^V^M//g
    :x

Explanations:

  • vim -b opens vim in binary mode, all control characters will be visible
  • vi doesn't have -b option but then again vi doesn't need it, it will show ^M by default
  • :%s is search replace - we are replacing ^M with nothing
  • You have to type Control-V Control-M in a sequence, you can keep Control down all the time, or you can release it and press again - both works.
  • ^V will disappear, only ^M will stay. ^V means insert next character literally (try pressing Escape after ^V). Without it Control-M would just mean Carriage Return - or same as pressing Enter.
  • Don't try to copy ^M with a mouse, it will break it into two characters ^ and M and it will mean search for M at the beginning of the line.
  • g means global - if you have ^M only on end of line you don't really need it
  • :x means save and exit - almost no one is using it

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionguilin 桂林View Question on Stackoverflow
Solution 1 - RegexNoToRView Answer on Stackoverflow
Solution 2 - RegexDigitalRossView Answer on Stackoverflow
Solution 3 - RegexjkerianView Answer on Stackoverflow
Solution 4 - RegexAnonymousView Answer on Stackoverflow
Solution 5 - RegexEtherView Answer on Stackoverflow
Solution 6 - RegexblurView Answer on Stackoverflow