惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
Docker
B
Blog RSS Feed
Microsoft Security Blog
Microsoft Security Blog
Y
Y Combinator Blog
N
Netflix TechBlog - Medium
M
MIT News - Artificial intelligence
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
B
Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
C
Check Point Blog
The GitHub Blog
The GitHub Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
P
Proofpoint News Feed
Martin Fowler
Martin Fowler
大猫的无限游戏
大猫的无限游戏
GbyAI
GbyAI
博客园_首页
A
About on SuperTechFans
Blog — PlanetScale
Blog — PlanetScale
人人都是产品经理
人人都是产品经理
T
Tailwind CSS Blog
aimingoo的专栏
aimingoo的专栏
T
The Blog of Author Tim Ferriss
The Cloudflare Blog

jdhao's digital space

Conversion between base64 and OpenCV or PIL Image 腾讯云对象存储博客图床开启 CDN 加速(不需要购买额外域名) Search and Replace in Multiple Files in Vim/Neovim Change Table Column Width in LaTeX Image or Table Side by Side in LaTeX LaTeX 并排显示图像或表格 Firenvim: Neovim inside Your Browser Content inside HTML tags missing in Latest Hugo? Creating Markdown Front Matter with Ultisnips Labelme JSON 标注格式转 voc XML 格式 Nifty Nvim Techniques That Make My Life Easier -- Series 6 macOS 下如何为视频制作字幕 Resolving Merge Conflict after Git Stash Pop Pylint: command not found? A Hands-on Experience with Neovim's Built-in LSP Support How to Convert PDF to Images with Imagemagick 互联网上常用缩略语集锦 File Backup in Neovim Converting PDF Pages to Images with Poppler Nifty Nvim Techniques That Make My Life Easier -- Series 5 Neovim Configuration for System-wide Use How to sort a list of tuple or list in Python -- lambda or itemgetter? Building A Vim Statusline from Scratch 人类第一颗原子弹爆炸始末 Distributed Training in PyTorch with Horovod Learning Expect Programming Essential Knowledge about SSH Nifty LaTeX Techniques -- Series 1 更改 Adsense 邮寄地址,重新寄送 PIN Mintty Tips and Configurations
Running Command Asynchronously inside Neovim
2019-12-09 · via jdhao's digital space

Introduction#

When I am writing some prototyping code inside Neovim, I want to run the script without going to the terminal and typing python toy_script.py. Of course, we can use :!python % in the command line to run the script (see :h :!). The problem is that running external command will block Nvim/Vim, and we can not move the cursor until the script finishes running.

Of course, on Linux and Mac, you can use !python % & to run the script in the background, but you can not get the output of the script.

Asynchronous command in Nvim/Vim#

Fortunately, both Neovim and Vim provides the job feature to run external command asynchronously. However, to use those features, you have to write non-trivial amount of code. Thanks to plugins like vim-dispatch and asyncrun.vim, we can use this feature easily.

Asyncrun.vim#

In this post, I would like to share how to set up asyncrun.vim and use it. First, you can install it with vim-plug:

Plug 'skywind3000/asyncrun.vim'

It provides the AsyncRun command to run external shell commands asynchronously. For example, to run the current Python script, you can use the following command:

" use quote to avoid spaces in file names
:AsyncRun python "%"

The above command will run current script in the background without blocking Neovim. You can still edit the source code as you will when the program is running..

Asyncrun.vim will put the output of the command in the quickfix list. To see the command output, use :copen to open the quickfix window. You can also set up a config to open the quickfix window automatically when you use AsyncRun. Just add the following setting to your init.vim or vimrc:

Then a quickfix window of 6 line tall will be opened automatically after asyncrun starts.

Python related issues#

Command output encoding#

When you use asyncrun.vim on Windows, you may find that Chinese characters may not be displayed properly in the quickfix window. That is because the command output encoding on Windows may not be the same with the encoding you use (suppose you are using utf-8 encoding). You can set up the variable g:asyncrun_encs to gbk encoding to solve this issue:

if has('win32')
    " Command output encoding for Windows
    let g:asyncrun_encs = 'gbk'
endif

Then Chinese characters should display properly.

Display command output in real time#

When python script is running in background, the output is buffered, i.e., you can not see the script output immediately after a print() statement is executed. You will only see the output after cache buffer is full or when the script finishes execution. There are two easy ways to fix this issue.

The first is to add PYTHONUNBUFFERED variable in your init.vim or vimrc:

Or you need to run python command with -u option:

We can also create a shortcut to execute script quickly:

augroup python_file
    autocmd!
    autocmd FileType python nnoremap <F9> :AsyncRun python -u "%"<CR>
augroup END

References#