Vim [compile and] run shortcut(Vim [编译和] 运行快捷方式)
问题描述
基本上我想要的是 vim 中的键盘快捷键,让我[编译和]运行当前正在编辑的 C、C++ 或 Python 程序.在伪代码中:
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!
推荐答案
这样的事情会起作用.只需创建映射
或任何您想保存并编译和运行程序的文件类型 autocmd.它使用 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 [编译和] 运行快捷方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Vim [编译和] 运行快捷方式


基础教程推荐
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 这个宏可以转换成函数吗? 2022-01-01
- 常量变量在标题中不起作用 2021-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01