使vim能够像vscode一样按f5运行代码
效果图
let g:last_terminal_buf = -1 " 用于存储上一个终端缓冲区编号
function! RunCurrentFile()
" 如果存在上次的终端缓冲区,则删除它
if g:last_terminal_buf != -1 && bufexists(g:last_terminal_buf)
silent execute 'bd! '.g:last_terminal_buf
endif" 确保文件被保存
writelet l:filetype = &filetype
let l:cmd = ""if l:filetype == 'python'
let l:cmd = "python ".expand("%:p")
elseif l:filetype == 'javascript'
let l:cmd = "node ".expand("%:p")
elseif l:filetype == 'cpp'
" 对于C++,你可能需要先编译再运行
let l:cmd = "g++ -o %< ".expand("%:p")." && ./%<"
else
echo "Unsupported file type!"
return
endif" 计算新窗口的高度为当前编辑器高度的三分之一
let l:winheight = winheight(0) / 3" 使用 botright terminal 直接打开终端并执行命令,同时指定窗口大小
execute "belowright terminal ".l:cmd
execute "resize ".l:winheight" 更新全局变量以指向新的终端缓冲区
let g:last_terminal_buf = bufnr("$")
endfunction" 将F5键映射到RunCurrentFile函数
nnoremap:call RunCurrentFile()
inoremap:call RunCurrentFile()
将以上配置添加到vimrc里就行