Why does "\ 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 does this happen and how can I use the start of '\ n'?

+6
source share
1 answer

Two things: Vim internally stores empty bytes (ie CTRL-@ ) as <NL> == CTRL-J for implementation reasons (the text is saved as C lines that end with zero).

In addition, the append() function only inserts a few lines when a list of text lines is passed as the second argument. One line will be inserted as one line, and (due to translation) newline characters will be displayed as CTRL-@ .

So you need to pass the list by creating a Python list or using the split() Vim function to turn your single line into a list:

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

Source: https://habr.com/ru/post/946004/


All Articles