Vim使用指南

本文记录一下从IDE转为Vim的一些资料和教程:

  1. Neovim配置实战:掘金小册,30块
  2. NeoVim 基本配置 - 某春雨的后花园
  3. 我的现代化 NeoVim 配置介绍/教程 - 知乎
  4. 我的现代化Neovim配置 - 知乎
  5. 入门指南 | SpaceVim
  6. 学习 Neovim 全 lua 配置 - 知乎:Neovim配置实战的旧版,免费
  7. Learn Neovim The Practical Way. All articles on how to configure and… | by alpha2phi | Medium:medium上关于neovim配置的系列文章
  8. GitHub - LunarVim/Neovim-from-scratch: 📚 A Neovim config designed from scratch to be understandable:Lunar官方配置教程
  9. Neovim IDE from Scratch:Neovim配置视频教程
  10. LunarVim | LunarVim:neovim的变种,便于配置
  11. Home | SpaceVim:neovim的变种,便于配置
  12. Getting Started | AstroNvim:neovim的变种,便于配置
  13. Interactive Vim tutorial:在线vim交互式教程。
  14. [GitHub - fengstats/vscode-settings: my vscode settings and theme config]:VSCode | 日常工作流分享_哔哩哔哩_bilibili中分享的配置
  15. GitHub - LintaoAmons/CoolStuffes: 我的分享放这里了,大家随便拿去用啊,记得给个星星就行啦~:b站up主的配置
  16. NvChad:NvChad,另一个neovim的变种。
  17. 玩转Vim 从放弃到爱不释手-Vim入门-慕课网:b站PegasusWang出品的免费Vim慕课教程
  18. vim tips and tricks: home:Vim在线交互学习网站。
  19. Vim Tips Wiki | Fandom:vim tips
  20. Vim Cheat Sheet:vim常用命令大全

碰到的一些问题的解决方法

  1. No C compiler found · Issue #274 · LunarVim/Neovim-from-scratch · GitHub

neovim配置步骤

借鉴配置

可供借鉴的配置有:

  1. GitHub - nshen/learn-neovim-lua: Neovim 配置实战:从 0 到 1 打造自己的 IDE
  2. GitHub - leslie255/nvim-config: A pretty epic NeoVim setup
  3. GitHub - ayamir/nvimdots: A well configured and structured Neovim.
    此处以第3个配置为例,介绍配置流程。(因为第3个配置最为复杂,且使用较为丰富的插件,和更小众、更新型的插件管理工具lazy.nvim。
    此处,先把dotfiles仓库中的对应文件夹映射到~/.config/nvim
ln -s /mnt/d/Desktop/dotfiles/nvim2 ~/.config/nvim

安装lazy.vim插件管理器

  1. 直接在终端运行nvim init.lua,让neovim自动安装,出现如下报错,即无法从github clone lazy.vim进行安装。

    原因:git出现问题,需要进行配置。
    解决方案:向github账户添加ssh验证。
git config --global user.name "你的github账户名"
git config --global user.email "你的github账户默认的邮箱地址"
ssh-keygen -t rsa -b 4096 -C "你的github账户默认的邮箱地址"
cat ~/.ssh/id_rsa.pub # 添加到git ssh
ssh-keyscan -t rsa github.com >> ~/.ssh/known_hosts 
  1. 进行上述配置后,再次运行nvim init.lua,会发现如下错误,即efm-langserver无法安装:

    原因:efm-langserver使用go语言编写,其使用go的包管理工具进行安装。
    解决方案:安装go语言。
# 下载对应版本go压缩包后解压到指定目录
sudo tar -C /usr/local -xzf go1.20.linux-amd64.tar.gz
# 将对应目录添加至PATH环境变量,写入~/.bashrc使其永久生效
export PATH=$PATH:/usr/local/go/bin
  1. 再次运行nvim init.lua,发现仍然无法安装。
    原因:go仓库源在国内无法稳定访问
    解决方案:配置国内镜像源,在.bashrc文件中写入:
# 启用 Go Modules 功能
go env -w GO111MODULE=on

# 配置 GOPROXY 环境变量,以下三选一

# 1. 七牛 CDN
go env -w  GOPROXY=https://goproxy.cn,direct
  1. 再次运行nvim init.lua,发现仍然无法安装。报如下错误:
Can only use path@version syntax with 'go get'?

原因:go1.16以下版本无法通过go install path@version安装。
解决方案:卸载掉apt-get默认安装的go1.13版本,手动安装go1.20并添加到指定路径(也可以手动安装efm和gopls):

export PATH=$PATH:/usr/local/go/bin
# 启用 Go Modules 功能
go env -w GO111MODULE=on
# 2. 阿里云
go env -w GOPROXY=https://mirrors.aliyun.com/goproxy/,direct

参考资料:

  1. Can only use path@version syntax with ‘go get’? · Issue #220 · mattn/efm-langserver · GitHub
  2. efm-langserver: Error while opening a new .cc file · Issue #98 · ayamir/nvimdots · GitHub

配置主题

以上述安装的配置为例。

  1. 在对应配置文件lua/ui/plugins.lua中添加主题:
ui["folke/tokyonight.nvim"] = {
	lazy = false,
	name = "tokyonight", 
	config = conf.tokyonight,
}
  1. 在对应配置文件中lazy-lock.json中添加主题仓库信息:
"tokyonight": { "branch": "main", "commit": "affb21a81e6d7de073378eb86d02864c594104d9" },
  1. lua/core/settings.lua中将colorsheme设置为tokyonight
settings["colorscheme"] = "tokyonight"

neovim配置java环境

  1. 下载解压jdt-language-server:
#创建workspace目录,后面会用到
mkdir -p ~/.local/share/nvim/lsp/jdt-language-server/workspace/folder
cd ~/.local/share/nvim/lsp/jdt-language-server
# 下载jdt-language-server-xxxxx.tar.gz
wget https://download.eclipse.org/jdtls/milestones/1.9.0/jdt-language-server-1.9.0-202203031534.tar.gz
# 解压
tar -zxvf jdt-language-server-1.9.0-202203031534.tar.gz
  1. 创建/lua/modules/lang/usr/文件夹,并添加config.luaplugins.lua两个文件,文件内容如下:
    config.lua:
local config = {}

function config.java()
	local opts = {
		cmd = {
			"java", 
			"-Declipse.application=org.eclipse.jdt.ls.core.id1",
			"-Dosgi.bundles.defaultStartLevel=4",
			"-Declipse.product=org.eclipse.jdt.ls.core.product",
			"-Dlog.protocol=true",
			"-Dlog.level=ALL",
			"-Xms1g",
			"--add-modules=ALL-SYSTEM",
			"--add-opens",
			"java.base/java.util=ALL-UNNAMED",
			"--add-opens",
			"java.base/java.lang=ALL-UNNAMED",
			"-jar",
			"/home/fg/.local/share/nvim/lsp/jdt-language-server/plugins/org.eclipse.equinox.launcher_1.6.400.v20210924-0641.jar",
			"-configuration",
			"/home/fg/.local/share/nvim/lsp/jdt-language-server/config_linux",
			"-data",
			"/home/fg/.local/share/nvim/lsp/jdt-language-server/workspace/folder",
		},
		root_dir = require("jdtls.setup").find_root({ ".git", "mvnw", "gradlew" }),
		settings = {
			java = {},
		},
		init_options = {
			bundles = {},
		},
	}
	require("jdtls").start_or_attach(opts)
end

return config

plugins.lua:

local custom = {}
local conf = require("modules.lang.user.config")

custom["mfussenegger/nvim-jdtls"] = {
	lazy = true,
	ft = "java",
	config = conf.java,
}

return custom
  1. lazy-lock.json添加仓库信息:
"nvim-jdtls": { "branch": "master", "commit": "1f640d14d17f20cfc63c1acc26a10f9466e66a75" },
  1. /lua/modules/editor/config.lua中添加java的语法高亮:
require("nvim-treesitter.configs").setup({
		ensure_installed = {
		"java",
		},
		})
  1. /lua/modules/editor/config.lua中添加java的debug配置:
-- local dap = require('dap')
dap.adapters.java = function(callback)
		callback({
			type = 'server';
			host = '127.0.0.1';
			port = port;
		})
	end

-- local dap = require('dap')
dap.configurations.java = {
  {
	classPaths = {},

	-- If using multi-module projects, remove otherwise.
	projectName = "yourProjectName",

	javaExec = "/usr/bin/java",
	mainClass = "your.package.name.MainClassName",
	modulePaths = {},
	name = "Launch YourClassName",
	request = "launch",
	type = "java"
  },
}	

参考资料:

  1. GitHub - lxyoucan/nvim-as-java-ide: 从零开始搭建Neovim Java IDE开发环境
  2. Usage · ayamir/nvimdots Wiki · GitHub
  3. Java · mfussenegger/nvim-dap Wiki · GitHub
  4. GitHub - mfussenegger/nvim-jdtls: Extensions for the built-in LSP support in Neovim for eclipse.jdt.ls

### Vim模式在中文输入法下的问题。 > 参考资料: > 1. [Vim模式在中文输入法下的问题 - 建议反馈 - Obsidian 中文论坛](https://forum-zh.obsidian.md/t/topic/11234/2)
### Obsidian vim模式配置 对Obsidian的`.obsidian.vimrc`文件配置`Ctrl`键(对Obsidian而言需要安装Vimrc Support插件)
"set some Ctrl- shortcuts"
nmap <C-[> <C-d> #映射为outdent,不能生效
imap <C-[> <C-d> #映射伪outdent,不能生效
PS: > 参考资料: > 1. [key bindings - How to disable Ctrl key? - Vi and Vim Stack Exchange](https://vi.stackexchange.com/questions/4060/how-to-disable-ctrl-key) > 2. [key bindings - Vim Stack Exchange](https://vi.stackexchange.com/questions/3225/disable-esc-but-keep-c/3570#3570) > 3. [GitHub - esm7/obsidian-vimrc-support: A plugin for the Obsidian.md note-taking software](https://github.com/esm7/obsidian-vimrc-support) > 4. [Can't map the Tab key w/o Mod Keys · Issue #48 · esm7/obsidian-vimrc-support · GitHub](https://github.com/esm7/obsidian-vimrc-support/issues/48) > 5. [vim tips and tricks: indenting](https://www.cs.swarthmore.edu/oldhelp/vim/indenting.html#:~:text=To%20indent%20the%20current%20line,by%20sw%20(repeat%20with%20.%20)&text=then%20try%20hitting%20the%20F5,or%20just%20%3Aset%20paste%20).)
### vim中yy复制到系统剪贴板 设置vim中的yy可以copy to clipboard: - Obsidian配置,在`.obsidian.vimrc`文件中添加:
"Yank to system clipboard"
set clipboard=unnamed #重启后生效
- VSCode配置,在settings中搜索`vim clip`点击生效: ![](https://raw.githubusercontent.com/Tom89757/ImageHost/main/hexo/1.png) > 参考资料: >1. [VS code VIM extension copy and paste - Stack Overflow](https://stackoverflow.com/questions/58306002/vs-code-vim-extension-copy-and-paste) >2. [How to copy to clipboard in Vim? - Stack Overflow](https://stackoverflow.com/questions/3961859/how-to-copy-to-clipboard-in-vim) >3. [GitHub - esm7/obsidian-vimrc-support: A plugin for the Obsidian.md note-taking software](https://github.com/esm7/obsidian-vimrc-support)
### Vim中各种map模式nmap/imap等详解: > 参考资料: > 1. [What is the difference between the remap, noremap, nnoremap and vnoremap mapping commands in Vim? - Stack Overflow](https://stackoverflow.com/questions/3776117/what-is-the-difference-between-the-remap-noremap-nnoremap-and-vnoremap-mapping) > 2. [key bindings - What is the difference between unmap and mapping to nop? - Vi and Vim Stack Exchange](https://vi.stackexchange.com/questions/16392/what-is-the-difference-between-unmap-and-mapping-to-nop?noredirect=1)
### `Ctrl`/`Shift`/方向键/Fn键的映射:

参考资料:

  1. vim - Map Shift + F3 in .vimrc - Super User
  2. vim: how to specify arrow keys - Stack Overflow

### `.vimrc`配置文件中``的含义: > 参考资料: > 1. [What is the meaning of a CR at the end of some vim mappings? - Stack Overflow](https://stackoverflow.com/questions/22142755/what-is-the-meaning-of-a-cr-at-the-end-of-some-vim-mappings)

### Ubuntu和wsl中vim配置 Ubuntu和wsl中进行基础的`.vimrc`配置:

参考资料:

  1. A basic .vimrc file that will serve as a good template on which to build. · GitHub
  2. ubuntu下vim的配置vimrc-掘金
  3. vim - Error Trailing Characters in Ubuntu - Stack Overflow
  4. Vim cursor can’t be changed · Issue #4335 · microsoft/terminal · GitHub
  5. Changing Vim cursor in Windows Terminal : vim