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:
- create a test.php/test.py or test.js with vim.
- launch a terminal, then run
php test.php
,python test.py
,node test.js
. - back to vim, make a change and save it, then back to terminal again, run
php test.php
etc. - 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
Basic idea of k.vim
The very basic idea of the plugin is to do the following tasks without leaving vim:
- call external command
- show command output in a vim scratch window
My other two usages of the plugin:
<leader><leader>
to translate english word in vim with kv as you can see in the screencast above.- convert vim into a quick launcher.
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.
- get and build kv, for Windows you can get pre-built binary here.
- copy kv or kv.exe to a folder in your PATH such as /usr/local/bin or C:\Windows\system32
-
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>'], \ }