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 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

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



知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看