1

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.

2

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
  new

  " 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"))
  else
    call setline(1, a:var)
  endif

  " go back to original window
  wincmd p
endfunction

:h setline()
:h :wincmd


examples:

:ShowVariable @@
:ShowVariable string_variable_name
:ShowVariable list_variable_name
| improve this answer | |
  • thanks for an answer that goes above and beyond. :) – masukomi Dec 23 '19 at 14:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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