I am trying to create a simple vim plugin that translates the word or phrase that is selected in visual mode. I have the plugin working for the most part, with one annoying exception, the translated string is always pasted on the next line from where the selection was made. I've come to understand that is is because the string was not created in a "word-wise" fashion.
My question is how do I create a string in vimscript that contains word-wise data, or alternately, how do I paste a regular string in the middle of a line?
I think I solved the first part of the question. Vim encodes new lines as NULL characters when the lines are yanked. The strtrans() function can be used to convert the NULL characters into ^@ codes in the register. Then the substitute() function can be invoked to search for and replace the null characters with spaces. Once the trailing NULL character is removed, the register can be pasted inline with p, just as if it was yanked with yw.
So now my question becomes how do I substitute only the last ^@ in the string?
My function currently looks like:
function! s:BingTranslate(...)
let s:query = a:000
let outp = ""
"call sub translator
let outp = s:NodeJSTranslate(s:query)
"replace with translation
let @x = outp
"remove null characters
let @x=substitute(strtrans(@x),'.*\zs^@',' ','g')
"re-select area and delete
normal gvd
"paste new string value back in
normal "xp
return outp
endfunction