vim null symbol effecting string ^@

Viewed 32

I am using the following function to get the Selected test

let s:drawscript = "somerandom.py"

func! GetSelectedText()
  normal gv"xy
  let result = getreg("x")
  normal gv
  return result
endfunc

vnoremap <tab><tab> :<c-u>call Box(GetSelectedText())<CR>
func! Box(text)
    let s:b = '"' . a:text . '"'
    echom s:b
    " exec boxcmd
    "echom 'hi'
    let c = ["python3", s:drawscript, s:b]
    execute ":.!".join(c, " ")
endfunc

I am trying to pass in the text selected to my python file, it works when I only select 1 line, but when I select multiple lines, there are "^@" symbols in the selected text which caused automatic execution which leads to an error. I just wanna pass in the text I have selected into the .py file.

text

error

1 Answers

It's a matter of escaping special characters for the shell. While you correctly thought of quoting the text, you missed to escape the line separator. There's the function shellescape() which takes care of this and more, so you can replace

    let s:b = '"' . a:text . '"'

by

    let s:b = shellescape(a:text, 1)
Related