Visit Our UNIX and Linux User Community


^M in yank but not in file in Vim


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ^M in yank but not in file in Vim
# 1  
Old 10-22-2009
^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
# 2  
Old 10-22-2009
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
Code:
"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
Code:
:s/

The command line should then look like this:
Code:
:'<,'>s/

which means that the command will only be applied to the selected text.
# 3  
Old 10-22-2009
Just want to add that you can also do
Code:
:set list

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

To turn it off you can use
Code:
:set nolist

# 4  
Old 10-22-2009
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
# 5  
Old 10-22-2009
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.
# 6  
Old 10-22-2009
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:

Code:
$
$ 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" :

Code:
$
$ 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:

Code:
head -2 your_filename | od -bc

tyler_durden
# 7  
Old 10-22-2009
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.

Previous Thread | Next Thread
Test Your Knowledge in Computers #56
Difficulty: Easy
A flip-flop circuit stores 1 bit of data.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Cut first column from file in Vi(Vim)

I have a parts file that looks like this: EE36264|0NH46||Y|A|EA|0|0|0|N|LUNETTE 3" ADJ. EYE|0|0|*|0|PEOZZU|N|12|N|N|VPS|N|N|N|N|LUNETTE 3" ADJ. EYE|0|||Receive into Inventory|81755|EE36264|*|*|*|0|0||EE36264|A|*|* F1.5|53932||Y|A|EA|0|0|0|N|FLAT ZERO CAL... (4 Replies)
Discussion started by: djehresmann
4 Replies

2. Red Hat

How to set password for file using vim in Linux/redhat?

Hello All, I have one query is "How to set the password for file using vi utility in linux. Please reply to my queries. I am waiting for reply. Thanks in advance. Thanks. Kuddus Shaikh (4 Replies)
Discussion started by: kuddusrhce
4 Replies

3. Shell Programming and Scripting

Help with update vim file-syntax relationship?

There are some ksh files named as "*.lib" in my system. When I open them in VIM, vim syntax can't parse it correctly. Is there a way that I can relate the *.lib with KSH syntax in VIM? Due to access limitation, i can only update the files under my home directory and can't modify the VIM... (6 Replies)
Discussion started by: sleepy_11
6 Replies

4. UNIX for Dummies Questions & Answers

VIM E212: Cannot save a new file

Hey everyone, I'm new to Linux and am attempting to run a TF2 server on a CentOS VPS (terminal only). I followed this guide (link below) and have arrived on step 4, creating a configuration file. When I try to save the new file however, I get the E212: Can't open file for writing error.... (0 Replies)
Discussion started by: rGSyn
0 Replies

5. UNIX for Dummies Questions & Answers

Yank versus control-insert formatting differences?

Hello! Graduate student thrown to the sharks of unix and fortran77 here. My basic question is this: what exactly is the difference between yank (ie "*yG for the whole file and "*p in a new file) and control-insert / shift-insert with respect to text formatting? One is vi and one is Windows,... (2 Replies)
Discussion started by: marshlight
2 Replies

6. Solaris

Very Importan - Vim Settings - Error while opening a File using vim

I downloaded vim.7.2 and compiled the vim source . Added the vim binary path to PATH (Because iam not the root of the box) when i load the file using vim it throws me an error Error detected while processing /home2/e3003091/.vimrc: line 2: E185: Cannot find color scheme darkblue line... (0 Replies)
Discussion started by: girija
0 Replies

7. Shell Programming and Scripting

Continuously display the file name in vim

Hi All, In vim, when we press ^G, the file name is displayed at the bottom of the screen and as soon as we enter into some other mode (say e.g. Insert), the file name is replaced by the words "Insert". I was just wondering whether it is possible to continuously show the file name no matter which... (3 Replies)
Discussion started by: jal_capri
3 Replies

8. UNIX for Dummies Questions & Answers

Yank a column in vi

I have a file which has data in columns. Is there a way to yank columns in vi? I tried searching in this forum. I did not find it. Please help me out. (2 Replies)
Discussion started by: stevelrf
2 Replies

9. UNIX for Dummies Questions & Answers

annoying vi yank word + delete all question

Hello when i try to yank word only that looks like this "$$foo$" when i stand with my curser marker on the first char ($) and do in vi : "yw" (yank word) its yanks me only the "$" char when i stand white my curser on "f" its yank's me only "foo" how can i yank all word no matter what... (4 Replies)
Discussion started by: umen
4 Replies

10. UNIX for Dummies Questions & Answers

yank

This is supposed to be simple to do but I am having a hard time trying to yank 1 line (yy) but creating from it 100 lines. I can do yy then keep hitting . to repeat but it doesn't make sense. I may want to create 1000 lines from the one liner. yy then 99 p doesn't work.... Thanks. I was... (5 Replies)
Discussion started by: giannicello
5 Replies

Featured Tech Videos