Call external command and show result in a scratch window

We usually need write a piece of snippet to test usage of some functions with some kind of script.

The common way to do:

  1. create a test.php/test.py or test.js with vim.
  2. launch a terminal, then run php test.php, python test.py, node test.js.
  3. back to vim, make a change and save it, then back to terminal again, run php test.php etc.
  4. again and again if you are not lucky enough.

Now it comes to k.vim

After vim launched,

:Ft pyt<tab>

or

:set filetype=python

Write your code snippet, press <leader>r anytime to run the snippet you have input, you even need not save the snippet to a disk file.

The output of your script will be displayed in a scratch window(:help special-buffers), as the below line indicates:

autocmd FileType python     nnoremap <buffer> <leader>r :call k#RunMe('python', 'botri 10', "")<CR>

Press <space><leader> to close the related scratch window of current buffer.

nnoremap <silent> <space><leader> :call k#CloseConsole()<CR>

Demo screencast

k.vim

Basic idea of k.vim

The very basic idea of the plugin is to do the following tasks without leaving vim:

My other two usages of the plugin:

use vim as quick launcher for some frequent commands

Create a shell script, for example, mycmds.sh, with its content as below

curl -s https://api.github.com/gists/public
curl -sk https://api.douban.com/v2/book/search?q=algorithm|python -mjson.tool
...

with each whole command in one line, whenever I need run a command, jump to the line, press <Enter>, the response will be displayed when it’s done.

For Windows, I also have a file named cli.cmd created, all my frequent commands are in it.

If you’re also an user of ctrlp.vim, please check out Use VIM as a quick launcher

Other commands

:Ri <command>           run vim command, read output into current buffer, for example: Ri version。
:Rc <command>           run vim command, read output into a scratch window, for example: Rc verbose map。

:Ri !<command>          run external command, read output into current buffer, for example: Ri !ls。
:Rc !<command>          run external command, read output into a scratch window, for example: Rc !dir。

Installation

Bundle 'brookhong/k.vim'

or put k.vim under .vim/plugin.

If you need the feature to translate word in vim, you need follow below instructions to install kv.

  1. get and build kv, for Windows you can get pre-built binary here.
  2. copy kv or kv.exe to a folder in your PATH such as /usr/local/bin or C:\Windows\system32
  3. build or download dictionaries you need to some folder, and tell VIM where it is

     " the plugin will scan this directory to create key mapping globally or specially for some type of file.
     let g:kdbDir = $HOME.'/kdb'
     " when press `<leader>,`, will translate word under the cursor, works for all kinds of files.
     let g:globalDBkeys = {
           \ 'oxford' : '<leader>,',
           \ }
     " when press `K` in normal mode or `C-j` in insert mode, will get referrence of function under the cursor, works for php file or C file.
     " for file-type dictionaries, the folder must be named same as the file type.
     let g:localDBkeys = {
           \ 'php' : ['K', '<C-j>'],
           \ 'c' : ['K', '<C-j>'],
           \ }