Missing Semester

Vim is a modal editor.

  • Normal: for moving around a files and making edits
  • Insert: for inserting text
  • Command-line: for running a command
  • Replace: for replacing text
  • Visual(plain, line or block): for selecting blocks of text

Keystrokes have different meanings in different operating modes.
e.g.
x: In Insert mode will just insert a literal character ‘x’, but in Normal mode, it will delete the character under the cursor, and in Visual mode, it will delete the selection.

For Normal mode, pressing

  • i: enter Insert mode
  • R: Replace mode
  • v: Visual mode
  • V: Visual line mode
  • Ctrl-v: Visual block mode
  • :: Command-line mode

Basics

Buffers, tabs, and windows

Vim maintains a set of open files, called “buffers”. A Vim session has a number of tabs, each of which has a number of windows (split panes). Each window shows a single buffer. Unlike other programs you are familiar with, like web browsers, there is not a 1-to-1 correspondence between buffers and windows; windows are merely views. A given buffer may be open in multiple windows, even within the same tab. This can be quite handy, for example, to view two different parts of a file at the same time.

By default, Vim opens with a single tab, which contains a single window.

Command-line mode

  • :q: quit (close window)
  • :w: save (“write”)
  • :wq: save and quite
  • e {name of file}: open file for editing
  • :ls: show open buffers
  • :help {topic}: open help
    • :help :w: opens help for the :w command
    • :help w: opens help for the w movement

Vim’s interface is a programming language

Movement

  • Basic movement: hjkl (left, down, up, right)
  • Words: w next word, b beginning of word, e end of word
  • Lines: 0 beginning of line, ^ first none-blank character, $ end of line
  • Screen: H top of screen, M middle of screen, L bottom of screen
  • Scroll: Ctrl-u up, Ctrl-d down
  • File: gg beginning of file, G end of file
  • Line numbers: :{unmber}<CR>or {number}G line{number}
    • <CR>: Carriage Return, 回车
  • Misc: % corresponding item
  • Find: f{character}t{character}F{character}T{character}
    • find/to forward/backward {character} on the current line
    • , / ; for navigating matches
  • Search: /{regex}n / N for navigating matches, ?{regex} opposite direction

Selection

Visual modes:

  • Visual: v
  • Visual Line: V
  • Visual Block: Ctrl-v

Can use movement keys to make selection.i

Edits

  • i: enter Insert mode
    • but for manipulating/deleting text, want to use something more than backspace
  • o / O: insert line below / above
  • d{motion}: delete {motion}
    • e.g. dw is delete word, d$ is delete to end of line, d0 is delete to beginning of line
  • c{motion}: change {motion}
    • e.g. cw is change word
    • like d{motion} followed by i
  • x: delete character (equal to dl)
  • s: substitute character (equal to cl)
  • Visual mode + manipulation
    • select text, d to delete it or c to change it
  • u: to undo, <C-r> to redo
  • y: to copy / “yank” (some other commands like d also copy)
  • p: to paste
  • Lots more to learn: e.g. ~ flips the case of a character

Counts

  • 3w move 3 words forward
  • 5j move 5 lines down
  • 7dw delete 7 words

Modifiers

You can use modifiers to change the meaning of a noun. Some modifiers are i, which means “inner” or “inside”, and a, which means “around”.

  • ci( change the contents inside the current pair of parentheses
  • ci[ change the contents inside the current pair of square brackets
  • da' delete a single-quoted string, including the surrounding single quotes

vimtutor

书籍推荐

  • Vim - Vi Improved

关于命令、计数、动作
d number motion

  • Ctrl-w Ctrl-w 可在窗口之间切换
  • :sp 多开窗口
  • :help [command] 找到关于命令的帮助
    • :help user-manual Vim的用户手册

插入

  • i 在光标前面插入
  • a 在光标后插入
  • A 在行末插入
  • ce 删除光标到单词末尾的文本,并且进入Insert mode
    • c [number] motion 比如cw, c2w, c$
  • o 在光标的下方新建一行并进入插入模式

删除

  • x 删除光标所在位置的字符
  • dw 从光标处删除至一个单词末尾
  • d$ 从光标处删除至行尾
  • dd 删除整行
    • 删除的内容会存入寄存器,类似剪切

撤销

  • u 撤销一下
  • U 撤销对整行的修改
  • Ctrl-r 重做被撤销的命令

复制与粘贴

  • y 拷贝
  • yy 拷贝整行
  • yw 拷贝一个单词
  • p 在光标下一行粘贴
  • P 在光标上一行粘贴

替换

  • r 输入r和一个字符替换光标所在位置的字符
  • R 连续替换多个字符

定位于文件状态

  • Ctrl-g 显示当前编辑文件中当前光标所在行位置,以及文件状态信息
  • G 跳到最后一行
  • [number] G 跳到指定行
  • gg 跳到第一行

搜索

  • / 顺向搜索
  • ? 逆向搜索
  • n 顺向查找下一个匹配字符串
  • N 逆向查找下一个匹配字符串
  • Ctrl-o 回到上一个位置
  • Ctrl-i 跳转到最新的位置
  • :noh 临时清除当前高亮,在下一次搜索时会重新高亮
    • nnoremap <silent> <C-l> :noh<CR><C-l> 映射 :nohCtrl-l:清除搜索高亮并重绘屏幕

括号配对

  • % 跳到与当前光标所处括号匹配的另一个括号处

替换命令

  • :s/old/new/g 替换old为new
  • :#,#s/old/new/g 其中 #,# 代表的是替换若干行中收尾两行的行号
  • :%s/old/new/g 替换整个文件中的所有匹配串
  • :%s/old/new/gc 会找到整个文件的所有匹配串,并且对每个匹配串是否替换进行提示

在vim内执行外部命令

  • :![command] 比如:!ls,所有的外部命令都可以这样执行

保存文件的更多信息

  • :w FILENAME 另存文件,名为FILENAME
    • 可以通过 v 进入 Visual mode 选择文本再另存(以行为单位进行选择)

提取和合并文件

  • :r FILENAME 向当前文件插入另外的文件的内容