VIM [编译]运行快捷方式 [英] Vim [compile and] run shortcut
问题描述
基本上我想要的是在vim键盘快捷键,让我[编译]运行当前正在编辑C,C ++或Python程序。在伪code:
Basically what I want is a keyboard shortcut in vim that lets me [compile and] run the currently being edited C, C++ or Python program. In psuedocode:
when a shortcut key is pressed:
if current_extension == 'c' then
shell: gcc this_filename.c -o this_filename_without_extension
if retcode == 0 then shell: ./this_filename_without_extension
else if current_extension == 'cpp' then
shell: g++ this_filename.cpp -o this_filename_without_extension
if retcode == 0 then shell: ./this_filename_without_extension
else if current_extension == 'py' then
shell: python this_filename.py
end if
end key
我知道我可能会问一个有点多,但会喜欢它,如果这是可能的!
I realise I might be asking a bit much but would love it if this was possible!
推荐答案
这样的事情会工作。只要创建的文件类型自动命令的地图< F4>
或任何你想保存并编译和运行程序。它使用exec来构建该字符串,并使用shellescape逃脱的文件名。
Something like this would work. Just create filetype autocmd that map <F4>
or whatever you want to save and compile and run the program. It uses exec to build the string and uses shellescape to escape the file name.
autocmd filetype python nnoremap <F4> :w <bar> exec '!python '.shellescape('%')<CR>
autocmd filetype c nnoremap <F4> :w <bar> exec '!gcc '.shellescape('%').' -o '.shellescape('%:r').' && ./'.shellescape('%:r')<CR>
autocmd filetype cpp nnoremap <F4> :w <bar> exec '!g++ '.shellescape('%').' -o '.shellescape('%:r').' && ./'.shellescape('%:r')<CR>
%
是当前缓冲区的文件名。 %,R
是缓冲文件名不带扩展
%
is the current buffer filename. %:r
is the buffer filename without extension
这篇关于VIM [编译]运行快捷方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!