Skip to content

Instantly share code, notes, and snippets.

@qiemem
Created March 29, 2015 00:21
Show Gist options
  • Save qiemem/59721c86668f233a73ab to your computer and use it in GitHub Desktop.
Save qiemem/59721c86668f233a73ab to your computer and use it in GitHub Desktop.
Send text to buffer in vim
function! GetTextObject(type)
let saved_register = @@
if a:type == 'v'
normal! `<v`>y
elseif a:type ==# 'char' || a:type ==# 'line'
normal! `[v`]y
endif
let text = @@
let @@ = saved_register
return text
endfunction
function! SendDirection(type, direction, back)
execute "normal! \<c-w>" . a:direction . "i" . GetTextObject(a:type) . "\<c-\>\<c-n>\<c-w>" . a:back
endfunction
function! SendRightOp(type)
call SendDirection(a:type, 'l', 'h')
endfunction
function! SendLeftOp(type)
call SendDirection(a:type, 'h', 'l')
endfunction
function! SendUpOp(type)
call SendDirection(a:type, 'k', 'j')
endfunction
function! SendDownOp(type)
call SendDirection(a:type, 'j', 'k')
endfunction
nnoremap <Leader>sl :set operatorfunc=SendRightOp<CR>g@
vnoremap <Leader>sl :<C-U>call SendRightOp(visualmode())<CR>
nnoremap <Leader>sh :set operatorfunc=SendLeftOp<CR>g@
vnoremap <Leader>sh :<C-U>call SendLeftOp(visualmode())<CR>
nnoremap <Leader>sk :set operatorfunc=SendUpOp<CR>g@
vnoremap <Leader>sk :<C-U>call SendUpOp(visualmode())<CR>
nnoremap <Leader>sj :set operatorfunc=SendDownOp<CR>g@
vnoremap <Leader>sj :<C-U>call SendDownOp(visualmode())<CR>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment