0

Setting: Vim session with multiple tabs, windows, and buffers.

Goal: Make Ex command that sends selected text from active buffer to another buffer in the session (preferably with autocompletion). By this I mean the text is appended to the end of the target buffer.

Example: Suppose you're in a buffer named apple and there is another buffer named banana in a different tab. You highlight the text you want to send to banana (say, a line that has text "asdf", and then type :'<,'>append ban<TAB> and vim autocompletes the text to :append banana. After pressing return, vim cuts the string "asdf" from apple and appends it to the end of banana.

The closest I've come is :'<,'>!cat >> banana; however, this only works if banana is in the same working directory as apple, and it fails to autocomplete with existing buffer names.

Is there a way to construct a vim script/viml function that does this?

George
  • 6,927
  • 4
  • 34
  • 67

1 Answers1

2

Something like this?

function! Append(l1, l2, buffer)
  let currentBuffer = @%
  let currentRegister = @z
  execute a:l1 . "," . a:l2 . 'y z'
  execute "buffer " . a:buffer
  normal G"zp<C-O>
  let @z = currentRegister
  execute "buffer " . currentBuffer
endfunction

command! -nargs=1 -complete=buffer -range Append call Append(<line1>, <line2>, <f-args>)

We remember the current buffer and the current value of the z register, as we'll use that for yanking; we yank, then switch buffers, go to bottom and paste; then put everything back the way it was. -nargs=1 -complete=buffer gives you autocomplete for buffer names.

Amadan
  • 191,408
  • 23
  • 240
  • 301