Dealing with \r \n ^M ^@ in text files using vim

2019-05-15 04:41发布

When I save lines of data in excel files as tab delimited .txt files, and then open those files in VIM, I see that what was once a multi-line file in excel is now a single line file in VIM.

The "lines" can be separated in VIM using some substitution commands:

%s/^M/\r\n/g

After this, the "lines" are now separated by an ^@. I deal with it using another substitution command:

%s/^@//g

My questions are:

  1. Why do my multi-line txt excel files open as a single line in VI?
  2. What is ^@?
  3. Is there a better way to 'fix' my txt files?

2条回答
闹够了就滚
2楼-- · 2019-05-15 05:05

Try this command:

    :%s/^M/\r/g

\r is the carriage return character vim uses. The ^M character is a newline character that is literally displayed.

查看更多
何必那么认真
3楼-- · 2019-05-15 05:24

You can often fix problems like this by running the following command:

dos2unix FILE_NAME

This will re-format the file's newline characters in place (ie it will modify your file).

(This command does not exist on Mac OS X)


If you don't have dos2unix (eg you're on a Mac), you can just use sed:

sed 's/^M$//' input.txt > output.txt

You can also use sed -i if you want to avoid creating a new file by performing the substitution in place.

You can enter ^M by typing CTRLV followed by CTRLM

More reading here

查看更多
登录 后发表回答