Working with Postgres and R recently, I had found some solution for getting my stuff from my VIM to their respective REPLs. That’s how I came up with this solution.
After searching around for a bit, I found :ter in the VIM 8.1 release notes. :ter was exactly what I wanted. With :ter psql -h ... you open up a window with the Postgres terminal in it. So you can either go around copy pasting your command-lines, or you can simply write a command for it.
Here’s the minimal version I’ve been using for the past few weeks:
nmap <silent> <leader>s :set opfunc=ExecuteAsCommand<CR>g@
let s:term_buffer = ''
function ExecuteAsCommand(type, ...)
silent exe "normal '[\"cy']"
echo s:term_buffer
" clear the screen (and quit less if its open)
call term_sendkeys(s:term_buffer, "q")
call term_sendkeys(s:term_buffer, "\<C-c>")
call term_sendkeys(s:term_buffer, "\<C-l>")
call term_sendkeys(s:term_buffer, getreg('c'))
endfunction
command! -nargs=1 Run call RunTerminalCommand(<f-args>)<CR>
function RunTerminalCommand(command)
execute 'ter ' . a:command
let s:term_buffer = a:command
endfunction
After having loaded this, :Run psql -h ... opens up a terminal window with psql. And now entering <leader>s{motion}2 sends the text you moved over to your terminal.
This little snipped is my favourite thing I’ve discovered in VIM in quite while. It’s now a central part of my number-crunching workflow. I’m going to write a post about that soon as well.
-
In psql
\escratched the VIM itch somewhat. You can edit your last command in your$EDITOR. ↩︎ -
The snippet above only works for whole lines selected with a motion. I try to use visual-mode as little as possible. If you’d like to add support for visual mode, that’s easy to do. Have a look at
:help :map-operator. ↩︎