Vim
Missing Semester
Modal editing
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 modeR: Replace modev: Visual modeV: Visual line modeCtrl-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 quitee {name of file}: open file for editing:ls: show open buffers:help {topic}: open help:help :w: opens help for the:wcommand:help w: opens help for thewmovement
Vim’s interface is a programming language
Movement
- Basic movement:
hjkl(left, down, up, right) - Words:
wnext word,bbeginning of word,eend of word - Lines:
0beginning of line,^first none-blank character,$end of line - Screen:
Htop of screen,Mmiddle of screen,Lbottom of screen - Scroll:
Ctrl-uup,Ctrl-ddown - File:
ggbeginning of file,Gend of file - Line numbers:
:{unmber}<CR>or{number}Gline{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/Nfor 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 / aboved{motion}: delete {motion}- e.g.
dwis delete word,d$is delete to end of line,d0is delete to beginning of line
- e.g.
c{motion}: change {motion}- e.g.
cwis change word - like
d{motion}followed byi
- e.g.
x: delete character (equal todl)s: substitute character (equal tocl)- Visual mode + manipulation
- select text,
dto delete it orcto change it
- select text,
u: to undo,<C-r>to redoy: to copy / “yank” (some other commands likedalso copy)p: to paste- Lots more to learn: e.g.
~flips the case of a character
Counts
3wmove 3 words forward5jmove 5 lines down7dwdelete 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 parenthesesci[change the contents inside the current pair of square bracketsda'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-manualVim的用户手册
插入
i在光标前面插入a在光标后插入A在行末插入ce删除光标到单词末尾的文本,并且进入Insert modec [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>映射:noh到Ctrl-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向当前文件插入另外的文件的内容

