Vim Introduction
Vim 是什么
Vim - the ubiquitous text editor
Vim is a highly configurable text editor built to make creating and changing any kind of text very efficient. It is included as "vi" with most UNIX systems and with Apple OS X.
Vim is rock stable and is continuously being developed to become even better. Among its features are:
- persistent, multi-level undo tree
- extensive plugin system
- support for hundreds of programming languages and file formats
- powerful search and replace
- integrates with many tools
Vim 是一个多模态的文本编辑器。对于大多数的普通用户,在接触 Vim 之前使用的编辑器,基本都是单模态编辑器,对应的是 Vim 的「插入」模式。Vim 还有以下特点:
- 基于 TUI (Terminal User Interface)
- 熟练用户重度依赖键盘,少用、不用鼠标
- 高度可定制化,个人风格明显
- 编辑纯英文文本可以像闪电一样快速
- 编辑中文文本稍稍差了那么一点意思,频繁的中英输入切换和 Vim 的模态转换,容易产生击键错误,让人有「利刃在手、刀未出鞘」的迟滞感
Vim 能做什么
- 可以使用 Vim 来写 Markdown 吗?
- vim-markdown 插件
- 可以使用 Vim 来管理文件夹项目吗?
- 内置的 Netrw 文件管理器
- 启用了
wildmenu
的:e
命令 - NEEDTree
- Vinegar
- CtrlP 插件
Yes, it can. 这可能是当你问起任何关于“Vim 能做...吗?”的答案。
安装 Vim
下载和安装 Vim 9.1 至 C:\Program Files\Vim
位置,选择 English 为安装语言。
但不知何故(可能因为操作系统是中文版 Windows),安装成功之后,进入 Vim 界面仍然存在中文提示。例如,当进入 Insert mode 时,底部命令行的状态提示显示为 --插入--
。
若要将 Vim 界面语言设置为英文,需要在配置文件内 ~/.vimrc
添加如下配置:
" set the menu and the message to English
set langmenu=en_US
let $LANG='en_US'
source $VIMRUNTIME/delmenu.vim
source $VIMRUNTIME/menu.vim
添加 Vim 到环境变量
把 Vim 添加到环境变量,是为了在 PowerShell 命令行终端直接运行 vim <path\filename>
就可以直接打开或创建一个文件。
从官网下载后,安装在以下路径:
C:\Program Files\Vim\vim91
后来发现,在 Windows 系统中,只要之前安装过 Git,就会有 Vim 同步安装:
C:\Program Files\Git\usr\bin\vim.exe
Vim 配置文件
查看 Vim 版本:
:version
查看 Vim 安装路径和配置路径的方法:
:echo $VIM " C:\Program Files\Vim
:echo $HOME " C:\Users\Alowr
:echo $VIMRUNTIME " C:\Program Files\Vim\vim91
以上三个目录分别简称为:Vim 目录、家目录、Vim 运行时目录。
Vim 的配置文件 .vimrc
创建在家目录下。
周四 2025-07-03 重新配置
.vim
Directory Structure Overview
Your setup with vim-plug
handles most of the complexity for you by managing the plugged directory. The other directories are for your own custom configurations and scripts.
autoload/
- Purpose: This directory is for scripts that should only be loaded when they are needed (i.e., "on demand"). Vim automatically finds and loads functions defined in these scripts when they are called. This is a key optimization strategy to keep Vim's startup time fast.
vim-plug
itself uses this directory for its core script (autoload/plug.vim
).
- Purpose: This directory is for scripts that should only be loaded when they are needed (i.e., "on demand"). Vim automatically finds and loads functions defined in these scripts when they are called. This is a key optimization strategy to keep Vim's startup time fast.
colors/
- Purpose: For manually installed colorscheme files (e.g., my-theme.vim). When you run :colorscheme my-theme, Vim will look in this directory. Since you install your theme
gruvbox
withvim-plug
, it's managed insideplugged/
, but if you were to download a theme manually, this is where it would go.
- Purpose: For manually installed colorscheme files (e.g., my-theme.vim). When you run :colorscheme my-theme, Vim will look in this directory. Since you install your theme
plugged/
- Purpose: This is the default directory used by
vim-plug
to download, store, and manage all your plugins. You should not manually edit the contents of this directory; letvim-plug
handle it entirely.
- Purpose: This is the default directory used by
ftplugin/
(Filetype Plugin)- Purpose: To have settings that apply only to a specific filetype. For example, if you want to set shiftwidth=2 exclusively for JavaScript files, you would create a file named ~/.vim/ftplugin/javascript.vim and put setlocal shiftwidth=2 inside it.
indent/
- Purpose: Similar to ftplugin/, but specifically for indentation rules. If you wanted to define custom indentation logic for a filetype, you would place the corresponding script here (e.g., indent/my-language.vim).
syntax/
- Purpose: For adding or extending syntax highlighting rules. If you were creating your own language or wanted to improve highlighting for an existing one, you would place the syntax file here (e.g., syntax/my-language.vim).
plugin/
- Purpose: For scripts that you want to be loaded every time Vim starts, regardless of filetype. This is for general-purpose custom plugins or snippets that aren't managed by vim-plug.
after/
- Purpose: This is a special directory. Scripts here are loaded after all the default Vim scripts and plugins (including those in plugged/). This is very useful for overriding settings. For example, if a plugin in
plugged/
sets an option you don't like, you can override it by placing a script in~/.vim/after/plugin/my-overrides.vim
.
- Purpose: This is a special directory. Scripts here are loaded after all the default Vim scripts and plugins (including those in plugged/). This is very useful for overriding settings. For example, if a plugin in
undodir/
- Purpose: As you've already configured in your .vimrc, this directory stores persistent undo history files. This allows you to undo changes even after you've closed and reopened Vim.
Conclusion
Your current structure is clean and effective for a vim-plug-based setup. You are correctly using plugged/
for managed plugins and have a place for autoload
and colors
. You don't need to create all the other directories unless you have a specific need for them, like creating custom filetype settings or overriding a plugin's behavior.
配置文件夹的结构和作用:
~/.vimrc
:主配置文件~/.vim/
:插件配置目录~/.vim/autoload/
目录里面放置的是,当你真正需要的时候才被自动加载运行的文件,而不是在 Vim 启动时就加载~/.vim/autoload/plug.vim
插件管理器~/.vim/colors/
是用来存放 Vim 配色方案的~/.vim/plugin/
存放的是每次启动 Vim 都会被运行一次的插件,也就是说只要你想在 Vim 启动时就运行的插件就放在这个目录下。我们可以放从 vim-plug 官方下载下来的插件~/.vim/syntax/
语法描述脚本。放置有关文本(比如 C 语言)语法相关的插件~/.vim/doc/
为插件放置文档的地方。例如:help
的时候可以用到~/.vim/ftdetect/
中的文件同样也会在 Vim 启动时就运行。有些时候可能没有这个目录。ftdetect 代表的是 filetype detection。此目录中的文件应该用自动命令(autocommands)来检测和设置文件的类型,除此之外并无其他。也就是说,它们只该有一两行而已~/.vim/ftplugin/
此目录中的文件有些不同。当 Vim 给缓冲区的 filetype 设置一个值时,Vim 将会在~/.vim/ftplugin/
目录下来查找和 filetype 相同名字的文件。例如你运行set filetype=derp
这条命令后,Vim 将查找~/.vim/ftplugin/derp.vim
此文件,如果存在就运行它。不仅如此,它还会运行 ftplugin 下相同名字的子目录中的所有文件,如~/.vim/ftplugin/derp/
这个文件夹下的文件都会被运行。每次启用时,应该为不同的文件类型设置局部缓冲选项,如果设置为全局缓冲选项的话,将会覆盖所有打开的缓冲区~/.vim/indent/
这里面的文件和 ftplugin 中的很像,它们也是根据它们的名字来加载的。它放置了相关文件类型的缩进。例如 Python 应该怎么缩进,java 应该怎么缩进等等。其实放在 ftplugin 中也可以,但单独列出来只是为了方便文件管理和理解~/.vim/compiler/
和 indent 很像,它放的是相应文件类型应该如何编译的选项~/.vim/after/
这里面的文件也会在 Vim 每次启动的时候加载,不过是等待~/.vim/plugin/
加载完成之后才加载 after 里的内容,所以叫做 after~/.vim/spell/
拼写检查脚本
将插件管理器 plug.vim 克隆下载,然后复制到
~/.vim/autoload/
目录下shgit clone https://gitcode.com/gh_mirrors/vi/vim-plug mkdir ~/.vim/autoload cp vim-plug/plug.vim ~/.vim/autoload/
编辑
.vimrc
文件,在前面增加如下内容:" => Plugins ---------------------------------------------- {{{ """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" call plug#begin('~/.vim/plugged') Plug 'bullets-vim/bullets.vim' Plug 'vim-airline/vim-airline' Plug 'neoclide/coc.nvim', {'branch': 'release'} Plug 'neoclide/coc-prettier' Plug 'tpope/vim-markdown' | Plug 'ap/vim-css-color' call plug#end() " }}}
打开 Vim 运行命令安装插件
PlugInstall
创建文件
在 PowerShell 就可以直接运行 vim <filename.md>
用于创建一个新的 Markdown 文件了。
In PowerShell, type vim foo.txt bar.txt zar.txt
will open 3 files, 1 existing file and 2 newly created files.
:n
will toggle to the 2nd file.:args
will show the current active file.
如果当前状态已经在 Vim 里面:
:e <path\filename>
creates a new file with a name:enew
will create an empty buffer - a new file without a name:Ex <directory>
lets you browse for the file from the given directory:Ex
on its own will open the pwd
多文件操作
:e <path\filename>
打开或创建一个文件:ls
:buffers
:files
这三个命令等效,都可查看现有的缓冲区列表:bn
切换下一个已经打开的文件:bp
切换上一个已经打开的文件:b1
切换到缓冲区编号为 1 的文件:bd
将一个缓冲区从打开的缓冲区列表中删除
保存文件
:w foo.txt
把一个新创建的文件保存为 foo.txt:w
把已经存在的文件更改之后保存:wq
保存退出
退出文件
:wq
orZZ
,保存退出当前 buffer:q!
不保存退出 Vim:qa
退出 Vim
配置入门
在 Windows 下,Vim 的配置文件 C:\Program Files\Vim\_vimrc
。如果您在 Windows 上无法修改 _vimrc
文件,您可以尝试以下解决方案:
以管理员身份运行记事本或其他文本编辑器:右键单击 _vimrc
文件,选择“属性”,然后单击“安全”。接着,单击“组或用户名”中的“Users”,再点击“编辑”。在“Users”权限中,选中“完全控制”,然后点击“确定”。
Remap Caps Lock to Esc
Windows 10
Win+R -> regedit -> HKEY_LOCAL_MACHINE -> SYSTEM -> CurrentControlSet -> Control -> Keyboard Layout -> 右键 “Keyboard Layout” -> “新建” -> “二进制值” -> 重命名 “新值 #1” 为 “Scancode Map” -> 右键 “Scancode Map” -> “修改” -> 输入值如下:
00,00,00,00, 00,00,00,00, 03,00,00,00, 3a,00,01,00, 01,00,3a,00, 00,00,00,00
这次遇到了 Win 10 下面无法正常编辑注册表,使用以下变通方法:
1、使用编辑软件 VS Code 保存以下文件,保存为 CapsLock2Esc.reg 文件;
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Keyboard Layout]
"Scancode Map"=hex:00,00,00,00,00,00,00,00,03,00,00,00,3a,00,01,00,01,00,3a,00,00,00,00,00
2、双击该文件,弹出框,选“是”;
3、重启系统,完成。
https://www.cnblogs.com/komean/p/12700100.html
Mac mini macOS, remap Caps Lock to Escape
Need to update on macOS to get a full path of the setting
Speed up your key repeat
https://www.thewindowsclub.com/keyboard-repeat-rate-and-repeat-delay-in-windows-10
Learning Paths
- Practical Vim (Second Edition), Drew Neil, 2015
- Modern Vim, Drew Neil, 2018
参考资料
2025-03-17 update
Now I have two versions of VIM91 installed, one using Windows installer (Vim and Gvim combined), the other being a byproduct of installing Zsh.
GUI version
C:/Program Files/Vim/vim91/vim.exe
C:/Program Files/Vim/vim91/
sh:echo $VIMRUNTIME C:/Program Files/Vim/vim91/
TUI version
C:/Program Files/Git/usr/bin/vim.exe
C:/Program Files/Git/usr/share/vim/vim91/
sh:echo $VIMRUNTIME /usr/share/vim/vim91/
Both Vim versions will source the same config file from ~/.vimrc
, but the GUI version does not source ~/.vim/colors/
for the additional themes added under this folder.
自动格式化
Add plugin in your
.vimrc
filePlug 'neoclide/coc-prettier'
Run vim command
:CocInstall coc-prettier
Run
:CocConfig
to open coc-settings.json (note this is json file, so everything is enclosed with a pair of curly braces) and add following{ // Require .prettierrc "prettier.requireConfig": true, // Tslint on save "tslint.enable": true, "tslint.autoFixOnSave": true, // Run prettier (and others) "coc.preferences.formatOnSaveFiletypes": [ "css", "markdown", "Markdown", "javascript", "javascriptreact", "typescript", "typescriptreact" ] }