[Description]
There is often this demand: In the Linux command line, I hope to run the currently edited script file under the test without leaving Vim (such as sometimes printing something)
I know a single language such as Python,<leader>r :!python %<cr>
I know that the shell can be called in Vim, but how to map multiple languages to one shortcut key (preliminarily planned to be <Leader>r)?
The basic idea is to use the
autocmd
根据当前缓冲的文件的类型或者语法的类型添加不同的map
command.For example, if you only need to compile/execute the current file, you can add the following in your
vimrc
file for each different file type:For python, that’s
Among them
<F5>
可以改成其他你想映射的键位,如<leader>r
.If you need cross-platform, you can refer to here.
If you need more advanced functions, such as multi-file compilation, running part of the code in the file, etc., you need to write your own VIM script. For python, please refer to here.
Create a file in vim's fplugin directory
[语言名].vim
Then write the corresponding language configuration into this file, vim will automatically load these configurations based on the file suffix
Add execution permissions to the file and add
#!/bin/bash #!/usr/bin/python #!/usr/bin/ruby to the file header.
Every time you want to run:./ this file will be fine.
Recommend you to use
vim-quickrun