I'm trying to write a plugin:

I have a variable with some text. I want to:

  1. create a new buffer
  2. place the contents of that variable in the new buffer
  3. open a new split that displays the new buffer
  4. keep focus on the current window (not the new one)

I'm having a really hard time googling for 2 and 3 and I'm not 100% sure I'm doing 1 correctly either. I'm assuming 4 isn't hard.

1 Answer 1


Works for register, String, and List:

" don't use <q-args> here, we need expression
com -nargs=1 ShowVariable call s:show_var(<args>)

function s:show_var(var) abort

  " split new buffer

  " set buffer content
  if type(a:var) == v:t_string

    " split string into list, otherwise you see ^@ as newline
    call setline(1, split(a:var, "\n"))
    call setline(1, a:var)

  " go back to original window
  wincmd p

:h setline()
:h :wincmd


:ShowVariable @@
:ShowVariable string_variable_name
:ShowVariable list_variable_name
  • thanks for an answer that goes above and beyond. :)
    – masukomi
    Dec 23, 2019 at 14:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.