how to make vim show ^M and substitute it
my vim show tab as --->, but does not show 开发者_如何学JAVAwindows ^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
Display CRLF as ^M:
:e ++ff=unix
Substitute CRLF for LF:
:setlocal ff=unix
:w
:e
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/
.)
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.
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
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
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 ^M
s to CR
, you could do: :%s/^V^M/CR/g
(type a literal ^V followed by a literal ^M).
精彩评论