Vim [编译并]运行快捷方式
发布于 2021-01-29 17:07:03
基本上,我想要的是vim中的键盘快捷键,可让我[编译并]运行当前正在编辑的C,C ++或Python程序。在伪代码中:
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
我意识到我可能会问很多,但如果可能的话,我会喜欢的!
关注者
0
被浏览
52
1 个回答
-
这样的事情会起作用。只需创建映射文件类型autocmd
<F4>
或您想要保存的文件类型,然后编译并运行该程序即可。它使用exec来构建字符串,并使用shellescape来转义文件名。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
是没有扩展名的缓冲区文件名