Vim 的文件类型判断

30次阅读

共计 903 个字符,预计需要花费 3 分钟才能阅读完成。

Vim 执行当前可执行文件
方法一:
:! %:p
其中:

方法二:
:! ./%
相当于在终端手敲了一遍:./script.sh 这样的。
Vim 根据不同类型文件设置不同快捷键
因为想做一个 IDE 中的 build 功能,即针对不同的语言类型,用不同的 build/compile/run 等方法。比如我想将这个 build 映射为 Ctrl+i。
那么可以用到 Vim 的 autocmd FileType 语言类型方式。其中,autocmd 相当于 call function() 的 call,说明要调用函数了。FileType 是 Vim 自带的一个函数,可以执行当前文件类型的检测。后面的语言相当于传给函数的参数。这个我们可以通过命令:echo &filetype 获得。
常用的语言类型有:vimrc 即 vim,zshrc 即 zsh,tmux.conf 即 tmux,python,c,cpp 等。
我的 Mappings:
” Filetype based Mappings—-{
” Get current filetype -> :echo &filetype or as variable &filetype
” [Builds / Compiles / Interpretes]

” C Compiler:
autocmd FileType c nnoremap <buffer> <C-i> :!gcc % && ./a.out <CR>

” C++ Compiler
autocmd FileType cpp nnoremap <buffer> <C-i> :!g++ % && ./a.out <CR>

” Python Interpreter
autocmd FileType python nnoremap <buffer> <C-i> :!python % <CR>

” Bash script
autocmd FileType sh nnoremap <buffer> <C-i> :!sh % <CR>

” Executable
nnoremap <buffer> <C-i> :!./% <CR>
“nnoremap <buffer> <C-i> :! %:p <CR>

” RCs (Configs)
autocmd FileType vim,zsh,tmux nnoremap <buffer> <C-i> :source % <CR>

” }

正文完
 0