Vim[编译并]运行快捷方式 基本上我想要的是VIM中的键盘快捷方式,它允许我编译当前运行的正在编辑的C、C++或Python程序。在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
我意识到我的要求可能有点高,但如果这是可能的话,我会喜欢的 像这样的东西会有用的。只需创建映射Vim[编译并]运行快捷方式 基本上我想要的是VIM中的键盘快捷方式,它允许我编译当前运行的正在编辑的C、C++或Python程序。在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,c++,python,c,shell,vim,C++,Python,C,Shell,Vim,我意识到我的要求可能有点高,但如果这是可能的话,我会喜欢的 像这样的东西会有用的。只需创建映射的文件类型autocmd或任何您想要保存、编译和运行程序的文件类型。它使用exec构建字符串,并使用shellescape转义文件名 autocmd filetype python nnoremap <F4> :w <bar> exec '!python '.shellescape('%')<CR> autocmd filetype c nnoremap <F4
的文件类型autocmd或任何您想要保存、编译和运行程序的文件类型。它使用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>
autocmd文件类型python nnoremap:w exec'!python“.shellescape(“%”)
autocmd文件类型c nnoremap:w exec'!gcc'.shellescape('%').-o'.shellescape('%:r').&&&./'.shellescape('%:r'))
autocmd文件类型cpp nnoremap:w exec'!g++'.shellescape('%').-o'.shellescape('%:r').&&./'.shellescape('%:r'))
%
是当前缓冲区文件名%:r
是没有扩展名的缓冲区文件名似乎比您想要的做得更多。对于更简单的解决方案,您还可以将以下内容添加到vimrc中
au BufEnter *.cpp set makeprg=g++\ -g\ %\ -o\ %<
au BufEnter *.c set makeprg=gcc\ -g\ %\ -o\ %<
au BufEnter *.py set makeprg=python\ %
au BufEnter *.[rR] set makeprg=Rscript\ %
map <F5> :call CompileGcc()<CR>
func! CompileGcc()
exec "w"
silent make
endfunc
au BufEnter*.cpp set makeprg=g++\-g\%\-o\%
au BufEnter*.c set makeprg=gcc \-g\%\-o\%
au BufEnter*.py set makeprg=python\%
au BufEnter*[rR]集makeprg=Rscript\%
映射:调用CompileGcc()
func!CompileGcc()
执行官“w”
无声制造
endfunc
HTH这是我的地图,它真的有效。您可以将其更新为任何语言:
" <!----------------------------" gcc compile C files----------------------------------------!>
autocmd filetype c nnoremap <Leader>c :w <CR>:!gcc % -o %:r && ./%:r<CR>
" <!----------------------------" java compile files----------------------------------------!>
autocmd filetype java nnoremap <Leader>c :w <CR>:!javac % :r&& java %:r<CR>
" <!----------------------------" php compile files----------------------------------------!>
autocmd filetype php nnoremap <Leader>c :w <CR>:!clear && php %<CR>
“现在是2018年,vim 8已经发布了2年,并随所有mean stream Linux发行版和Mac OS X一起发布。但许多vim教程在十年前仍在教人们一些东西
您可以在vim中编译您的C++/Java程序,就像Sublime Text或NotePad++一样方便,并带有一些vim 8或NeoVim专用插件
例如,该插件将允许您在后台运行shell命令,并实时从quickfix窗口读取输出。
就像在IDE中编译程序一样,编译错误将由errorformat匹配,并突出显示并可选择。您可以在quickfix窗口中导航错误或在编译时继续编辑
快速设置
将以下行复制并粘贴到vimrc:
Plug 'skywind3000/asyncrun.vim'
" open quickfix window automatically when AsyncRun is executed
" set the quickfix window 6 lines height.
let g:asyncrun_open = 6
" ring the bell to notify you job finished
let g:asyncrun_bell = 1
" F10 to toggle quickfix window
nnoremap <F10> :call asyncrun#quickfix_toggle(6)<cr>
双引号用于处理包含空格的路径名。选项-cwd=$(VIM_FILEDIR)
表示在文件目录中运行文件。绝对路径名$(VIM_FILEDIR)/$(VIM_FILENOEXT)之所以使用
,是因为linux需要在当前目录中运行可执行文件时使用
/前缀,但windows不需要。使用二进制文件的绝对路径名可以处理此跨平台问题
另一个选项-raw
意味着输出将不会与vim的errorformat匹配,并将在quickfix中按原样显示。现在您可以使用F9编译文件,在quickfix窗口中检查编译错误,然后按F5运行二进制文件
构建C/C++项目
无论您使用何种生成工具make或cmake,项目生成都意味着对一组文件执行操作。它需要定位项目根目录。AsyncRun使用一种称为根标记的简单方法来标识项目根目录。项目根目录被标识为当前文件的最近祖先目录,其中包含se目录或文件:
let g:asyncrun_rootmarks = ['.svn', '.git', '.root', '_darcs']
如果没有任何父目录包含这些根标记,则当前文件的目录将用作项目根。这使我们能够使用
或$(VIM_root)
来表示项目根。可以设置F7来构建当前项目:
noremap <silent> <F7> :AsyncRun -cwd=<root> make <cr>
noremap <silent> <F8> :AsyncRun -cwd=<root> -raw make run <cr>
同时,如果您正在编写C++代码,可以将STD::Endl附加到STD::CUT的结尾。它可以强制刷新StdOUT缓冲区。如果您正在Windows上开发,AsyncRun可以为子进程打开一个新的CMD窗口:
nnoremap <silent> <F5> :AsyncRun -cwd=$(VIM_FILEDIR) -mode=4 "$(VIM_FILEDIR)/$(VIM_FILENOEXT)" <cr>
nnoremap <silent> <F8> :AsyncRun -cwd=<root> -mode=4 make run <cr>
AsyncRun定义了以下shell环境变量:
$VIM_FILEPATH - File name of current buffer with full path
$VIM_FILENAME - File name of current buffer without path
$VIM_FILEDIR - Full path of current buffer without the file name
$VIM_FILEEXT - File extension of current buffer
$VIM_FILENOEXT - File name of current buffer without path and extension
$VIM_CWD - Current directory
$VIM_RELDIR - File path relativize to current directory
$VIM_RELNAME - File name relativize to current directory
$VIM_ROOT - Project root directory
$VIM_CWORD - Current word under cursor
$VIM_CFILE - Current filename under cursor
$VIM_GUI - Is running under gui ?
$VIM_VERSION - Value of v:version
$VIM_COLUMNS - How many columns in vim's screen
$VIM_LINES - How many lines in vim's screen
$VIM_SVRNAME - Value of v:servername for +clientserver usage
上述所有环境变量都可以在build\u advanced.sh
中使用。使用外部shell脚本文件可以完成比单个命令更复杂的工作
Grep符号
有时,如果您的远程linux设备中没有良好的设置环境,grep是在源代码中搜索符号定义和引用的最便宜的方法。现在我们将使用F2来搜索光标下的关键字:
if has('win32') || has('win64')
noremap <F2> :AsyncRun! -cwd=<root> grep -n -s -R <C-R><C-W> --include='*.h' --include='*.c*' '<root>' <cr>
else
noremap <F2> :AsyncRun! -cwd=<root> findstr /n /s /C:"<C-R><C-W>" "\%CD\%\*.h" "\%CD\%\*.c*" <cr>
endif
if has('win32')| | has('win64'))
noremap:AsyncRun!-cwd=grep-n-s-R--include='*.h'--include='*.c*''
其他的
noremap:AsyncRun!-cwd=findstr/n/s/C:“\%CD\%\*.h”“\%CD\%\*.C*”
恩迪夫
上面的脚本将在项目根目录中运行grep或findstr,并仅在.c
、.cpp
和.h
文件中查找符号。现在我们移动光标并按F2键,当前项目中的符号引用将立即显示在quickfix窗口中
这个简单的keymap在大多数情况下都足够了。您可以改进这个脚本,以便在vimrc中支持更多的文件类型或其他grep工具
这是在Vim 8或NeoVim中构建/运行C/C++项目的实用方法,就像Sublime Text的构建系统和NotePad++的NppExec一样
不再使用过时的vim教程,请尝试新的内容。我知道我是7年后来到这里的。我尝试了其他答案的代码,但结果令我不满意,因此我尝试了以下方法:
autocmd filetype cpp nnoremap <F9> :w<bar>term ++shell g++ %:p -o %:p:r && %:p:r<CR>
autocmd文件类型cpp nnoremap:wterm++shell g++%:p-o%:p:r&&%:p:r
按F9键时,它(与上面的所有答案一样)编译并执行当前文件。输出显示在:终端部分拆分屏幕中。
您可以更改g++命令以运行其他语言代码
它保存在中,使用另一个映射来显示输出,也保存在分割的部分中,但保存在新文件中,因此您可以保存输出
希望它能帮助别人。我也想找到一条捷径。但我不想出于某种原因而使用
autocmd
nnoremap <silent> <F4> :AsyncRun -cwd=<root> cmake . <cr>
setbuf(stdout, NULL);
nnoremap <silent> <F5> :AsyncRun -cwd=$(VIM_FILEDIR) -mode=4 "$(VIM_FILEDIR)/$(VIM_FILENOEXT)" <cr>
nnoremap <silent> <F8> :AsyncRun -cwd=<root> -mode=4 make run <cr>
nnoremap <F3> :AsyncRun -cwd=<root> sh /path/to/your/dotfiles/script/build_advanced.sh <cr>
$VIM_FILEPATH - File name of current buffer with full path
$VIM_FILENAME - File name of current buffer without path
$VIM_FILEDIR - Full path of current buffer without the file name
$VIM_FILEEXT - File extension of current buffer
$VIM_FILENOEXT - File name of current buffer without path and extension
$VIM_CWD - Current directory
$VIM_RELDIR - File path relativize to current directory
$VIM_RELNAME - File name relativize to current directory
$VIM_ROOT - Project root directory
$VIM_CWORD - Current word under cursor
$VIM_CFILE - Current filename under cursor
$VIM_GUI - Is running under gui ?
$VIM_VERSION - Value of v:version
$VIM_COLUMNS - How many columns in vim's screen
$VIM_LINES - How many lines in vim's screen
$VIM_SVRNAME - Value of v:servername for +clientserver usage
if has('win32') || has('win64')
noremap <F2> :AsyncRun! -cwd=<root> grep -n -s -R <C-R><C-W> --include='*.h' --include='*.c*' '<root>' <cr>
else
noremap <F2> :AsyncRun! -cwd=<root> findstr /n /s /C:"<C-R><C-W>" "\%CD\%\*.h" "\%CD\%\*.c*" <cr>
endif
autocmd filetype cpp nnoremap <F9> :w<bar>term ++shell g++ %:p -o %:p:r && %:p:r<CR>
filename=$1
compiler=clang-10
if [ $2 ]; then
if [ $2 == "-v" ]; then
FLAGS="-g -Wall -Wextra -pedantic -std=c2x"
fi
else
FLAGS="-Wall -Wextra -pedantic -std=c2x"
fi
$compiler $FLAGS $filename.c -o $filename -lm
return_value=$?
if [ $return_value -eq 0]; then
if [ $2 ]; then
if [ $2 == "-v" ]; then
valgrind ./$filename
rm $filename
fi
else
./$filename
echo
echo "[process exited $return_value]"
rm $filename
fi
fi
$ chmod 777 run
$ mv run ~/bin
$ cd ~
$ mkdir bin
$ mv run ~/bin
nnoremap <F9> :vertical bo term run %:r<CR>
# This will run my code with valgrind
nnoremap <F2> :vertical bo term run %:r -v<CR>