Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would "\n" become "^@" when writing Python in a .vim file?

For example, here is my function:

function! Test()
python << EOF
import vim
str = "\n\n"
vim.command("let rs = append(line('$'), '%s')"%str)
EOF
endfunction

and when I :call Test() , what I see is "^@^@".
Why would this happen and how can I use the origin '\n' ?

like image 885
yakiang Avatar asked May 28 '13 11:05

yakiang


People also ask

Does vim add newline at end of file?

Vim doesn't show latest newline in the buffer but actually vim always place EOL at the end of the file when you write it, because it standard for text files in Unix systems. You can find more information about this here. In short you don't have to worry about the absence a new lines at the end of the file in vim.

What does m mean vim?

0xD is the carriage return character. ^M happens to be the way vim displays 0xD (0x0D = 13, M is the 13th letter in the English alphabet). You can remove all the ^M characters by running the following: :%s/^M//g.

How to remove m in gvim?

To remove the ^M : See File format – Vim Tips Wiki. Show activity on this post. To get the ctrl-M I usually type ctrl-Q, then ctrl-M and it puts it in. (In some environments it may be ctrl-V then ctrl-M.)


1 Answers

Two things: Vim internally stores null bytes (i.e. CTRL-@) as <NL> == CTRL-J for implementation reasons (the text is stored as C strings, which are null-terminated).

Additionally, the append() function only inserts multiple lines when passed a List of text lines as its second argument. A single String will be inserted as one line, and (because of the translation), the newlines will appear as CTRL-@.

Therefore, you need to pass a List, either by building a Python List, or by using the split() Vim function to turn your single String into a List:

function! Test()
python << EOF
import vim
str = "\n"
vim.command("let rs = append(line('$'), split('%s', '\\n', 1))"%str)
EOF
endfunction
like image 198
Ingo Karkat Avatar answered Nov 14 '22 22:11

Ingo Karkat