我曾经多次向人推荐Vim,其热情程度有些类似现在卖保险的,有的时候,人们会因为一些弥足珍贵的美好暗暗渴望一个巨大的负面,比如因为想重温手动挡的快乐而渴望买下一辆二十万公里的老爷车,比如因为所谓完美的音质而舍不得一个老旧的有线耳机,比如因为一个铜炉火锅而期待北京那漫长而寒冷的冬天。
(相关资料图)
也许有的人会因为Vim而放弃169刀的JetBrains全家桶,没错,Vim的快乐,就是手动挡的快乐,懂得自然懂,不懂的永远也不会懂,但如果没有用Vim敲过代码,那么绝对枉生于有Vim的世界。
之前一篇:上古神兵,先天至宝,Win11平台安装和配置NeoVim0.8.2编辑器搭建Python3开发环境(2023最新攻略),我们已经配置好了Python3开发环境,本次继续添砖加瓦,让NeoVim进化为全栈编辑器,全知全能,无所不通。
之前配置Python补全,我们使用过NCM2扩展插件:
Plug "ncm2/ncm2"Plug "roxma/nvim-yarp"Plug "ncm2/ncm2-bufword"Plug "ncm2/ncm2-path"Plug "ncm2/ncm2-jedi"
五个插件,仅仅为了Python的补全,而Coc.nvim 通过 Microsoft 的 Language Server Protocol,支持许多编程语言,包括 JavaScript, Python, C++ ,Ruby等等。同时还可以通过设置和扩展进行灵活定制,满足不同用户的需求。
重新编写配置:
Plug "neoclide/coc.nvim", {"branch": "release"}
安装插件:
:PlugInstall
安装Python补全:
:CocInstall coc-pyls
就这么简单。
随后,还可以对其他目标语言进行设置,比如想支持Golang的补全,通过命令:
:CocConfig
打开配置文件,Win11默认路径是:~\AppData\Local\nvim\coc-settings.json
{ \"languageserver\": { \"golang\": { \"command\": \"gopls\", \"rootPatterns\": [ \"go.mod\" ], \"filetypes\": [ \"go\" ] } }, \"suggest.noselect\": false, \"coc.preferences.diagnostic.displayByAle\": true, \"suggest.floatEnable\": true}
添加Golang的配置,这里使用gopls模块。
正确配置之后,就可以使用代码补全了 例如我们输入 fmt. 就会提示fmt包中的方法,默认选择第一个,使用< C-n >< C-p >上下选择,回车确认,nvim下可以使用悬浮窗功能。
类似的,如果想配置Ruby的智能提示,设置不需要配置文件,只需要安装对应模块即可:
gem install solargraph
随后NeoVim内运行命令:
:CocInstall coc-solargraph
但这也带来了一个问题,即编译运行的时候,默认运行的语言是Python,如何让Vim程序自动进行判断?只需要修改配置即可:
autocmd FileType python nnoremap :sp :term python % autocmd FileType go nnoremap :sp :term go run % nnoremap :bd!
这里通过NeoVim中的autocmd进行判断,如果是Python代码就通过python解释器运行,如果是golang代码就通过Golang的编译器进行编译,互不影响。
NeoVim 的 autocmd 是用来自动执行命令的一种机制。它可以在特定的事件发生时触发命令的执行,比如打开文件、保存文件等。这样可以自动地对文件进行格式化、添加头部信息等操作。
前端的补全更简单,一键式命令安装即可:
:CocInstall coc-vetur coc-json coc-html coc-css
但前端页面默认是没有闭合高亮的,所以推荐下面这个插件:
Plug "leafOfTree/vim-matchtag"
它可以针对前端页面标签的闭合进行动态高亮:
非常方便。
也许有人会因为诸如保存、注释以及记录等操作还需要输入vim命令而苦恼,但其实这并不是什么问题,Vim也可以自动保存:
Plug "Pocco81/auto-save.nvim"
这样就可以免去:w的操作。
单行以及多行的批量注释可以依赖这个插件:
Plug "tpope/vim-commentary"
这样就可以通过组合键gc快速进行注释操作了。
编辑操作记录可以依赖这个插件:
Plug "mhinz/vim-startify"
如此可以在首页动态的选择曾经编辑过的文件:
想要传统IDE那样的动态调节字体大小?
let s:fontsize = 12function! AdjustFontSize(amount) let s:fontsize = s:fontsize+a:amount :execute \"GuiFont! Consolas:h\" . s:fontsizeendfunctioninoremap pumvisible() ? \"\\" : \"\\"inoremap pumvisible() ? \"\\" : \"\\"inoremap pumvisible() ? \"\\" : \"\\"inoremap pumvisible() ? \"\\" : \"\\"
通过tab键选择自动补全的代码提示?
\" In insert mode, pressing ctrl + numpad"s+ increases the fontinoremap :call AdjustFontSize(1)ainoremap :call AdjustFontSize(-1)a
在Vim中,你甚至可以和ChatGpt一亲芳泽:
use({ "terror/chatgpt.nvim", run = "pip3 install -r requirements.txt"})
当然,在用户目录下需要chatgpt的apikey或者token: ~/.chatgpt-nvim.json:
{ \"authorization\": \"\", # Optional API key \"session_token\": \"\" # Your ChatGPT session token}
由于api-key是收费的,这里建议使用token:
访问 https://chat.openai.com/chat 并且登录 按F12打开开发者工具 在应用的标签上 >选择Cookies 直接复制__Secure-next-auth.session-token的value值写到上面的session_token中即可。
效果如下:
最后,完整的全栈NeoVim配置:
call plug#begin("C:\nvim-win64\nvim-win64\share\nvim\plugged")Plug "navarasu/onedark.nvim"Plug "pablopunk/native-sidebar.vim"Plug "Pocco81/auto-save.nvim"Plug "leafOfTree/vim-matchtag"Plug "mhinz/vim-startify"Plug "neoclide/coc.nvim", {"branch": "release"}Plug "tpope/vim-commentary"call plug#end()let g:onedark_config = { \ "style": "warm",\}colorscheme onedarklet g:native_sidebar_shortcut = ""set clipboard^=unnamed,unnamedplussyntax on \"syntax highlighting, see :help syntaxfiletype plugin indent on \"file type detection, see :help filetypeset number \"display line numberset path+=** \"improves searching, see :help pathset noswapfile \"disable use of swap filesset wildmenu \"completion menuset backspace=indent,eol,start \"ensure proper backspace functionalityset undodir=~/.cache/nvim/undo \"undo ability will persist after exiting fileset undofile \"see :help undodir and :help undofileset incsearch \"see results while search is being typed, see :help incsearchset smartindent \"auto indent on new lines, see :help smartindentset ic \"ignore case when searchingset expandtab \"expanding tab to spacesset tabstop=4 \"setting tab to 4 columnsset shiftwidth=4 \"setting tab to 4 columnsset softtabstop=4 \"setting tab to 4 columnsset showmatch \"display matching bracket or parenthesisset hlsearch incsearch \"highlight all pervious search pattern with incsearchhighlight ColorColumn ctermbg=9 \"display ugly bright red bar at color column number\" Keybind Ctrl+l to clear searchnnoremap :nohl:echo \"Search Cleared\"\" When python filetype is detected, F5 can be used to execute script \" autocmd FileType python nnoremap ::exec "!python" shellescape(expand("%:p"), 1)autocmd FileType python nnoremap :sp :term python % autocmd FileType go nnoremap :sp :term go run % nnoremap :bd!let s:fontsize = 12function! AdjustFontSize(amount) let s:fontsize = s:fontsize+a:amount :execute \"GuiFont! Consolas:h\" . s:fontsizeendfunctioninoremap pumvisible() ? \"\\" : \"\\"inoremap pumvisible() ? \"\\" : \"\\"inoremap pumvisible() ? \"\\" : \"\\"inoremap pumvisible() ? \"\\" : \"\\"\" In insert mode, pressing ctrl + numpad"s+ increases the fontinoremap :call AdjustFontSize(1)ainoremap :call AdjustFontSize(-1)a
只需要不到70行的配置,我们就拥有了一个万能的Vim编辑器。
满打满算,七个插件,全知全能,而我们需要做的,只是一行简单的:PlugInstall。因为什么?因为热爱,如果是真爱,哪怕风情万千遇到不解风情,也所甘愿,哪怕没人懂,也要周周至至做出来。
Copyright 2015-2022 西南礼仪网 版权所有 备案号:皖ICP备2022009963号-8 联系邮箱:39 60 29 14 2@qq.com