先說(shuō)點(diǎn)廢話。
最近裝了 WSL2吧凉,想著作為 Win10 內(nèi)置的 Linux 系統(tǒng)隧出,應(yīng)該比一般的虛擬機(jī)要來(lái)得平滑一些。畢竟虛擬機(jī)還要裝 Virtualbox客燕,每次開機(jī)還要多點(diǎn)幾下鸳劳,還要等它啟動(dòng)。怕麻煩也搓。赏廓。。
可能習(xí)慣問(wèn)題傍妒,喜歡在 Linux 下配置開發(fā)環(huán)境(學(xué)習(xí)寫代碼)幔摸。總感覺 Windows 上略顯磨嘰了一些颤练。也許我道行不夠既忆。碰到需要編譯的東西,一套工具裝起來(lái)過(guò)于繁瑣嗦玖,直接 sudo apt install gcc
不香嗎患雇。
可惜 WSL 沒有桌面支持,裝不上 Vscode宇挫。對(duì)于工具的選擇苛吱,我的原則是簡(jiǎn)單夠用就好,Vscode 即是目前我的最愛(非重度編碼)器瘪。
開箱即用翠储,功能滿足基本需求。無(wú)非代碼高亮橡疼、自動(dòng)補(bǔ)全援所、查看定義、語(yǔ)法檢查欣除、代碼格式化住拭,再加個(gè)跳轉(zhuǎn)。
于是決定在 Windows 系統(tǒng)上安裝 Vscode历帚,借助 Remote WSL 插件“遠(yuǎn)程”編碼废酷。可惜有 BUG抹缕,一直不能正常工作澈蟆。Github 上提了 issue,沒人理卓研。無(wú)奈轉(zhuǎn)向 Vim趴俘。
一開始用的補(bǔ)全插件是 YouCompleteMe睹簇,還要編譯。雖然敲個(gè)命令就能自動(dòng)執(zhí)行寥闪,但光 Rust 支持就在本地給我搞了將近 1G 的文件太惠,不太能忍。網(wǎng)上查了下疲憋,貌似已經(jīng)很老的機(jī)制了凿渊。于是轉(zhuǎn)向較新一點(diǎn)的 coc.nvim。據(jù)說(shuō)支持各種 LSP(這個(gè)縮寫缚柳。埃脏。。)秋忙,也不懂彩掐,能用就行。
廢話結(jié)束灰追。
一堵幽、效果截圖
二、NERDTree 文件瀏覽器
vim 用的自帶的 8.1 版本弹澎,有內(nèi)置的插件管理朴下。一般的插件安裝流程是直接將插件源代碼 clone 到 ~/.vim/pack/vendor/start
路徑下(沒有就創(chuàng)建),再在 vim 中運(yùn)行 :helptags ~/.vim/pack/vendor/start/someplugin/doc
命令生成幫助文檔(這一步可選)苦蒿。
vim 會(huì)自動(dòng)檢測(cè)已經(jīng)下載的插件殴胧。vendor
路徑下可以有 start
和 opt
兩個(gè)目錄,start
路徑下的插件會(huì)在 vim 啟動(dòng)時(shí)自動(dòng)加載刽肠,opt
路徑下的插件則需要通過(guò) :packadd
命令手動(dòng)加載。
vendor
也可以是其他名稱免胃,同位置下也可以有多個(gè)相同結(jié)構(gòu)的目錄音五,方便對(duì)不同類型的插件分別進(jìn)行管理。
省事起見羔沙,這里所有的插件都放在 ~/.vim/pack/vendor/start
下面躺涝。
NERDTree 文件瀏覽器插件安裝:
cd ~/.vim/pack/vendor/start
git clone https://github.com/scrooloose/nerdtree
在 ~/.vimrc
文件中添加配置:
" 進(jìn)入 vim 時(shí)自動(dòng)開啟 NERDTree
autocmd VimEnter * NERDTree | wincmd p
" 若關(guān)閉某個(gè) buff 后 NERDTree 是僅剩的最后一個(gè) buff,則自動(dòng)關(guān)閉 NERDTree
autocmd BufEnter * if tabpagenr('$') == 1 && winnr('$') == 1 && exists('b:NERDTree') && b:NERDTree.isTabTree() | quit | endif
" 使用 Ctrl+n 快捷鍵打開或關(guān)閉 NERDTree
nnoremap <C-n> :NERDTreeToggle<CR>
更多配置選項(xiàng)參考 VimAwesome
三扼雏、vim-airline 與配色
安裝 vim-airline 狀態(tài)欄美化插件:
git clone https://github.com/vim-airline/vim-airline
關(guān)于配色坚嗜,蘿卜青菜各有所愛。懶得去一個(gè)一個(gè)試诗充。
準(zhǔn)備了兩套苍蔬,vim-one 深色和 gruvbox 淺色。
vim-one 插件安裝:
git clone https://github.com/rakr/vim-one
添加配置:
" 深色背景
set bg=dark
" 啟用 one 配色
colorscheme one
效果截圖:安裝 gruvbox 插件:
git clone https://github.com/morhetz/gruvbox
修改配置文件(注釋掉 colorscheme one
):
set bg=light
" colorscheme one
autocmd vimenter * ++nested colorscheme gruvbox
效果截圖:更復(fù)雜的配置可自行在 VimAwesome 搜索對(duì)應(yīng)的插件蝴蜓,或者進(jìn)入相應(yīng)的 Github 主頁(yè)查看碟绑。
四俺猿、coco.nvim 代碼補(bǔ)全與語(yǔ)法檢查
安裝 nodejs >= 10.12,官網(wǎng)上寫的是通過(guò) curl -sL install-node.now.sh/lts | bash
命令安裝格仲。我個(gè)人建議使用 nvm 安裝最新的 lts 版本押袍。此處不贅述。
安裝 coc.nvim 插件:
git clone https://github.com/neoclide/coc.nvim.git
為了得到某種編程語(yǔ)言的補(bǔ)全功能凯肋,還需要安裝對(duì)應(yīng)語(yǔ)言的 coc 擴(kuò)展以及代碼補(bǔ)全后端(LSP)谊惭。
比如 C/C++ 對(duì)應(yīng)的 coc 擴(kuò)展為 coc-clangd,LSP 為 clangd
侮东。兩個(gè)都需要圈盔。
安裝 coc 擴(kuò)展的方法非常簡(jiǎn)單,進(jìn)入 vim 后運(yùn)行 :CocInstall extension_name
命令即可苗桂。
比如使用 :CocInstall coc-clangd
命令安裝 coc-clangd
擴(kuò)展药磺。coc 擴(kuò)展我遇到的都比較小,安裝非常迅速煤伟,也會(huì)單獨(dú)開一個(gè)窗口顯示進(jìn)度信息癌佩。
coc 擴(kuò)展安裝完成后,打開對(duì)應(yīng)的源代碼文件便锨,比如 vim test.c
围辙,vim 就會(huì)自動(dòng)在本地環(huán)境中尋找對(duì)應(yīng)的 LSP(C/C++ 語(yǔ)言是 clangd
)。
若 clangd
此時(shí)并未安裝放案,vim 就會(huì)提示你運(yùn)行某個(gè)命令(在 vim 內(nèi)部)自動(dòng)安裝該依賴姚建。這里有個(gè)坑。
不知道是不是網(wǎng)絡(luò)的問(wèn)題吱殉,我復(fù)制運(yùn)行了 vim 提供的命令掸冤,一直顯示下載中,幾個(gè)小時(shí)不見下載完成友雳。稿湿。。
好在可以手動(dòng)安裝 clangd
押赊,退出 vim 直接運(yùn)行 sudo apt install clangd
即可饺藤。
此時(shí) coc.nvim 對(duì)于 C/C++ 的補(bǔ)全支持即安裝配置完成。
Rust 和 Python 語(yǔ)言支持
對(duì)于 Rust 語(yǔ)言流礁,需要先安裝 coc-rust-analyzer 擴(kuò)展:
:CocInstall coc-rust-analyzer
這一步簡(jiǎn)單迅速涕俗。
安裝 rust-src
:rustup component add rust-src
接著還必須安裝針對(duì) Rust 的 LSP(rust-analyzer
)。鑒于安裝 clangd
時(shí)出現(xiàn)的曲折神帅,我決定手動(dòng)安裝 rust-analyzer
再姑。詭異的事情發(fā)生了。手動(dòng)安裝的可執(zhí)行程序不被 coc 識(shí)別找御。
無(wú)奈下嘗試 vim 中的自動(dòng)安裝居然成功了询刹。谜嫉。。
方法是用 vim 新建任意一個(gè) rust 源文件(vim test.rs
)凹联,vim 會(huì)自動(dòng)彈出提示沐兰,找不到 rust-analyzer
,是否自動(dòng)安裝蔽挠,選擇 Yes
即可住闯。這里的安裝過(guò)程居然異乎尋常的快。
安裝完成后可能不會(huì)立即生效澳淑,會(huì)嘗試創(chuàng)建索引比原。多打開幾個(gè)文件試試。
至于 Python杠巡,安裝 coc-pyright:
:CocInstall coc-pyright
印象中并沒有做其他操作量窘,對(duì)于 Python 的支持就自動(dòng)生效了,也許是安裝擴(kuò)展的時(shí)候自動(dòng)安裝了對(duì)應(yīng)的 LSP氢拥。
對(duì)于其他語(yǔ)言的支持蚌铜,可參考 Using coc extensions。
coc.nvim 示例配置(從官方 Github 上 copy 的嫩海,主要是一些快捷鍵的映射冬殃,可根據(jù)需求刪減。沒細(xì)看):
" Set internal encoding of vim, not needed on neovim, since coc.nvim using some
" unicode characters in the file autoload/float.vim
set encoding=utf-8
" TextEdit might fail if hidden is not set.
set hidden
" Some servers have issues with backup files, see #649.
set nobackup
set nowritebackup
" Give more space for displaying messages.
set cmdheight=2
" Having longer updatetime (default is 4000 ms = 4 s) leads to noticeable
" delays and poor user experience.
set updatetime=300
" Don't pass messages to |ins-completion-menu|.
set shortmess+=c
" Always show the signcolumn, otherwise it would shift the text each time
" diagnostics appear/become resolved.
if has("nvim-0.5.0") || has("patch-8.1.1564")
" Recently vim can merge signcolumn and number column into one
set signcolumn=number
else
set signcolumn=yes
endif
" Use tab for trigger completion with characters ahead and navigate.
" NOTE: Use command ':verbose imap <tab>' to make sure tab is not mapped by
" other plugin before putting this into your config.
inoremap <silent><expr> <TAB>
\ pumvisible() ? "\<C-n>" :
\ <SID>check_back_space() ? "\<TAB>" :
\ coc#refresh()
inoremap <expr><S-TAB> pumvisible() ? "\<C-p>" : "\<C-h>"
function! s:check_back_space() abort
let col = col('.') - 1
return !col || getline('.')[col - 1] =~# '\s'
endfunction
" Use <c-space> to trigger completion.
if has('nvim')
inoremap <silent><expr> <c-space> coc#refresh()
else
inoremap <silent><expr> <c-@> coc#refresh()
endif
" Make <CR> auto-select the first completion item and notify coc.nvim to
" format on enter, <cr> could be remapped by other vim plugin
inoremap <silent><expr> <cr> pumvisible() ? coc#_select_confirm()
\: "\<C-g>u\<CR>\<c-r>=coc#on_enter()\<CR>"
" Use `[g` and `]g` to navigate diagnostics
" Use `:CocDiagnostics` to get all diagnostics of current buffer in location list.
nmap <silent> [g <Plug>(coc-diagnostic-prev)
nmap <silent> ]g <Plug>(coc-diagnostic-next)
" GoTo code navigation.
nmap <silent> gd <Plug>(coc-definition)
nmap <silent> gy <Plug>(coc-type-definition)
nmap <silent> gi <Plug>(coc-implementation)
nmap <silent> gr <Plug>(coc-references)
" Use K to show documentation in preview window.
nnoremap <silent> K :call <SID>show_documentation()<CR>
function! s:show_documentation()
if (index(['vim','help'], &filetype) >= 0)
execute 'h '.expand('<cword>')
elseif (coc#rpc#ready())
call CocActionAsync('doHover')
else
execute '!' . &keywordprg . " " . expand('<cword>')
endif
endfunction
" Highlight the symbol and its references when holding the cursor.
autocmd CursorHold * silent call CocActionAsync('highlight')
" Symbol renaming.
nmap <leader>rn <Plug>(coc-rename)
" Formatting selected code.
xmap <leader>f <Plug>(coc-format-selected)
nmap <leader>f <Plug>(coc-format-selected)
augroup mygroup
autocmd!
" Setup formatexpr specified filetype(s).
autocmd FileType typescript,json setl formatexpr=CocAction('formatSelected')
" Update signature help on jump placeholder.
autocmd User CocJumpPlaceholder call CocActionAsync('showSignatureHelp')
augroup end
" Applying codeAction to the selected region.
" Example: `<leader>aap` for current paragraph
xmap <leader>a <Plug>(coc-codeaction-selected)
nmap <leader>a <Plug>(coc-codeaction-selected)
" Remap keys for applying codeAction to the current buffer.
nmap <leader>ac <Plug>(coc-codeaction)
" Apply AutoFix to problem on the current line.
nmap <leader>qf <Plug>(coc-fix-current)
" Map function and class text objects
" NOTE: Requires 'textDocument.documentSymbol' support from the language server.
xmap if <Plug>(coc-funcobj-i)
omap if <Plug>(coc-funcobj-i)
xmap af <Plug>(coc-funcobj-a)
omap af <Plug>(coc-funcobj-a)
xmap ic <Plug>(coc-classobj-i)
omap ic <Plug>(coc-classobj-i)
xmap ac <Plug>(coc-classobj-a)
omap ac <Plug>(coc-classobj-a)
" Remap <C-f> and <C-b> for scroll float windows/popups.
if has('nvim-0.4.0') || has('patch-8.2.0750')
nnoremap <silent><nowait><expr> <C-f> coc#float#has_scroll() ? coc#float#scroll(1) : "\<C-f>"
nnoremap <silent><nowait><expr> <C-b> coc#float#has_scroll() ? coc#float#scroll(0) : "\<C-b>"
inoremap <silent><nowait><expr> <C-f> coc#float#has_scroll() ? "\<c-r>=coc#float#scroll(1)\<cr>" : "\<Right>"
inoremap <silent><nowait><expr> <C-b> coc#float#has_scroll() ? "\<c-r>=coc#float#scroll(0)\<cr>" : "\<Left>"
vnoremap <silent><nowait><expr> <C-f> coc#float#has_scroll() ? coc#float#scroll(1) : "\<C-f>"
vnoremap <silent><nowait><expr> <C-b> coc#float#has_scroll() ? coc#float#scroll(0) : "\<C-b>"
endif
" Use CTRL-S for selections ranges.
" Requires 'textDocument/selectionRange' support of language server.
nmap <silent> <C-s> <Plug>(coc-range-select)
xmap <silent> <C-s> <Plug>(coc-range-select)
" Add `:Format` command to format current buffer.
command! -nargs=0 Format :call CocAction('format')
" Add `:Fold` command to fold current buffer.
command! -nargs=? Fold :call CocAction('fold', <f-args>)
" Add `:OR` command for organize imports of the current buffer.
command! -nargs=0 OR :call CocAction('runCommand', 'editor.action.organizeImport')
" Add (Neo)Vim's native statusline support.
" NOTE: Please see `:h coc-status` for integrations with external plugins that
" provide custom statusline: lightline.vim, vim-airline.
set statusline^=%{coc#status()}%{get(b:,'coc_current_function','')}
" Mappings for CoCList
" Show all diagnostics.
nnoremap <silent><nowait> <space>a :<C-u>CocList diagnostics<cr>
" Manage extensions.
nnoremap <silent><nowait> <space>e :<C-u>CocList extensions<cr>
" Show commands.
nnoremap <silent><nowait> <space>c :<C-u>CocList commands<cr>
" Find symbol of current document.
nnoremap <silent><nowait> <space>o :<C-u>CocList outline<cr>
" Search workspace symbols.
nnoremap <silent><nowait> <space>s :<C-u>CocList -I symbols<cr>
" Do default action for next item.
nnoremap <silent><nowait> <space>j :<C-u>CocNext<CR>
" Do default action for previous item.
nnoremap <silent><nowait> <space>k :<C-u>CocPrev<CR>
" Resume latest coc list.
nnoremap <silent><nowait> <space>p :<C-u>CocListResume<CR>
五叁怪、彩蛋
smile
在 vim 中運(yùn)行 :smile
:smile
oooo$$$$$$$$$$$$oooo
oo$$$$$$$$$$$$$$$$$$$$$$$$o
oo$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$o o$ $$ o$
o $ oo o$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$o $$ $$ $$o$
oo $ $ "$ o$$$$$$$$$ $$$$$$$$$$$$$ $$$$$$$$$o $$$o$$o$
"$$$$$$o$ o$$$$$$$$$ $$$$$$$$$$$ $$$$$$$$$$o $$$$$$$$
$$$$$$$ $$$$$$$$$$$ $$$$$$$$$$$ $$$$$$$$$$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$$$$$$ $$$$$$$$$$$$$ $$$$$$$$$$$$$$ """$$$
"$$$""""$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ "$$$
$$$ o$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ "$$$o
o$$" $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ $$$o
$$$ $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$" "$$$$$$ooooo$$$$o
o$$$oooo$$$$$ $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ o$$$$$$$$$$$$$$$$$
$$$$$$$$"$$$$ $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ $$$$""""""""
"""" $$$$ "$$$$$$$$$$$$$$$$$$$$$$$$$$$$" o$$$
"$$$o """$$$$$$$$$$$$$$$$$$"$$" $$$
$$$o "$$""$$$$$$"""" o$$$
$$$$o o$$$"
"$$$$o o$$$$$$o"$$$$o o$$$$
"$$$$$oo ""$$$$o$$$$$o o$$$$""
""$$$$$oooo "$$$o$$$$$$$$$"""
""$$$$$$$oo $$$$$$$$$$
""""$$$$$$$$$$$
$$$$$$$$$$$$
$$$$$$$$$$"
"$$$""""
Kill Sheep 小游戲
針對(duì) vim 8.2 版本审葬,可在 Windows 系統(tǒng)中安裝 gvim 8.2。
進(jìn)入 C:\Users\xxx\vimfiles\pack\vendor\start
路徑下(沒有就創(chuàng)建)奕谭,clone 源代碼:
git clone https://github.com/vim/killersheep.git
打開 gvim涣觉,最大化,運(yùn)行 :KillKillKill
命令即可進(jìn)入游戲血柳。