6

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' ?

yakiang
  • 1,608
  • 1
  • 16
  • 17

1 Answers1

9

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
Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324