Skip to content
Published at:

Vim笔记

Vim介绍

Vim 编辑器之神, Emacs 神的编辑器

官网:https://www.vim.org/

Vim is a highly configurable text editor for efficiently creating and changing any kind of text. It is included as "vi" with most UNIX systems and with Apple OS X.

两个关键点:

  • 高度可配置(根据个人的使用习惯去修改快捷键、风格。避免花过多时间在折腾配置上
  • 高效的编辑文本

Show me your .vimrc file

改配置 <===> 好好写代码

Vim作用

效率、效率、还是TM的效率

效率慢的原因:

  • 鼠标:右手不停的在鼠标和键盘之间切换(找鼠标,移动鼠标,手归位到键盘)
  • 小键盘/方向键盘:切换

小故事:乔布斯为了优化开机启动时间和相关人员沟通

乔布斯认为当时的Mac电脑启动速度过慢。乔布斯问拉里:“如果能救人一命的话,你可以将系统启动时间缩短10秒钟么?”

乔布斯在白板上列出:如果Mac电脑卖出500万台,而每天每台机器开机多花费10秒钟,那加起来每年就要浪费大约3亿分钟,而3亿分钟至少相当于100个人的寿命!Mac电脑的市场占有率比较小,那么PC机呢,目前全世界PC机大约2亿台,如此推算,每年PC机浪费的开机时间就相当于4000人的寿命。

乔布斯的逻辑:开机浪费10秒 = 浪费100人的寿命

So,高效使用Vim之后省下来的时间,相当于这是比别人多出来的生命

资料

官网:https://www.vim.org

Github:https://github.com/vim/vim

书籍:vim 实用技巧https://book.douban.com/subject/26967597/

推荐的 vimrc 配置:https://github.com/amix/vimrc

学习曲线

Vim Cheat Sheet

学习过程中把握两个关键点:

  • vim命令非常多,文档中列有1000多个,还能组合
    1. Vim提供的功能:背下来、记下来
    2. 我要用Vim做什么:根据实际使用场景需要,针对性的找最高效的命令,然后针对练习
  • 类比:学打字经历的几个阶段:
    1. 找键盘/字怎么拼:怎么去切换根式,有哪些命令可以完成同样的操作
    2. 重复练习/优化:重复练习,并在不同的场景找到最高效的vim命令
    3. 量变到质变:想表达啥的时候,手指已经在敲键盘了

Vim模式

模式Mode

  • 普通模式 Normal mode(default mode):输入的一个或多个按键都会成是命令
  • 插入模式 Insert mode:正常输入的编辑模式
  • 可视模式 Visual mode:选择模式,类似拖动鼠标左键,选择内容
    • 可视行模式 Visual line mode:以行为单位复制
    • 可视块模式 Visual block mode:选择代码块
  • 命令行/末行模式 Command-line command:执行命令
  • 替换模式 Replace mode:替换字符

模式切换

  • —> 普通模式 Esc/Ctrl+[
  • 普通模式 —> 插入模式 iIaAoO
  • 普通模式 —> 可视模式 v
  • 普通模式 —> 可视行模式 V
  • 普通模式 —> 可视块模式 Ctrl+v
  • 普通模式 —> 替换模式 r/R

转跳Move

光标移动 Move

  • hjkl:左下上右; Here is why vim uses hjkl keys as arrow keys
  • e/E:移动光标到单词的末尾e(end)
  • b/B:移动光标到单词的开头b(begin of the world)
  • 0:移动光标到行首
  • $/^:移动光标到行尾/移动光标到有字符的行首
  • gg/G:移动光标到文件的首行/移动光标到文件的末行
  • 10G/10gg:移动光标到当前文件的第10行
  • :N:移动光标到当前文件的第10行
  • 10%:移动光标到当前文件行数的10%位置
  • H/M/L:移动光标到当前可见页面代码的high/middle/low
  • %:匹配()/[]/{}
    • 如果光标在括号上,就会转跳到与之匹配的的括号上
    • 如果光标没有在括号上,就会在当前行, 正向搜索第一个括号,转跳到与这个括号匹配括号的位置
  • Ctrl + ] / Ctrl + O / Ctrl + I:转跳到代码定义/跳出来(out)/跳回去(in)
  • [[ / ]] / [] / ][
  • /words:在当前文件中, 向后搜索单词words
  • ?words:在当前文件中, 向前搜索单词words
  • n/N:正向/反向的重复上一次搜索
  • #/*:在当前文件中, 正向/反向搜索光标位置的字符串
  • f{char}/t{char}:在当前行,正向搜索字符char,
    • f{char}:搜索到字符之前,光标转跳到char的位置
    • t{char}:搜索到字符之后,光标转跳到char的前一个位置
  • F{char}/T{char}:在当前行,返回搜索字符char
    • F{char}:搜索到字符之前,光标转跳到char的位置
    • T{char}:搜索到字符之后,光标转跳到char的一个位置(搜索路径上的前一个位置,其实是char的后一个位置)
  • ;/,:重复上一次f{char}/t{char}/F{char}/T{char}的查找

命令Command

修改 Change

  • C:修改光标到行尾的字符字符, 然后进入插入模式
  • cc/S:修改光标所在行的所有字符, 然后进入插入模式
  • s:删除光标位置的字符, 然后进入插入模式

删除 Delete

  • x:删除光标位置的字符, 模式不变
  • D:修改光标到行尾的字符字, 模式不变
  • dd:修改光标所在行的所有字符, 模式不变
  • 3dd/d3d:正向删除3行, 模式不变

选择(可视) v/V/Ctrl+V

  • v:进入可视模式
  • V:进入可视行模式
  • Ctrl + v:进入可视块模式
  • o/O:转跳光标
    • 可视行模式:上下转跳
    • 可视块模式:o:转跳到对角位置,O:转跳到同行的另一个角位置
  • >>/<<:对选中的代码,向左/向右移动indent单位, 2/4/8
  • :sort:对选中的代码,按字母排序

复制/粘贴 Copy(yank)

  • yy/Y:复制一行
  • 选择(可视) + y:对选中的代码进行复制

替换 Replace

  • r: rx替换当前光标位置字母为x(替换一个字母)
  • R:进入替换模式,自动正向替换(替换多个)

对象 Object

  • w:word
  • t:tag
  • ":"HelloWorld"
  • ':'HelloWorld'
  • ><img src="http://baidu.com/a.webp">
  • ): ( some code )
  • }
  • ]:[ some code ]

命令组合

公式1动作(action) + [ 范围<a/i> + ] 对象(object)

  • <action><object>
  • <action>a<object>
  • <action>i<object>

动作action:

  • c:change修改
  • d:delete删除
  • v:visual选中
  • y:yank复制

范围a = all / i = in

  • cw / caw / ciw:修改一个单词

公式2:动作(action) + 方向/数字

主题Subject

缓冲区Buffer

A buffer is the in-memory text of a file.

  • :buffers/:ls/:files:显示所有的buffer文件
  • :bn/:bnext:转跳到下一个buffer文件
  • :bp/:bprevious:转跳到上一个buffer文件
  • :b1/:buffer1:转跳到第一个buffer文件

标签页Tab page

Todo:

书签 Mark

  • m{a-zA-Z}:给光标所在的位置设置一个书签,名为后面所使用的字符
  • '{a-zA-Z} /{a-zA-Z}:转跳到书签名为字符的书签位置
  • :marks:查看所有的mark标签
  • :delmarks {a-zA-Z}:删除某个mark标签
  • :delmarks!:删除当前buffer文件的所有书签 but not marksA-Z or 0-9

滚动屏幕 Roll

  • zz:把光标所在行,滚动到中间
  • zt:把光标所在行,滚动到顶部t(top)
  • zb:把光标所在行,滚动到底部b(bottom)
  • Ctrl + e:向下滚动一行
  • Ctrl + y 向上滚动一行
  • Ctrl + d 向下滚动半屏 Down
  • Ctrl + u 向上滚动半屏 Up
  • Ctrl + f 向下滚动一屏 Forward
  • Ctrl + b 向上滚动一屏 Back Forward

分屏 Split

  • :vs a.txt:竖直分割窗口,并打开a.txt
  • :vsplit a.txt:同上
  • :vertical split a.txt:同上
  • :sp a.txt:水平分割窗口,并打开a.txt
  • :split a.txt:同上

窗口 Windows

  • Ctrl + W, c:关闭光标所在的当前窗口 Close
  • Ctrl + W, k:移动光标到上方窗口
  • Ctrl + W, j:移动光标到下方窗口
  • Ctrl + W, h:移动光标到左方窗口
  • Ctrl + W, l:移动光标到右方窗口

代码折叠 Fold

  • zo:打开折叠 Open
  • zc:关闭折叠 Close
  • zR:打开所有折叠
  • zM:关闭所有折叠

替换 Replace

Todo:

宏 Macro

重复复杂的操作,这时候就可以制作成一个宏。

制作/使用步骤:

  1. qa:将后续命令录制在寄存器 a 中(从 a 到 z 有 26 个可用)
  2. some cmd:完成操作的一系列命令
  3. q:停止录制
  4. @a: 执行宏 a

剪切板 Copy/Paste

Vim 有 12 个粘贴板依次编号为:0、1、2、...、9、a、"、+,其中 + 号为系统粘贴板,” 为临时粘贴板。系统剪切板中的内容可在其他程序中使用。上面的复制指令都可以配合剪切板进行操作。

  • "nyw 复制当前单词到 n 号剪切板(双引号开始)
  • "np 粘贴 n 号剪切板内容到当前位置后
  • "+Y 复制当前行到系统剪切板
  • "+ny 复制当前行加下面 n 行到系统剪切板
  • "+p 粘贴系统剪切板内容到当前位置后

其他常用命令

  • .:重复上一次操作
  • u/Ctrl+R : 撤销undo/重做redo
  • Ctrl+W(Insert mode):删除光标前面的一个单词
  • Ctrl+U(insert mode):删除光标前面的所有单词
  • :g/关键字/d:删除包括关键字的行
  • :%g!/关键字/d:v/关键字/d:删除包括关键字的行
  • :g/^$/d:删除所有空行
  • :set relativenumber:设置相对行号
  • :set norelativenumber--> 取消相对行号
  • :set scrolloff=5:设置光标距离顶部和底部的滚动间距为5行
  • :!cmd:在vim编辑器中执行一条shell命令
  • gd:转跳到变量定义gd(goto Declaration)
  • gf:转跳到光标所在的文件(后面插件open_file_under_cursor.vim讲)

文本删除和保留:todo

Vim配置

软件配置文件的种类

  • 自定义:Vim(Vimscript)
  • xml:VS, Idea
  • json:VS code, Sublime text

Vimscript学习资料:Learn Vimscript the Hard Way 英文版 中文版

配置的目录

不同的系统配置文件目录:help vimrc

shell
Unix            $HOME/.vimrc or $HOME/.vim/vimrc
OS/2            $HOME/.vimrc, $HOME/vimfiles/vimrc or $VIM/.vimrc (or _vimrc)
MS-Windows      $HOME/_vimrc, $HOME/vimfiles/vimrc or $VIM/_vimrc
Amiga           s:.vimrc, home:.vimrc, home:vimfiles:vimrc or $VIM/.vimrc

具体系统配置文件目录$ vim --version

shell
   system vimrc file: "$VIM/vimrc"
     user vimrc file: "$HOME/.vimrc" # 👍
 2nd user vimrc file: "~/.vim/vimrc"
      user exrc file: "$HOME/.exrc"
       defaults file: "$VIMRUNTIME/defaults.vim"
  fall-back for $VIM: "/usr/local/share/vim"

.vimrc配置

rc:resource资源, vim启动的时候会去加载的文件

站在巨人的肩膀上

https://github.com/amix/vimrc star:19.5k 👍

安装install

shell
# 下载
git clone --depth=1 https://github.com/amix/vimrc.git ~/.vim_runtime
# 安装完整版本
sh ~/.vim_runtime/install_awesome_vimrc.sh
# 安装基础版本
sh ~/.vim_runtime/install_basic_vimrc.sh

更新update

shell
cd ~/.vim_runtime && git pull --rebase

额外添加自己的配置或插件

shell
# 添加自定义配置的文件
vim ~/.vim_runtime/my_configs.vim

# 添加自定义配置的目录, 会自动加载这个插件
cd ~/.vim_runtime/my_plugins
git clone git@github.com:neoclide/coc.nvim.git

Vim插件

管理Vim插件的插件

  • ack.vim: Vim plugin for the_silver_searcher (ag) or ack -- a wicked fast grep
  • bufexplorer.zip: Quickly and easily switch between buffers. This plugin can be opened with <leader+o>
  • ctrlp.vim: Fuzzy file, buffer, mru and tag finder. It's mapped to <Ctrl+F>
  • goyo.vim and vim-zenroom2:
  • lightline.vim: A light and configurable statusline/tabline for Vim
  • NERD Tree: A tree explorer plugin for vim
  • open_file_under_cursor.vim: Open file under cursor when pressing gf
  • pathogen.vim: Manage your vim runtimepath
  • snipmate.vim: snipmate.vim aims to be a concise vim script that implements some of TextMate's snippets features in Vim
  • ale: Syntax and lint checking for vim (ALE requires NeoVim >= 0.2.0 or Vim 8 with +timers +job +channel)
  • vim-commentary: Comment stuff out. Use gcc to comment out a line (takes a count), gc to comment out the target of a motion. gcu uncomments a set of adjacent commented lines.
  • vim-expand-region: Allows you to visually select increasingly larger regions of text using the same key combination
  • vim-fugitive: A Git wrapper so awesome, it should be illegal
  • vim-indent-object: Defines a new text object representing lines of code at the same indent level. Useful for python/vim scripts
  • vim-multiple-cursors: Sublime Text style multiple selections for Vim, CTRL+N is remapped to CTRL+S (due to YankRing)
  • vim-yankstack: Maintains a history of previous yanks, changes and deletes
  • vim-zenroom2 Remove all clutter and focus only on the essential. Similar to iA Writer or Write Room
  • gist-vim Easily create gists from Vim using the :Gist command
  • vim-indent-guides Is a plugin for visually displaying indent levels in Vim
  • editorconfig-vim EditorConfig helps maintain consistent coding styles for multiple developers working on the same project across various editors and IDEs.

Vim & LSP

coc.nvim: Make your Vim/Neovim as smart as VSCode. 用上VSCode积累的LSP协议+语言服务实现,

Updated at: