Warm tip: This article is reproduced from serverfault.com, please click

c++-Vim [编译并]运行快捷方式

(c++ - Vim [compile and] run shortcut)

发布于 2013-08-18 06:00:25

基本上,我想要的是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

我意识到我可能会问很多,但是如果可能的话,我会很乐意的!

Questioner
user1318194
Viewed
11
FDinoff 2013-08-18 14:23:30

这样的事情会起作用。只需创建映射文件类型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是没有扩展名的缓冲区文件名