Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Customize Runner

Linwei edited this pageDec 30, 2021 ·56 revisions

User-defined runners provide you the possibility to run commands in any way you want, which could be helpful when you want a command run in atmux split, a newgnome-terminal window, or afloaterm window.

Create a New Runner

A runner is a function with one argumentopts 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 ing:asyncrun_runner so that AsyncRun can recognize them.

function!s:my_runner(opts)echo"run:" .a:opts.cmdendfunctionletg:asyncrun_runner=get(g:,'asyncrun_runner', {})letg:asyncrun_runner.test=function('s:my_runner')

Than use:

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

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

Get Information

There is aopts argument in the runner function, which contains necessary information:

FieldNeedDescriptionExample
cmd(required)Command string (macros have already been expanded here)ls -la
cwd(optional)Working directory (will be an empty string if not provided)/home/yourname/github
mode(optional)Running modeasync,terminal/term, orvim
pos(optional)Runner name or terminal positionTAB,gnome,tmux, ...
option(optional)Runner option passed by:AsyncRun -option=xxx ......
close(optional)Close terminal after job finished-close=1
post(optional)A vim script needs to be executed after finished-post=echo\ "done" ls -la
program(optional)Command modifier-program=grep
focus(optional)if set to zero, the runner will not get focused-focus=0
encoding(optional)Command encoding-encoding=gbk

If-cwd=xxx is provided after:AsyncRun command, AsyncRun will temporarily change thecurrent working directory to the target position when calling runner function. So, you caneither pick the value ina:opts.cwd or use the return value fromgetcwd() .

Range Mode

When use the:AsyncRun command in the range mode, additional information can be use:

FieldDescriptionExample
range> 0 for range mode enabled0
range_topfirst line of the range, where range starts100
range_botlast line of the range, where range ends105
range_bufbuffer id of the document1

Range support is not compulsory for runners, but it would be nice to provide this feature.

Real Example

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

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()callVimuxRunCommand('cd' .shellescape(cwd) .';' .a:opts.cmd)endfunctionletg:asyncrun_runner=get(g:,'asyncrun_runner', {})letg:asyncrun_runner.tmux=function('s:run_tmux')

And you are able to use:

:AsyncRun -mode=term -pos=tmuxls-la

screenshot:

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

Separated Script

Another way to create a runner is to create a separated.vim file in the folder:

autoload/asyncrun/runner/

In one of yourruntimepath, it will be automatically loaded in need. The script is required to provide arun function with the full name:

asyncrun#runner#{name}#run(opts)

For example, seegnome.vim in theautoload/asyncrun/runner folder:

function!asyncrun#runner#gnome#run(opts)if!executable('gnome-terminal')returnasyncrun#utils#errmsg('gnome-terminal executable not find !')endiflet cmds= []let cmds+= ['cd' .shellescape(getcwd()) ]let cmds+= [a:opts.cmd]let cmds+= ['echo ""']let cmds+= ['read -n1 -rsp "press any key to continue ..."']let text=shellescape(join(cmds,";"))letcommand='gnome-terminal -- bash -c' . textcallsystem(command .' &')endfunction

Try it with:

:AsyncRun -mode=term -pos=gnomels-la

Screenshot:

More Examples

In therunner folder, some pre-included runner script can be found:

RunnerDescriptionRequirementLink
gnomerun in a new gnome terminalGNOMEgnome.vim
gnome_tabrun in a new gnome terminal tabGNOMEgnome_tab.vim
xtermrun in a xterm windowxtermxterm.vim
tmuxrun in a separated tmux paneVimuxtmux.vim
floatermrun in a new floaterm windowfloatermfloaterm.vim
floaterm_reuserun in a reusable floaterm windowfloatermfloaterm_reuse.vim
quickuirun in a quickui windowvim-quickuiquickui.vim
toggletermrun in a toggleterm windowtoggleterm.nvimtoggleterm.vim
xfcerun in a new xfce terminalxfce4-terminalxfce.vim
konsolerun in a new konsole terminalKDEkonsole.vim
macosrun in a macOS system terminalmacOSmacos.vim
itermrun in a new iTerm2 tabmacOS + iTerm2iterm.vim

They can be a good reference if you want to create a new runner.

Clone this wiki locally

[8]ページ先頭

©2009-2025 Movatter.jp