Vim 是 Linux 系统上的最著名的文本/代码编辑器,也是早年的 Vi 编辑器的加强版。它的最大特色是完全使用键盘命令进行编辑,脱离了鼠标操作虽然使得入门变得困难,但上手之后键盘流的各种巧妙组合操作却能带来极为大幅的效率提升。另外,由于 Vim 的可配置性非常强,各种插件、语法高亮配色方案等多不胜数,无论作为代码编辑器或是文稿撰写工具都非常给力:black_nib:.
前言 vi/vim的区别 vim 是 vi 的升级版本,它不仅兼容vi的所有指令,还有新增了一些新的特性。
vi filename vim filename // 两者同样的功能,能少敲一个字母,所以我们就用vi吧 “工具总须多磨,方才用的趁手。”
终端键入 vimtutor 即可进入Vim入门教程,30分钟足以让你应对Vim日常操作。
常规操作
命令 说明 x 删除光标下的字符 X 删除光标左边的字符 s 修改一个字符(与r的区别,修改后直接进入Insert模式) i 光标前插入字符 a 光标后插入字符 A 行尾插入字符 o 光标下方新建一行并进入Insert模式 O (字母) 光标上方新建一行并进入Insert模式 r 替换字符(单个) R 替换字符(多个) cw 替换文本直到单词末尾(ce也可) c$ 替换文本直到行尾 dw 删除单词 (de, d2w) d$ 删除到行尾 D 删除到行尾 S 删除到行为,并进入Insert模式 dd 删除整行 y 复制 yy 复制整行 (yw, ye) p 粘贴到光标之后 P 粘贴到光标之前 u 撤销 . 重复上一次操作 :! 执行外部名 (:!dir/:ls) % 查找配对的括号 ), }, ] v motion :w 选择性保存 Ctrl + D 查看可能的补全 光标移动
命令 说明 h 左 l 右 k 上 j 下 0 (数字) 行首 $ 行尾 w 下一个单词起始处 nw (2w) n可为数字,表示移动两个单词 e 单词末尾(与w的区别) gg 文件起始处 G 文件末尾处 退出
命令 说明 :wq 保存并退出 ZZ 保存并退出 :q! 丢弃修改并退出 (q退出,有修改的话q不能退出) :e! 丢弃修改并打开原文件 查找并替换
命令 说明 :s/old/new/g 查找old并替换为new,g代表整行匹配 :#, #s/old/new #, #代表首尾行号 :%s/old/new/g 全文匹配替换 :%s/old/new/gc 全文匹配替换,并每次替换会弹出确认提示 [注意]:如果你的字符串中包含/字符,为了防止命令冲突,你需要将你的/前加上转义符号,变成这样:/ 翻页
命令 说明 Ctrl + e 向下滚动一行 Ctrl + y 向上滚动一行 Ctrl + f 向下翻一页 Ctrl + b 向上翻一页 Ctrl + u 向上翻半页 Ctrl + d 向下翻半页 文件操作
命令 说明 :e + filename 打开新文件 :r + filename 读取文件并插入到当前文件 选项设置
命令 说明 :set ic 忽略大小写 :set is 部分匹配(like) :set hls 高亮匹配 :set nu 显示行号 分屏启动vim
命令 说明 vim -on file1 file2… 水平分屏 vim -On file1 file2… 垂直分屏 关闭分屏
命令 说明 Ctrl + w c 关闭当前窗口(如果是最后一个,无法关闭) Ctrl + w q 关闭当前窗口(如果是最后一个,则退出vim) 分屏
命令 说明 Ctrl + w s 上下分屏 Ctrl + w v 左右分屏 :sp filename 左右分屏,且打开一个新的文件 :vsp filename 上下分屏,且打开一个新的文件 切换分屏(光标切换)
命令 说明 Ctrl + w l 右边 Ctrl + w h 左边 Ctrl + w j 上边 Ctrl + w k 下边 分屏尺寸调整 TIPS: (上下分屏生效)
命令 说明 Ctrl + w = 所有屏一样高度 Ctrl + w + 增加高度 Ctrl + w - 减少高度 点击查看大图:vi/vim-cheat-sheet 配置VIM 安装VIM 源代码编译
git clone https://github.com/vim/vim.git sudo apt-get install libncurses5-dev libncursesw5-dev cd vim/src make 或者 apt-get install vim
快速配置版: git clone --depth=1 https://github.com/amix/vimrc.git ~/.vim_runtime sh ~/.vim_runtime/install_awesome_vimrc.sh The ultimate Vim configuration: vimrc
SpaceVIM:
curl -sLf https://spacevim.org/install.sh | bash 安装Vundle VIM plug-in:Vundle
Set up Vundle: git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim touch ~/.vimrc, set up Vundle in your .vimrc by adding the following to the top of the file: set nocompatible " be iMproved, required filetype off " required
" set the runtime path to include Vundle and initialize set rtp+=~/.vim/bundle/Vundle.vim call vundle#begin() " alternatively, pass a path where Vundle should install plugins "call vundle#begin('~/some/path/here')
" let Vundle manage Vundle, required Plugin 'VundleVim/Vundle.vim'
" The following are examples of different formats supported. " Keep Plugin commands between vundle#begin/end. " plugin on GitHub repo Plugin 'tpope/vim-fugitive' " plugin from http://vim-scripts.org/vim/scripts.html " Plugin 'L9' " Git plugin not hosted on GitHub Plugin 'git://git.wincent.com/command-t.git' " git repos on your local machine (i.e. when working on your own plugin) Plugin 'file:///home/gmarik/path/to/plugin' " The sparkup vim script is in a subdirectory of this repo called vim. " Pass the path to set the runtimepath properly. Plugin 'rstacruz/sparkup', {'rtp': 'vim/'} " Install L9 and avoid a Naming conflict if you've already installed a " different version somewhere else. " Plugin 'ascenator/L9', {'name': 'newL9'}
" All of your Plugins must be added before the following line
call vundle#end() " required
filetype plugin indent on " required
" To ignore plugin indent changes, instead use:
"filetype plugin on
"
" Brief help
" :PluginList - lists configured plugins
" :PluginInstall - installs plugins; append !
to update or just :PluginUpdate
" :PluginSearch foo - searches for foo; append !
to refresh local cache
" :PluginClean - confirms removal of unused plugins; append !
to auto-approve removal
"
" see :h vundle for more details or wiki for FAQ
" Put your non-Plugin stuff after this line
set nu " set encoding=utf-8 set encoding=utf-8 " UTF-8 Support
https://blog.easwy.com/archives/advanced-vim-skills-catalog/ https://segmentfault.com/a/1190000017502205 https://realpython.com/vim-and-python-a-match-made-in-heaven/ SpaceVim: https://spacevim.org/
$ defaults write com.microsoft.VSCode ApplePressAndHoldEnabled -bool false # For VS Code
$ defaults write com.microsoft.VSCodeInsiders ApplePressAndHoldEnabled -bool false
https://github.com/VSCodeVim/Vim
https://vim.rtorr.com/
https://vim.rtorr.com/lang/zh_cn
"vim.easymotion": true, "vim.visualModeKeyBindingsNonRecursive": [ { "before": ["p"], "commands": ["editor.action.clipboardPasteAction"] } ], "vim.useSystemClipboard": true