windows下在vim中搭建c語言開發(fā)環(huán)境的詳細(xì)過程
1 代碼格式化
C語言代碼的格式化需要使用clang-format,而clang-format被集成在了llvm中,所以需要先安裝llvm,點(diǎn)擊此處下載

下載之后運(yùn)行安裝文件,將其中的bin目錄添加到環(huán)境變量path中(需重啟電腦使新添加的環(huán)境變量生效)。例如我安裝后的目錄為C:\wsr\LLVM\bin,圖中的clang-format就是格式化c代碼需要的組件


1.1 clang-format初體驗(yàn)
test1.c
#include <stdio.h>
int main(int argc, char *argv[])
{
int a[2][2] = {{1,2}
,
{3,4}};
printf("Hello World!\n");
printf("Hello World!\n");
printf("Hello World!\n");
printf("Hello World!\n");
return 0;
}
打開powershell或cmd,設(shè)置環(huán)境變量后沒有重啟電腦需要進(jìn)入llvm的bin目錄,運(yùn)行如下命令:
PS C:\Users\fy> cd C:\wsr\LLVM\bin\ PS C:\wsr\LLVM\bin> .\clang-format.exe -style=google -i C:\Users\fy\Desktop\test1.c PS C:\wsr\LLVM\bin>
test1.c的源代碼被格式化為
#include <stdio.h>
int main(int argc, char *argv[]) {
int a[2][2] = {{1, 2}, {3, 4}};
printf("Hello World!\n");
printf("Hello World!\n");
printf("Hello World!\n");
printf("Hello World!\n");
return 0;
}
命令解釋
用法:clang-format -style=xxx -i ***.c
- -style 表示風(fēng)格,可選項(xiàng)為 LLVM、Google、Chromium、Mozilla、WebKit 和 file,其中 file 指定參數(shù)文件。
- -i 表示將格式化后的內(nèi)容寫入原文件。
如:
PS C:\wsr\LLVM\bin> .\clang-format -style=Chromium -i C:\Users\fy\Desktop\test1.c PS C:\wsr\LLVM\bin> .\clang-format -style=LLVM -i C:\Users\fy\Desktop\test1.c PS C:\wsr\LLVM\bin> .\clang-format -style=google -i C:\Users\fy\Desktop\test1.c PS C:\wsr\LLVM\bin> .\clang-format -style=WebKit -i C:\Users\fy\Desktop\test1.c
1.2 在vim中格式化c代碼
1.2.1 為vim配置python
首先,打開vim,在命令行模式下輸入:version,查看所使用的vim是否支持python,以及支持的python版本


從輸出可以看出,我用的vim是32位的vim8.2,此版本支持python,且所用的python版本為python2.7及python3.6。由于配置環(huán)境過程中用到的插件與vim使用的python版本有關(guān),所以下面先為vim配置python。
打開python官網(wǎng),下載32位的python2.7及python3.6,我下載的為2.7.18和3.6.8:
https://www.python.org/ftp/python/3.6.8/python-3.6.8.exe
https://www.python.org/ftp/python/2.7.18/python-2.7.18.msi
下載好之后將兩個(gè)版本的python分別安裝(若已安裝過其他版本python不用卸載)。這里安裝時(shí)自定義安裝目錄,不要為其設(shè)置環(huán)境變量。
我的自定義安裝目錄如下所示:
C:\wsr (x86)\python\python27 C:\wsr (x86)\python\python36
編輯vim的配置文件.gvimrc,在其中添加如下設(shè)置項(xiàng)(安裝目錄中有空格的話,在空格前添加一個(gè)\):
set pythondll=C:\wsr\ (x86)\python\python27\python27.dll set pythonhome=C:\wsr\ (x86)\python\python27 set pythonthreedll=C:\wsr\ (x86)\python\python36\python36.dll set pythonthreehome=C:\wsr\ (x86)\python\python36
然后在vim命令行模式執(zhí)行:source $MYVIMRC,使改動(dòng)生效。
1.2.2 安裝代碼格式化插件
安裝代碼格式化插件vim-clang-format,打開powershell,執(zhí)行命令:
PS C:\Users\fy\vimfiles\pack\my_plugins\start> git clone https://github.com/rhysd/vim-clang-format.git vim-clang-format
注:這里沒有使用插件管理器安裝插件
仍然以test1.c為例
#include <stdio.h>
int main(int argc, char *argv[])
{
int a[2][2] = {{1,2}
,
{3,4}};
printf("Hello World!\n");
printf("Hello World!\n");
printf("Hello World!\n");
printf("Hello World!\n");
return 0;
}
命令行模式下,執(zhí)行:ClangFormat,則源文件就被格式化了

也可以在.gvimrc中為vim-clang-format設(shè)置快捷鍵
let g:clang_format#style_options = {
\ "AccessModifierOffset" : -4,
\ "AlignConsecutiveAssignments": "true",
\ "AlignConsecutiveDeclarations": "true",
\ "AlignConsecutiveMacros": "true",
\ "AlignOperands": "true",
\ "AlignTrailingComments": "true",
\ "AllowShortFunctionsOnASingleLine" : "Empty",
\ "AllowShortIfStatementsOnASingleLine" : "true",
\ "AllowShortLoopsOnASingleLine" : "true",
\ "AlwaysBreakTemplateDeclarations" : "true",
\ "BreakBeforeBraces" : "WebKit",
\ "BreakBeforeTernaryOperators " : "true",
\ "BreakStringLiterals" : "true",
\ "ColumnLimit": 80,
\ "MaxEmptyLinesToKeep": 1,
\ "Standard" : "C++11"}
" 使用 <Leader>cf 格式化代碼
autocmd FileType c,cpp,objc nnoremap <buffer><Leader>cf :<C-u>ClangFormat<CR>
autocmd FileType c,cpp,objc vnoremap <buffer><Leader>cf :ClangFormat<CR>
" Toggle auto formatting:
nmap <Leader>C :ClangFormatAutoToggle<CR>
2 代碼自動(dòng)補(bǔ)全
代碼自動(dòng)補(bǔ)全需要安裝插件vim-snippets、 ultisnips
PS C:\Users\fy\vimfiles\pack\my_plugins\start> git clone https://github.com/honza/vim-snippets.git vim-snippets PS C:\Users\fy\vimfiles\pack\my_plugins\start> git clone https://github.com/SirVer/ultisnips.git ultisnips
安裝完成之后,就可以在vim中使用代碼自動(dòng)補(bǔ)全了。使用vim編輯test2.c:
輸入 inc、按Tab鍵
輸入 main、按Tab鍵
輸入 printf、按Tab鍵
……
如果想自定義代碼片段的話,可以在c:\users\username\vimfiles文件夾中新建文件夾ultisnips,并在其中新建文件c.snippets、cpp.snippets……
c.snippets對應(yīng)C語言、cpp.snippets對應(yīng)C++……

其中文件夾的名字自定義,若有多個(gè)文件夾,可在.gvimrc添加如下設(shè)置
let g:UltiSnipsSnippetDirectories=["ultisnips","ultisnips-1"]

其中自定義代碼片段的格式為:
snippet trigger_word [ "description" [ options ] ] snippet 縮寫 [ “描述” [選項(xiàng)] ] code endsnippet
如C:\Users\fy\vimfiles\ultisnips\c.snippets:
snippet if0
if ($1 == 0) {
${2}
}
endsnippet
定義上述代碼片段之后,在C源文件中編輯代碼時(shí)輸入if0,再按下Tab就能添加代碼了。
3 編譯運(yùn)行源程序
在.gvimrc中添加如下設(shè)置:
nnoremap <F5> :call CompileAndRun()<CR>
func! CompileAndRun()
exec "w"
echo "Compiling..."
if &filetype == 'c'
exec "!gcc % -o %<"
elseif &filetype == 'cpp'
exec "!g++ % -o %<"
endif
echo "\nRunning..."
exec "!%<"
"exec "! ./%<"
endfunc
更改生效后,按下F5就能一鍵編譯運(yùn)行C程序了
到此這篇關(guān)于windows下在vim中搭建c語言開發(fā)環(huán)境的文章就介紹到這了,更多相關(guān)vim中搭建c語言開發(fā)環(huán)境內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
利用C++單例模式實(shí)現(xiàn)高性能配置管理器
這篇文章主要為大家詳細(xì)介紹了如何利用C++單例模式實(shí)現(xiàn)高性能配置管理器,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下2023-04-04
C++ 中l(wèi)ambda表達(dá)式的編譯器實(shí)現(xiàn)原理
C++ 11加入了一個(gè)非常重要的特性——Lambda表達(dá)式。這篇文章主要介紹了C++ 中l(wèi)ambda表達(dá)式的編譯器實(shí)現(xiàn)原理,需要的朋友可以參考下2017-02-02
C++中用substr()函數(shù)消除前后空格的解決方法詳解
本篇文章是對C++中用substr()函數(shù)消除前后空格的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
關(guān)于C語言函數(shù)strstr()的分析以及實(shí)現(xiàn)
以下是對C語言中strstr()函數(shù)的使用進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以參考下2013-07-07
Qt實(shí)現(xiàn)簡易計(jì)時(shí)器的示例代碼
計(jì)時(shí)器實(shí)現(xiàn)四個(gè)功能:開始計(jì)時(shí)、停止計(jì)時(shí)、暫停計(jì)時(shí)以及打點(diǎn)。當(dāng)點(diǎn)擊暫停時(shí),開始按鈕和停止按鈕無法點(diǎn)擊。當(dāng)點(diǎn)擊停止時(shí),開始按鈕和暫停按鈕無法點(diǎn)擊,此時(shí)停止按鈕變?yōu)榍辶恪1疚膶⒂肣t實(shí)現(xiàn)這樣的一個(gè)計(jì)時(shí)器,需要的可以參考一下2022-06-06
C語言數(shù)據(jù)結(jié)構(gòu)與算法之鏈表(一)
鏈表是線性表的鏈?zhǔn)酱鎯Ψ绞健f湵淼膬?nèi)存是不連續(xù)的,前一個(gè)元素存儲地址的下一個(gè)地址中存儲的不一定是下一個(gè)元素。小編今天就將帶大家深入了解一下鏈表,快來學(xué)習(xí)吧2021-12-12

