Home

Awesome

Preface

This plugin takes the advantage of new APIs in Vim 8 (and NeoVim) to enable you to run shell commands in the background and read the output in the quickfix window in realtime:

If that doesn't excite you, then perhaps this GIF screen capture below will change your mind.

README in Chinese | 中文文档

News

Install

Install with vim-plug:

Plug 'skywind3000/asyncrun.vim'

Example

Remember to open vim's quickfix window by :copen (or setting g:asyncrun_open) before invoking AsyncRun, otherwise, you will not see any output.

Contents

<!-- TOC --> <!-- /TOC -->

Tutorials

Async run gcc to compile current file

:AsyncRun gcc "$(VIM_FILEPATH)" -o "$(VIM_FILEDIR)/$(VIM_FILENOEXT)"
:AsyncRun g++ -O3 "$(VIM_FILEPATH)" -o "$(VIM_FILEDIR)/$(VIM_FILENOEXT)" -lpthread 

This command will run gcc in the background and output to the quickfix window in real time. Macro '$(VIM_FILEPATH)' stands for filename with full path and '$(VIM_FILENOEXT)' represents filename without extension.

Async run make

:AsyncRun make
:AsyncRun make -f makefile

Remember to open the quickfix window by :copen before using the AsyncRun command, if you don't open it, you will not see any output.

Grep key word

:AsyncRun! grep -R -n word . 
:AsyncRun! grep -R -n <cword> . 

when ! is included, auto-scroll in quickfix will be disabled. <cword> represents current word under cursor.

Compile go project

:AsyncRun go build "$(VIM_FILEDIR)"

Macro '$(VIM_FILEDIR)' stands for the current file dir.

Lookup man page

:AsyncRun! man -S 3:2:1 <cword>

Git push

:AsyncRun git push origin master

Git push from project root

:AsyncRun -cwd=<root> git push origin master

Use -cwd=? to specify the working directory, macro <root> or $(VIM_ROOT) represents current Project Root.

Setup <F7> to compile file

:noremap <F7> :AsyncRun gcc "$(VIM_FILEPATH)" -o "$(VIM_FILEDIR)/$(VIM_FILENAME)" <cr> 

File name may contain spaces, therefore, it's safe to quote them.

Run a python script

:AsyncRun -cwd=$(VIM_FILEDIR) python "$(VIM_FILEPATH)"

New option -raw will display the raw output (without matching to errorformat). Remember to put let $PYTHONUNBUFFERED=1 in your .vimrc to disable python stdout buffering, see here.

Run a python script in a new terminal

:AsyncRun -cwd=$(VIM_FILEDIR) -mode=term -pos=TAB  python "$(VIM_FILEPATH)"

This will run python in the internal-terminal (vim 8.2 or nvim-0.4.0 is required) in a new tabpage.

A good assistant to asyncrun

asynctasks.vim a plugin built upon asyncrun, an easy way to use asyncrun. It allows you to manage your building, testing, and deploying tasks in a global or project local configuration, and run them by their names.

Manual

There are two vim commands: :AsyncRun and :AsyncStop to control async jobs.

AsyncRun - Run shell command

:AsyncRun[!] [options] {cmd} ...

Run shell command in the background and output to quickfix. when ! is included, auto-scroll in quickfix will be disabled. Parameters are split by space, if a parameter contains space, it should be quoted or escaped as backslash + space (Unix only).

Macro variables in the parameters will be expanded before executing:

$(VIM_FILEPATH)  - File name of current buffer with full path
$(VIM_FILENAME)  - File name of current buffer without path
$(VIM_FILEDIR)   - Full path of current buffer without the file name
$(VIM_FILEEXT)   - File extension of current buffer
$(VIM_FILENOEXT) - File name of current buffer without path and extension
$(VIM_PATHNOEXT) - Current file name with full path but without extension
$(VIM_CWD)       - Current directory
$(VIM_RELDIR)    - File path relativize to current directory
$(VIM_RELNAME)   - File name relativize to current directory 
$(VIM_ROOT)      - Project root directory
$(VIM_CWORD)     - Current word under cursor
$(VIM_CFILE)     - Current filename under cursor
$(VIM_GUI)       - Is running under gui ?
$(VIM_VERSION)   - Value of v:version
$(VIM_COLUMNS)   - How many columns in vim's screen
$(VIM_LINES)     - How many lines in vim's screen
$(VIM_SVRNAME)   - Value of v:servername for +clientserver usage
$(VIM_PRONAME)   - Name of current project root directory
$(VIM_DIRNAME)   - Name of current directory

Environment variables with same name, like $VIM_FILENAME, are also initialized. Thus your child process can access them with getenv(xxx) at any time.

Some macros variables have their short names starting with '<' :

<cwd>   - Current directory
<cword> - Current word under cursor
<cfile> - Current file name under cursor
<root>  - Project root directory

They are also acceptable. So, you can use both $(VIM_ROOT) or its alias <root> to represent Project Root of the current file. Macro variables can be quoted with "..." in the command string when file name contains spaces (like normal shell command escaping), but they should not be quoted in the -cwd=? option.

There can be some options before your [cmd]:

OptionDefaultDescription
-mode=?"async"specify how to run the command as -mode=?, available modes are "async" (default), "bang" (with ! command) and "terminal" (in internal terminal), see running modes for details.
-cwd=?unsetinitial directory (use current directory if unset), for example use -cwd=<root> to run commands in project root directory, or -cwd=$(VIM_FILEDIR) to run commands in current buffer's parent directory.
-save=?0use -save=1 to save current file, -save=2 to save all modified files before executing.
-program=?unsetset to make to use &makeprg, grep to use &grepprt and wsl to execute commands in WSL (windows 10), see command modifiers.
-post=?unsetvimscript to exec after job finished, spaces must be escaped to '\ '
-auto=?unsetevent name to trigger QuickFixCmdPre/QuickFixCmdPost [name] autocmd.
-rawunsetuse raw output if present, and &errorformat will be ignored.
-stripunsetremove the heading/trailing messages if it is present (omit command and "[Finished in ...]" message).
-errorformat=?unseterrorformat for error matching, if it is unprovided, use current &errorformat value. Beware that % needs to be escaped into \%.
-silentunsetprovide -silent to prevent open quickfix window (will override g:asyncrun_open temporarily)
-scroll=?unsetset to 0 to prevent quickfix auto-scrolling
-onceunsetprovide to buffer all output and flush when job is finished, useful when there are multi-line patterns in your errorformat
-encoding=?unsetspecify command encoding independently (overshadow g:asyncrun_encs)
-pos=?"bottom"When using internal terminal with -mode=term, -pos is used to specify where to split the terminal window, it can be one of "tab", "curwin", "top", "bottom", "left", "right" and "external". And you can customize new runners and pass runner's name to -pos option.
-rows=num0When using a horizontal split terminal, this value represents the height of terminal window.
-cols=num0When using a vertical split terminal, this value represents the width of terminal window.
-focus=?1set to 0 to prevent focus changing when -mode=term
-hidden=?0set to 1 to init bufhidden to hide for internal terminal, set to 0 to init bufhidden to wipe
-listed=?1when using -mode=term, set to 0 to hide the terminal in the buffer list
-closeunsetwhen using -mode=term, close the terminal automatically when terminal process is finished

For the full list of the options, please see the Command Specification.

All options must start with a minus and position before [cmd]. Since no shell command string starting with a minus. So they can be distinguished from shell command easily without any ambiguity.

Don't worry if you do have a shell command starting with '-', Just put a placeholder @ before your command to tell asyncrun explicitly: "stop parsing options now, the following string is all my command".

AsyncStop - Stop the running job

:AsyncStop[!]

stop the running job, when "!" is included, job will be stopped by signal KILL.

Function (API)

Function form is convenient for vimscript:

:call asyncrun#run(bang, opts, command)

parameters:

Settings

For more information of above options, please visit option details.

Variables

Autocmd

autocmd User AsyncRunPre   - triggered before executing
autocmd User AsyncRunStart - triggered after starting successfully
autocmd User AsyncRunStop  - triggered when job finished

Note, AsyncRunPre is always likely to be invoked, but AsyncRunStart and AsyncRunStop will only be invoked if the job starts successfully.

When the previous job is still running or vim job slot is full, AsyncRun may fail. In this circumstance, AsyncRunPre will be invoked but AsyncRunStart and AsyncRunStop will have no chance to trigger.

Project Root

Vim is lack of project management, as files usually belong to projects, you can do nothing to the project if you don't have any information about where the project locates. Inspired by CtrlP, this feature (new in version 1.3.12) is very useful when you've something to do with the whole project.

Macro <root> or $(VIM_ROOT) in the command line or in the -cwd option will be expanded as the Project Root Directory of the current file:

:AsyncRun make
:AsyncRun -cwd=<root> make

The first make will run in the vim's current directory (which :pwd returns), while the second one will run in the project root directory of current file. This feature is very useful when you have something (make / grep) to do with the whole project.

The project root is the nearest ancestor directory of the current file which contains one of these directories or files: .svn, .git, .hg, .root or .project. If none of the parent directories contains these root markers, the directory of the current file is used as the project root. The root markers can also be configurated, see Project Root.

Running Modes

The default behavior is to run async command and output to quickfix window. However there is a -mode=? option can allow you specify how to run your command:

modedescription
asyncdefault behavior, run async command and output to quickfix window
bangsame as !
termopen a reusable internal terminal window and run your command

For more information, please see here.

Internal Terminal

AsyncRun is capable to run commands in Vim/NeoVim's internal terminal with the -mode=term option. You can specify how to open the terminal window by -pos=?, available positions are:

Examples:

:AsyncRun -mode=term -pos=tab python "$(VIM_FILEPATH)"
:AsyncRun -mode=term -pos=TAB -close -cwd=<root> lazygit
:AsyncRun -mode=term -pos=bottom -rows=10 python "$(VIM_FILEPATH)"
:AsyncRun -mode=term -pos=right -cols=80 python "$(VIM_FILEPATH)"
:AsyncRun -mode=term -pos=curwin python "$(VIM_FILEPATH)"
:AsyncRun -mode=term -pos=curwin -hidden python "$(VIM_FILEPATH)"

Internal terminal related options:

OptionDefaultDescription
-pos=?"bottom"When using internal terminal with -mode=term, -pos is used to specify where to split the terminal window, it can be one of "tab", "curwin", "top", "bottom", "left", "right" and "external".
-rows=num0When using a horizontal split terminal, this value represents the height of terminal window.
-cols=num0When using a vertical split terminal, this value represents the width of terminal window.
-focus=?1set to 0 to prevent focus changing when -mode=term
-closeunsetwhen using -mode=term, close the terminal automatically when terminal process is finished
-hidden=?0set to 1 to setup bufhidden to hide for internal terminal
-listed=?1when using -mode=term, set to 0 to hide the terminal in the buffer list

The -pos field accepts an uppercase TAB, to create a tab on the left of the current tab. When using internal terminal in a split window, AsyncRun will firstly reuse a finished previous terminal window if it exists, if not, a new terminal window will be created in given position. Tab based terminal can also be reusable if -reuse is provided.

Terminal Name

There can be many commands running in the internal terminal, you can specify a name for each of them and receive it in g:asyncrun_name:

:AsyncRun -mode=term -pos=hide -name=123 -post=echo\ g:asyncrun_name  ls -la

When this process finished, script defined in -post will be executed and your command name will display by echo. Another variable g:asyncrun_code stores exit code.

Quickfix window

AsyncRun displays its output in quickfix window, so if you don't use :copen {height} to open quickfix window, you won't see any output. For convenience there is an option g:asyncrun_open for you:

:let g:asyncrun_open = 8

Setting g:asyncrun_open to 8 will open quickfix window automatically at 8 lines height after command starts.

Range support

AsyncRun can take a range of lines in the current buffer as command's stdin after version 1.3.27. You can try:

:%AsyncRun cat

the whole buffer will be the input of command cat. you will see the content of your current buffer will be output to the quickfix window.

:10,20AsyncRun python

text between line 10-20 will be taken as the stdin of python. code in that range will be executed by python and the output will display in the quickfix window.

:'<,'>AsyncRun -raw perl

The visual selection (line-wise) will be taken as stdin.

Advanced Topics

AsyncRun provides enough flexibility and possibility to customize various details of how to run a command.

Extra Runners

Besides the default quickfix and internal terminal mechanism, the user-defined runners allow you to run commands in any way you want. eg. in a new gnome-terminal window/tab, a floaterm window, or a side-by-side tmux split.

By default, AsyncRun is shipped with some popular runners:

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 splitVimuxtmux.vim
floatermrun in a new floaterm windowfloatermfloaterm.vim
floaterm_reuserun in a reusable floaterm windowfloatermfloaterm_reuse.vim
quickuirun in a quickui windowvim-quickuiquickui.vim
termhelprun in terminal helpvim-terminal-helptermhelp.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

e.g.

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

Screenshot for gnome runner:

<!-- ![](https://github.com/skywind3000/images/raw/master/p/asyncrun_extra/p_gnome_gvim.gif) -->

When using gnome, konsole, or xfce runner in GVim, you get exactly the same experience like starting a command-line program from IDEs.

When you use toggleterm2 and use the packer.nvim management plugin, you can set shortcut keys to specify the open window, such as:

	use({
		"skywind3000/asyncrun.vim",
		as = "asyncrun",
		config = function()
			require("asyncrun.toggleterm2").setup({
				mapping = "<leader>tt",
				start_in_insert = false,
				clear_env = false,
				go_back = true,
			})
		end,
	})

All runners are customizable, you can modify or define your own runners, see the next section "customize runner".

Customize Runner

User-defined runners allow you to specify how the command will run by creating a new runner. It can be useful when you want your commands run in a tmux split or a new gnome-terminal window:

function! MyRunner(opts)
    echo "command to run is: " . a:opts.cmd
endfunction

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

Then try:

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

When -mode is term and -pos can used to represent runner name.

Runner function has only one argument: opts, it contains the options extracted from :AsyncRun command line, and opts.cmd stores current command.

Another way to create a runner is to simply create a .vim file in the autoload/asyncrun/runner/ folder of your run-time-path (see the examples).

For more information, please visit project wiki: customize runner.

Command Modifier

Command modifiers can be used to change your command before running:

let g:asyncrun_program = get(g:, 'asyncrun_program', {})
let g:asyncrun_program.nice = { opts -> 'nice -5' . opts.cmd }

When you are using:

:AsyncRun -program=nice ls -la

The command ls -la will be changed into nice -5 ls -la.

The -program=msys, -program=wsl are both implemented as a new command modifier it changes command ls into:

c:\windows\sysnative\wsl.exe ls

And replace any thing like $(WSL_FILENAME) and $(WSL_FILEPATH) in your command.

Requirements

Vim 7.4.1829 is minimal version to support async mode. If you are use older versions, g:asyncrun_mode will fall back from 0/async to 1/sync. NeoVim 0.1.4 or later is also supported.

Recommend to use Vim 8.0 or later.

Cooperate with vim-fugitive:

asyncrun.vim can cooperate with vim-fugitive, see here.

Language Tips

More Topics

Don't forget to read the Frequently Asked Questions.

Cooperate with other Plugins

NameDescription
asynctasksIntroduce vscode's task system to vim (powered by AsyncRun).
vim-fugitiveperfect cooperation, asyncrun gets Gfetch/Gpush running in background
errormarkerperfect cooperation, errormarker will display the signs on the error or warning lines
airlinevery well, airline will display status of background jobs
sprintnice plugin who uses asyncrun to provide an IDE's run button to runs your code

See: Cooperate with famous plugins

History

Credits

Trying best to provide the most simply and convenience experience in the asynchronous-jobs.

Author: skywind3000 Please vote it if you like it: http://www.vim.org/scripts/script.php?script_id=5431