^M in yank but not in file in Vim

I am trying to visually select a section of text in vim and then substitute for it using the :%s/ sequence.

First I visually select text. Then I yank using "ay. Then I type :%s/ followed by Control R and the register name, in this case a.

This fills in the text I have visually selected into the search space, but it ADDS ^M characters at the end of lines.

When I then attempt to substitute for this text, I get a "pattern not found error".

How can I avoid these ^M's.

They are NOT repeat NOT in the file.

Thanks

Chances are they are in the file. When you start vim with the file, look at the status line (at the bottom). If there's something like this

"test.txt" [dos]

it means that the file is in DOS format with carriage return/line feed line endings. vim can correctly interpret them, and if you yank a line the line endings go with it. But for the buffer vim doesn't know it's in DOS format, and shows the carriage return as the corresponding control character: ^M (one character, not 2).

By the way, if you want to apply a substitution to a small part of the file, select it (using v or V), and, without pressing ESC/y/x/d/... enter

:s/

The command line should then look like this:

:'<,'>s/

which means that the command will only be applied to the selected text.

Just want to add that you can also do

:set list

to show all special characters in the file you are editing.

To turn it off you can use

:set nolist

The file is a Unix file. No question about it.

When I execute :set list, each line is terminated by a red dollar sign ($). No ^M's in sight.

The file was created in the following way. It is text from an article in a medical journal that I highlighted and copied from Firefox. I then pasted it into an empty Vim buffer and saved it.

I am sure there is something very simple I am missing/screwing up. Please forgive my newbie-ness.

Thanks

Do you use vim/gvim on windows as editor when doing this or via a terminal session, let's say putty? I guess it is either a setting in gvim or putty if you are sure the file is clear of ^M.

What does the "od" command return, when run on this file ? If you really have DOS end-of-line character sequences, then you'd see "\r\n" in the output, as seen below:

$
$ cat t2
first line
second line
third line
$
$ od -bc t2
0000000 146 151 162 163 164 040 154 151 156 145 015 012 163 145 143 157
          f   i   r   s   t       l   i   n   e  \r  \n   s   e   c   o
0000020 156 144 040 154 151 156 145 015 012 164 150 151 162 144 040 154
          n   d       l   i   n   e  \r  \n   t   h   i   r   d       l
0000040 151 156 145 015 012
          i   n   e  \r  \n
0000045
$
$

If your file has the Unix end-of-line character, then the output will show "\n" :

$
$ od -bc t2
0000000 146 151 162 163 164 040 154 151 156 145 012 163 145 143 157 156
          f   i   r   s   t       l   i   n   e  \n   s   e   c   o   n
0000020 144 040 154 151 156 145 012 164 150 151 162 144 040 154 151 156
          d       l   i   n   e  \n   t   h   i   r   d       l   i   n
0000040 145 012
          e  \n
0000042
$
$

If your file is huge, you may want to run that command on only a couple of lines at the beginning:

head -2 your_filename | od -bc

tyler_durden

It's \n, and thank you for the tip on seeing the codes and the "letters".

I'm using gvim, but really MacVim. It is perhaps somewhere in there. But I don't know where.

Thank you everyone. Further tips will be gratefully accepted.