Skip to content

Task Examples

Linwei edited this page Nov 30, 2021 · 25 revisions

CMake Example

[project-init]
command=mkdir build && cd build && cmake .. -G "MinGW Makefiles"
cwd=<root>
errorformat=

[project-build]
command=cmake --build build
cwd=<root>
errorformat=%f:%l:%m

[project-run]
command="build/$(VIM_PRONAME)"
cwd=<root>
output=terminal

another complex version:

[project-init]
command=mkdir build && cd build && cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=1 .. && ln -s compile_commands.json ..
cwd=<root>
errorformat=

It will take care of compile_commands.json

Grep Example

Project wide grep, will ask you input a keyword to search:

[grep-word]
command=rg -n --no-heading --color never "$(?keyword)" "<root>" -tc -tcpp -tpy -tvim -tgo
cwd=$(VIM_ROOT)
errorformat=%f:%l:%m

If you want to search current word under cursor, you can use <c-r><c-w> to pick up current word, or define another task to search word under cursor directly:

[grep-cword]
command=rg -n --no-heading --color never "$(VIM_CWORD)" "<root>" -tc -tcpp -tpy -tvim -tgo 
cwd=$(VIM_ROOT)
errorformat=%f:%l:%m

They can be defined as a global task, and use a .ignore in each project to indicate what to search and what to skip.

Generate Ctags Database

[gen-tags]
command=ctags -R -f tags .
cwd=$(VIM_ROOT)
errorformat=

Don't forget set:

:set tags+=./tags;

It will allow vim search tags files in all parent directories of current buffer.

Universal File-Run Example

This is my own version of global file-run task:

[file-run]
command="$(VIM_FILEPATH)"
command:c,cpp="$(VIM_PATHNOEXT)"
command:go="$(VIM_PATHNOEXT)"
command:python=python "$(VIM_FILENAME)"
command:make=make -f "$(VIM_FILENAME)" run
command:emake=emake -e "$(VIM_FILENAME)"
command:javascript=node "$(VIM_FILENAME)"
command:sh=sh "$(VIM_FILENAME)"
command:lua=lua "$(VIM_FILENAME)"
command:perl=perl "$(VIM_FILENAME)"
command:ruby=ruby "$(VIM_FILENAME)"
command:zsh=zsh "$(VIM_FILENAME)"
command:bash=bash "$(VIM_FILENAME)"
command:fish=fish "$(VIM_FILENAME)"
command:php=php "$(VIM_FILENAME)"
command:erlang=escript "$(VIM_FILENAME)"
command:ps1=powershell -file "$(VIM_FILENAME)"
command:scala=scala "$(VIM_FILENAME)"
command:haskell=ghci "$(VIM_FILENAME)"
command:applescript=osascript "$(VIM_FILENAME)"
command:vim=:source %
output=terminal
cwd=$(VIM_FILEDIR)
save=2

Be aware, commands starting with a colon (command:vim=:source %) will be executed as vimscript.

Internal Variable Example

asynctasks.vim allows command have internal variables in $(VIM:varname) form:

[test-var-replace]
command=echo $(VIM:my_name)

And in your vimrc, you can define a dictionary contains this internal variables:

let g:asynctasks_environ = {'my_name': 'Somebody'}

And command :AsyncTask test-var-replace will output:

Somebody

WSL Example

If you are working in Windows GVim like me, you may want to edit with GVim.exe but compile and run your file in WSL. asynctasks.vim enables you to achieve this by simply including a program=wsl in your task configuration:

[wsl-file-build]
command=gcc -O2 -Wall "$(WSL_FILEPATH)" -o "$(WSL_PATHNOEXT)" -lm -lpthread
program=wsl

[wsl-file-run]
command="$(WSL_PATHNOEXT)"
program=wsl
output=terminal
cwd=$(VIM_FILEDIR)

Macros starting with VIM_ can still be used, in addition, there are some special macros for wsl:

$(WSL_FILEPATH)    # (WSL) File name of current buffer with full path
$(WSL_FILENAME)    # (WSL) File name of current buffer without path
$(WSL_FILEDIR)     # (WSL) Full path of current buffer without the file name
$(WSL_FILENOEXT)   # (WSL) File name of current buffer without path and extension
$(WSL_PATHNOEXT)   # (WSL) Current file name with full path but without extension
$(WSL_RELDIR)      # (WSL) File path relativize to current directory
$(WSL_RELNAME)     # (WSL) File name relativize to current directory
$(WSL_ROOT)        # (WSL) Project root directory
$(WSL_CWD)         # (WSL) Current directory
$(WSL_CFILE)       # (WSL) Current filename under cursor

for example, current $(VIM_FILEPATH) is:

D:\Source\Project1\src\hello.c

It will be converted in $(WSL_FILEPATH) as:

/mnt/d/Source/Project1/src/hello.c

You can use :AsyncTaskMacro! to check them out in Windows.

MSYS and Cygwin

When program is one of msys, mingw32 and mingw64, the task will use msys.

[msys-file-build]
command=gcc -O2 -Wall "$(MSYS_FILEPATH)" -o "$(MSYS_PATHNOEXT)" -lm -lpthread
program=msys

[mingw32-file-build]
command=gcc -O2 -Wall "$(MSYS_FILEPATH)" -o "$(MSYS_PATHNOEXT)" -lm -lpthread
program=mingw32

The only thing required it to tell asyncrun where your msys/git-bash is:

# use a standalone MSYS2
let g:asyncrun_msys = 'D:\MSYS32'

# or use git-bash
let g:asyncrun_msys = 'C:\Program Files\Git'

When program is cygwin, it will locate cygwin folder in g:asyncrun_cygwin, and tasks for cygwin can use macros starting with CYGWIN_, eg. $(CYGWIN_FILENAME).

Range Example

try this:

[cat-me]
command=cat

use it as:

:%AsyncTask cat-me

The whole buffer will be sen5 to cat command from stdin. You can specify a line range like:

:10,20AsyncTask cat-me

The content between line 10 to 20 will be sent to cat from its stdin. Visual selection also works.

Beware: this feature is only available when output=quickfix

Silence and Notify

If there is a silent=1 in your task, quickfix window will not open automatically no matter g:asyncrun_open is set or not:

[.silent-test]
command=echo silent
silent=1
notify=echo

Another option notify can be one of:

  • echo: echo a status in the cmdline when task finished.
  • bell: ring a bell when task finished.
  • sound:xxx.wav: play a sound when task finished.
  • :script: execute a vim script when task finished.

For vim built with +sound feature, you can define a sound:xxx like:

# play sound when finished
notify=sound:~/documents/finish.wav

# play success.wav if return code is zero, otherwise play failure.wav
notify=sound:~/documents/success.wav,~/documents/failure.wav

# call a function when finished
notify=:call MyTaskFinish()