关于vim:千姿百态瞬息万变Win11系统NeoVim打造全能全栈编辑器前端GolangRubyChatGpt

1次阅读

共计 5164 个字符,预计需要花费 13 分钟才能阅读完成。

我已经屡次向人举荐 Vim,其激情水平有些相似当初卖保险的,有的时候,人们会因为一些弥足珍贵的美妙暗暗渴望一个微小的负面,比方因为想重温手动挡的高兴而渴望买下一辆二十万公里的老爷车,比方因为所谓完满的音质而舍不得一个老旧的有线耳机,比方因为一个铜炉火锅而期待北京那漫长而凛冽的冬天。

兴许有的人会因为 Vim 而放弃 169 刀的 JetBrains 全家桶,没错,Vim 的高兴,就是手动挡的高兴,懂得天然懂,不懂的永远也不会懂,但如果没有用 Vim 敲过代码,那么相对枉生于有 Vim 的世界。

之前一篇:上古神兵, 先天至宝,Win11 平台装置和配置 NeoVim0.8.2 编辑器搭建 Python3 开发环境 (2023 最新攻略),咱们曾经配置好了 Python3 开发环境,本次持续添砖加瓦,让 NeoVim 进化为全栈编辑器,全知全能,无所不通。

全能补全:coc.nvim

之前配置 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 <C-B> :sp <CR> :term python % <CR>  
autocmd FileType go nnoremap <C-B> :sp <CR> :term go run % <CR>  
nnoremap <C-W> :bd!<CR>

这里通过 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 = 12  
function! AdjustFontSize(amount)  
  let s:fontsize = s:fontsize+a:amount  
  :execute "GuiFont! Consolas:h" . s:fontsize  
endfunction  
  
inoremap <expr> <TAB> pumvisible() ? "\<C-y>" : "\<CR>"  
inoremap <expr> <Esc> pumvisible() ? "\<C-e>" : "\<Esc>"  
inoremap <expr> <C-j> pumvisible() ? "\<C-n>" : "\<Down>"  
inoremap <expr> <C-k> pumvisible() ? "\<C-p>" : "\<Up>"

通过 tab 键抉择主动补全的代码提醒?

"In insert mode, pressing ctrl + numpad's+ increases the font  
inoremap <C-kPlus> <Esc>:call AdjustFontSize(1)<CR>a  
inoremap <C-kMinus> <Esc>:call AdjustFontSize(-1)<CR>a

在 Vim 中,你甚至能够和 ChatGpt 一亲芳泽:

use({  
  'terror/chatgpt.nvim',  
  run = 'pip3 install -r requirements.txt'  
})

当然,在用户目录下须要 chatgpt 的 apikey 或者 token:~/.chatgpt-nvim.json:

{  
  "authorization": "<API-KEY>",      # Optional API key  
  "session_token": "<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 onedark  
  
  
let g:native_sidebar_shortcut = '<c-t>'  
  
set clipboard^=unnamed,unnamedplus  
  
syntax on                       "syntax highlighting, see :help syntax  
filetype plugin indent on       "file type detection, see :help filetype  
set number                      "display line number  
set path+=**                    "improves searching, see :help path  
set noswapfile                  "disable use of swap files  
set wildmenu                    "completion menu  
set backspace=indent,eol,start  "ensure proper backspace functionality  
set undodir=~/.cache/nvim/undo  "undo ability will persist after exiting file  
set undofile                    "see :help undodir and :help undofile  
set incsearch                   "see results while search is being typed, see :help incsearch  
set smartindent                 "auto indent on new lines, see :help smartindent  
set ic                          "ignore case when searching  
  
set expandtab                   "expanding tab to spaces  
set tabstop=4                   "setting tab to 4 columns  
set shiftwidth=4                "setting tab to 4 columns  
set softtabstop=4               "setting tab to 4 columns  
set showmatch                   "display matching bracket or parenthesis  
set hlsearch incsearch          "highlight all pervious search pattern with incsearch  
  
highlight ColorColumn ctermbg=9 "display ugly bright red bar at color column number" Keybind Ctrl+l to clear search  
nnoremap <C-l> :nohl<CR><C-l>:echo "Search Cleared"<CR>  
  
"When python filetype is detected, F5 can be used to execute script" autocmd FileType python nnoremap <buffer> <c-b> :<cr>:exec '!python' shellescape(expand('%:p'), 1)<cr>  
  
autocmd FileType python nnoremap <C-B> :sp <CR> :term python % <CR>  
autocmd FileType go nnoremap <C-B> :sp <CR> :term go run % <CR>  
nnoremap <C-W> :bd!<CR>  
  
  
let s:fontsize = 12  
function! AdjustFontSize(amount)  
  let s:fontsize = s:fontsize+a:amount  
  :execute "GuiFont! Consolas:h" . s:fontsize  
endfunction  
  
inoremap <expr> <TAB> pumvisible() ? "\<C-y>" : "\<CR>"  
inoremap <expr> <Esc> pumvisible() ? "\<C-e>" : "\<Esc>"  
inoremap <expr> <C-j> pumvisible() ? "\<C-n>" : "\<Down>"  
inoremap <expr> <C-k> pumvisible() ? "\<C-p>" : "\<Up>"  
  
"In insert mode, pressing ctrl + numpad's+ increases the font  
inoremap <C-kPlus> <Esc>:call AdjustFontSize(1)<CR>a  
inoremap <C-kMinus> <Esc>:call AdjustFontSize(-1)<CR>a

只须要不到 70 行的配置,咱们就领有了一个万能的 Vim 编辑器。

结语

满打满算,七个插件,全知全能,而咱们须要做的,只是一行简略的:PlugInstall。因为什么?因为酷爱,如果是真爱,哪怕风情万千遇到不解风情,也所甘心,哪怕没人懂,也要周周至至做进去。

正文完
 0