Skip to content

Customize Runner

Linwei edited this page Dec 17, 2021 · 56 revisions

AsyncRun allows you to define new runners to specify how to run your command. It can be useful when you want your commands run in a tmux split, a new gnome-terminal window, or a floaterm window.

Create a New Runner

A runner is a function with one argument opts as a dictionary, from which stores the command string, working directory, and other parameters passed with :AsyncRun command. All the runners are required to register in g:asyncrun_runner so that AsyncRun can recognize them.

function! s:my_runner(opts)
    echo "run: " . a:opts.cmd
endfunction

let g:asyncrun_runner = get(g:, 'asyncrun_runner', {})
let g:asyncrun_runner.test = function('s:my_runner')

Than use:

:AsyncRun -mode=term -pos=test ls -la $(VIM_FILEDIR)

When -mode=term and -pos=test are provided, runner test will be called. In this example, runner function s:my_runner does nothing but display the command in the bottom of your vim.

Real Example: tmux

It is very easy to make the command run in a tmux pane with vimux:

function! s:run_tmux(opts)
    " asyncrun has temporarily changed working directory for you
    " getcwd() in the runner function is the target directory defined in `-cwd=xxx`  
    let cwd = getcwd()   
    call VimuxRunCommand('cd ' . shellescape(cwd) . '; ' . a:opts.cmd)
endfunction

let g:asyncrun_runner = get(g:, 'asyncrun_runner', {})
let g:asyncrun_runner.tmux = function('s:run_tmux')

And you are able to use:

:AsyncRun -mode=term -pos=tmux ls -la

screenshot:

You can specify pane position (vertical or horizontal) and size, just check vimux's doc.

Clone this wiki locally