Vim使用指南
本文记录一下从IDE转为Vim的一些资料和教程:
- Neovim配置实战:掘金小册,30块
- NeoVim 基本配置 - 某春雨的后花园
- 我的现代化 NeoVim 配置介绍/教程 - 知乎
- 我的现代化Neovim配置 - 知乎
- 入门指南 | SpaceVim
- 学习 Neovim 全 lua 配置 - 知乎:Neovim配置实战的旧版,免费
- Learn Neovim The Practical Way. All articles on how to configure and… | by alpha2phi | Medium:medium上关于neovim配置的系列文章
- GitHub - LunarVim/Neovim-from-scratch: 📚 A Neovim config designed from scratch to be understandable:Lunar官方配置教程
- Neovim IDE from Scratch:Neovim配置视频教程
- LunarVim | LunarVim:neovim的变种,便于配置
- Home | SpaceVim:neovim的变种,便于配置
- Getting Started | AstroNvim:neovim的变种,便于配置
- Interactive Vim tutorial:在线vim交互式教程。
- [GitHub - fengstats/vscode-settings: my vscode settings and theme config]:VSCode | 日常工作流分享_哔哩哔哩_bilibili中分享的配置
- GitHub - LintaoAmons/CoolStuffes: 我的分享放这里了,大家随便拿去用啊,记得给个星星就行啦~:b站up主的配置
- NvChad:NvChad,另一个neovim的变种。
- 玩转Vim 从放弃到爱不释手-Vim入门-慕课网:b站PegasusWang出品的免费Vim慕课教程
- vim tips and tricks: home:Vim在线交互学习网站。
- Vim Tips Wiki | Fandom:vim tips
- Vim Cheat Sheet:vim常用命令大全
碰到的一些问题的解决方法
neovim配置步骤
借鉴配置
可供借鉴的配置有:
- GitHub - nshen/learn-neovim-lua: Neovim 配置实战:从 0 到 1 打造自己的 IDE
- GitHub - leslie255/nvim-config: A pretty epic NeoVim setup
- 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插件管理器
- 直接在终端运行
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
- 进行上述配置后,再次运行
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
- 再次运行
nvim init.lua
,发现仍然无法安装。
原因:go仓库源在国内无法稳定访问
解决方案:配置国内镜像源,在.bashrc
文件中写入:
# 启用 Go Modules 功能
go env -w GO111MODULE=on
# 配置 GOPROXY 环境变量,以下三选一
# 1. 七牛 CDN
go env -w GOPROXY=https://goproxy.cn,direct
- 再次运行
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
参考资料:
配置主题
以上述安装的配置为例。
- 在对应配置文件
lua/ui/plugins.lua
中添加主题:
ui["folke/tokyonight.nvim"] = {
lazy = false,
name = "tokyonight",
config = conf.tokyonight,
}
- 在对应配置文件中
lazy-lock.json
中添加主题仓库信息:
"tokyonight": { "branch": "main", "commit": "affb21a81e6d7de073378eb86d02864c594104d9" },
- 在
lua/core/settings.lua
中将colorsheme
设置为tokyonight
:
settings["colorscheme"] = "tokyonight"
neovim配置java环境
- 下载解压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
- 创建
/lua/modules/lang/usr/
文件夹,并添加config.lua
和plugins.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
- 在
lazy-lock.json
添加仓库信息:
"nvim-jdtls": { "branch": "master", "commit": "1f640d14d17f20cfc63c1acc26a10f9466e66a75" },
- 在
/lua/modules/editor/config.lua
中添加java
的语法高亮:
require("nvim-treesitter.configs").setup({
ensure_installed = {
"java",
},
})
- 在
/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"
},
}
### 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键的映射:
### `.vimrc`配置文件中`参考资料:
参考资料: