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' ?
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.
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.
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.)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With