Skip to content

Task Examples

Linwei edited this page Feb 18, 2020 · 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:%m:%l

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

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=echo cannot run $(VIM_FILEPATH)
output=terminal
cwd=$(VIM_FILEDIR)
save=2

Feel free to take and modify.

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.

Clone this wiki locally