Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I use Vim 7.3 under Windows 7 and have experienced a problem. Symbols ^M are not disappearing despite any my efforts, :e ++ff=dos does not help.

Any suggestions?

share|improve this question

2 Answers 2

^M usually appear when the file is inconsistent with regards to the line terminators used. Try the following:

  • Create testfile using vim with a few random lines, and write it in dos mode.
  • Then run (hope you have cygwin installed):

     sed '2s/.$//' testfile > corruptfile
    

    This will remove the last character of the second line, creating an inconsistency in the line terminators used.

  • Open corruptfile with vim. ^M symbols will appear so that you are made aware of the inconsistency.

In real life, programs that have been written with a single type of line-terminator in mind may produce such inconsistencies. While these inconsistencies seem innocent, they may cause you problems with other programs. E.g. subversion does not allow file with inconsistent line terminators to be added to a repository. Other programs may just fail silently.

To make ^M dissapear just make a global replacement:

:% s/^M//g

The ^M is produced by pressing: Ctrl+v <Enter>

Then write back the file in the desired format:

:set ff=dos
:w
share|improve this answer
    
I know this trick and it does work. But why these ^M symbols happen in files created on Windows (not in Vim) while accessing from Windows? I met this many time and do not understand the logick. –  OlegG Sep 13 '12 at 11:34
    
The problem exists for file completion in command line, not in editor filed. For example, when I write e: filenam<tab> and awaiting that Vim completes typed filenam, then Vim doesn't complete. –  OlegG Sep 13 '12 at 11:59
    
I added an explanation for ^M appearing. Could you elaborate more on which command line you mean? Is it windows shell (cmd)? Bash shell under cygwin? Something else? –  m000 Sep 13 '12 at 12:05
    
It is windows shell (cmd). I have not cygwin (mingw instead). And regarding ^M: files were created in Code::Blocks and then I tried to edit them in Vim. I suggested that ^M are appeared due to presense of a several non-CRLF-lines. But did not found such lines. –  OlegG Sep 13 '12 at 12:09
    
If part 1 regards windows shell I strongly recommend splitting this question in two separate questions. Have you checked e.g. with hexdump -c that all lines in your file use the same line terminator? A oneliner for this would be sed -e 's/[[:print:]]*//g' file | hexdump -c. With a consistent file, the same pattern of line terminators characters will be repeated on the output. (sorry for the unix-slang :-) ) –  m000 Sep 13 '12 at 12:38

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 open a text file created on a Windows computer on a Linux box, you may see trailing CR characters in each line. There are several ways to remove them. One is to replace them in vim as m000 suggested, another would be to recode the file:

recode ibmpc..latin1 SOME.TXT
share|improve this answer
    
If you'll read my question more accureate then you will see that I use Vim under Windows to edit files created under Windows also. –  OlegG Sep 13 '12 at 11:56
1  
It can happen on Windows as well if at least one line uses only LF without CR. See this answer to a similar question on Stack Overflow. –  Ansgar Wiechers Sep 13 '12 at 12:03
    
"If you open a text file created on a Windows computer on a Linux box, you'll see trailing...": This is only true for b-class editors who can't handle non-native line terminators properly. Definitely not true for vim. –  m000 Sep 13 '12 at 12:07
    
@m000 I changed that to "may see". And even vim will display CR when at least one line uses only LF, see my other comment above. –  Ansgar Wiechers Sep 13 '12 at 12:37
2  
@AnsgarWiechers I.e. vim will display line terminators when they are not consistent through the file :-) –  m000 Sep 13 '12 at 12:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.