Neovim 的配置与使用

发布时间 2023-06-14 10:18:50作者: zwyyy456

LazyVim

参考Lazyvim的官方安装教程即可安装,要求系统已经安装好了npm

You can find a starter template for LazyVim here

Macos or Linux

Install the LazyVim Starter

  • Make a backup of your current Neovim files:

    # required
    mv ~/.config/nvim ~/.config/nvim.bak
    
    # optional but recommended
    mv ~/.local/share/nvim ~/.local/share/nvim.bak
    mv ~/.local/state/nvim ~/.local/state/nvim.bak
    mv ~/.cache/nvim ~/.cache/nvim.bak
    
  • Clone the starter

    git clone https://github.com/LazyVim/starter ~/.config/nvim
    
  • Remove the .git folder, so you can add it to your own repo later

    rm -rf ~/.config/nvim/.git
    
  • Start Neovim!

    nvim
    

    Refer to the comments in the files on how to customize LazyVim.

windows

Install the LazyVim Starter with PowerShell

  • Make a backup of your current Neovim files:

    # required
    Move-Item $env:LOCALAPPDATA\nvim $env:LOCALAPPDATA\nvim.bak
    
    # optional but recommended
    Move-Item $env:LOCALAPPDATA\nvim-data $env:LOCALAPPDATA\nvim-data.bak
    
  • Clone the starter

    git clone https://github.com/LazyVim/starter $env:LOCALAPPDATA\nvim
    
  • Remove the .git folder, so you can add it to your own repo later

    Remove-Item $env:LOCALAPPDATA\nvim\.git -Recurse -Force
    
  • Start Neovim!

    nvim
    

    Refer to the comments in the files on how to customize LazyVim.

Docker

docker run -w /root -it --rm alpine:edge sh -uelic '
  apk add git lazygit neovim ripgrep alpine-sdk --update
  git clone https://github.com/LazyVim/starter ~/.config/nvim
  cd ~/.config/nvim
  nvim
'

It is recommended to run :checkhealth after installation

Sublime text4 使用 NeoVintageous插件

Sublime text4中安装该插件

启用ctrl + [作为esc

点击sublime Text -> settings ->settings,编辑右侧的配置文件,添加"vintage_ctrl_keys": true,

启用jkesc

点击sublime Text -> settings ->settings,编辑右侧的配置文件,添加"vintageous_i_escape_jk": true,

使yy会复制到系统剪贴板

点击sublime Text -> settings ->settings,编辑右侧的配置文件,添加"vintageous_use_sys_clipboard": true,

vscode使用vscode.neovim插件

高亮异常问题

修改~/.config/nvim/init.lua为,由于该插件需要基于本机的nvim

if vim.g.vscode then
    -- Vscode extension
else
    -- bootstrap lazy.vim, LazyVim and your plugins
    require("config.lazy")
end

系统剪贴板同步问题

win11

插件设置:Settings-user页面,Neovim Executable Paths: Win32内容设置为C:\Users\zwyyy\scoop\apps\neovim\current\bin\nvim.exeNeovim Init Vim Paths: Win32内容无需设置。

为了使vscode中yank的内容与系统剪贴板同步,在~/AppData/Local/nvim目录下坐如下操作:

  • 修改init.lua内容如下:
    if vim.g.vscode then
        -- Vscode extension
        require("vscode.config.options")
    else
        -- bootstrap lazy.vim, LazyVim and your plugins
        require("config.lazy")
    end
    
  • lua目录下新建vscode/configvscode/plugins两个文件夹,同时在vscode/config文件夹下新建options.lua文件,文件内容如下:
    local opt = vim.opt
    opt.clipboard = "unnamedplus" -- 复制会复制到系统剪贴板
    

wsl

插件设置:Settings-Remote [WSL:Debian]页面,Neovim Executable Paths: Linux内容设置为/usr/bin/nvimNeovim Init Vim Paths: Linux内容无需设置。

key bindings中查找ctrl+c,删除与neovim有关的两项,否则会导致插入模式下无法使用ctrl+c复制

设置jkesc

编辑keybindings.json,添加

{
    "command": "vscode-neovim.compositeEscape2",
    "key": "j",
    "when": "neovim.mode == insert && editorTextFocus",
    "args": "j"
}
{
    "command": "vscode-neovim.compositeEscape2",
    "key": "k",
    "when": "neovim.mode == insert && editorTextFocus",
    "args": "k"
}

使用gc作为注释快捷键

vscode.neovim官方给出了基于init.vim修改方法,即在init.vim中添加以下内容

xmap gc  <Plug>VSCodeCommentary
nmap gc  <Plug>VSCodeCommentary
omap gc  <Plug>VSCodeCommentary
nmap gcc <Plug>VSCodeCommentaryLine

这里我们修改为基于lua的: 在lua/vscode/config目录下新建keymaps.lua文件,内容修改为

local keymap = vim.keymap
-- local opts = { noremap = true, silent = true }
-- vscode.neovim用于注释代码的按键映射
keymap.set("v", "gc", "<Plug>VSCodeCommentary")
keymap.set("n", "gc", "<Plug>VSCodeCommentary")
keymap.set("o", "gc", "<Plug>VSCodeCommentary")
keymap.set("n", "gcc", "<Plug>VSCodeCommentaryLine")

同时修改init.lua,添加require("vscode.config.keymaps")

Neovim配置

win11

keymap配置

Insert模式下按下jk退出Insert模式,将~/AppData/Local/nvim/config/keymap.lua中文件内容修改为:

local keymap = vim.keymap
keymap.set("i", "jk", "<ESC>")

WSL

同步系统剪贴板

执行sudo apt install xclip以安装xclip,在~/.config/nvim/lua/vscode/config/options.lua中,修改内容为:

local opt = vim.opt
opt.clipboard = "unnamedplus" -- Sync with system clipboard

keymap配置

同Windows,注意keymap.lua文件的位置就好了。

设置tab-size为4

修改~/.config/nvim/lua/config/optionas.lua,追加内容

opt.tabstop = 4
opt.shiftwidth = 4

杂项

<S-Tab>已经被lazyvim设置于为Go to left windows

切换tab

备份

文件存档于github-nvim-config,理论上安装lazyvim之后pull即可。

参考

NeoVintageous-clipboard NeoVintageous-jk Vscode.neovim Vscode.neovim-gc-comment