Practical Vim Edit Text at the Speed of Thought笔记

Foreword
Vim is highly configurable. However, customization is a personal thing, so I’ve tried to avoid recommending what should or should not go into your vimrc file. Instead, Practical Vim focuses on the core functionality of the editor—the stuff that’s always there, whether you’re working over SSH on a remote server or using a local instance of GVim, with plugins installed to add extra functionality. Master Vim’s core, and you’ll gain portable access to a text editing power tool.
Learn to Touch Type, Then Learn Vim
If you have to look down to find the keys on the keyboard, the benefits of learning Vim won’t come fast. Learning to touch type is imperative.
Get to Know Vim’s Built-in Documentation
The best way to get to know Vim’s documentation is by spending time in it. To help out, I’ve included “hyperlinks” for entries in Vim’s documentation. For example, here’s the “hyperlink” for the Vim tutor: :h vimtutor.

Chapter 1. The Vim Way
本章核心理念:减少重复操作 从不同的操作中提炼共同点 灵活使用.重复修改

1.Meet the Dot Command
The dot command lets us repeat the last change. It is the most powerful and
versatile command in Vim.
======== 用<与>缩进(:h >)

2.Don’t Repeat Yourself
寻找操作中重复步骤加以简化 i.e.如要在当前行开始5行末尾增加分号 可以先$a;<ESC> 然后5次j$. 优化先A;<ESC> 然后j.
要做到这点先要熟悉常用修改键的变种C=c$ s=cl S=^C I=^i A=$a o=$<CR> O=ko
3.Take One Step Back, Then Three Forward
第一步费操作 方便后面用.来重复操作

======== 用;与,重复最近一次f/t查找命令
4.Act, Repeat, Reverse
熟练使用u来撤销修改 用成对的移动/查找指令来来回切换 i.e. ;/, f/F t/T n/N
======== 用&重复最近一次替换

5.Find and Replace by Hand
======== *即反向#
======== */#前加g为非全字匹配
======== gd/gD 跳转本地/全局声明

6.Meet the Dot Formula
One Keystroke to Move, One Keystroke to Execute
将移动与修改操作区别开来(修改操作容易使用.重复)

 

Part 1 ---- Modes ==============================================================

Chapter 2. Normal Mode
本章简述普通模式下常用技巧
要点:尽量用重复(repeat)指令而不要用计数(count)指令来重复操作(计数需要数数 降低效率 此外计数操作不利于之后重复操作 i.e.删除两个单词(d2w)后再次删除一个单词 不如dw后..)

7.Pause with Your Brush Off the Page

8.Chunk Your Undos
在恰当的位置退出insert mode后重新插入 而非从头到尾一次性打完代码 可以更方便的使用u来撤销 i.e.每修改一行<ESC>一下
++++++++ 习惯用normal mode的h j k l移动 而不是在insert mode中用<UP> <DOWN> <LEFT> <RIGHT>移动

9.Compose Repeatable Changes
======== daw删除整个单词 无需先b到单词头部再dw (还是方便.重复操作 后者.只会执行dw 删除不完整单词)
======== aw(a word) 一个单词(包含空格) iw(inner word) 一个单词(不包含空格) 类似还有ap ip as is a( i( a[ i[ 具体:h可查
======== c d v后面不光可以接w b p 还可以接/xxx 具体可以:h operator查看操作符 :h motion查看位移符

10.Use Counts to Do Simple Arithmetic
======== <C-a>与<C-x>可以增加/减少光标所在(或光标右侧最近)的数字

11.Don’t Count If You Can Repeat
repeat指令像是原子指令 可以重复利用 而count指令降低可重复利用性

12.Combine and Conquer
======== gu/gU/g~修改大小写 i.e.guaw将当前光标所在单词改为小写 这样就不用bvwu了(其实我觉得这样也挺方便 但是不利于重复)

Chapter 3. Insert Mode
本章大部分大部分技巧于我自身习惯并不合适 故略记

13.Make Corrections Instantly from Insert Mode
Touch typing is more than just not looking at the keyboard; it means doing it by feel. When touch typists make an error, they know it even before their eyes process the information on the screen in front of them. They feel it in their fingers, like a misplaced step.
提升打字速度(每分钟60个单词) 单个错误可以直接删除错误后重新键入(尼玛臣妾做不到啊)
======== insert mode下分别删除一个字符(退格)<C-h> 一个单词<C-w> 一行<C-u>

14.Get Back to Normal Mode
======== <C-[> insert mode下可切换至normal mode 效果同<ESC>
======== <C-o> insert mode下可切换至insert normal mode (insert normal mode 可执行一次normal mode命令然后回到insert mode)

15.Paste from a Register Without Leaving Insert Mode
++++++++ 重映射Caps Lock 防止使用k l移动时变为K L (非常同意! 但是更好的办法是使用<C-[>来退出 使用SHIFT gu gU来大小写) 可映射为<ESC>
======== 附注: H L 光标移到顶部/底部 J 合并两行 K 在man page中查找光标所在字符串
======== 用<C-r>{register}在insert mode下粘贴

16.Do Back-of-the-Envelope Calculations in Place
======== 用<C-r>=在insert mode下输入数学表达式可直接获得结果

17.Insert Unusual Characters by Character Code
======== <C-v>{num} 输入num对应的ASCII字符
======== 用ga :as获取光标所在字符的ASCII码

18.Insert Unusual Characters by Digraph

19.Overwrite Existing Text with Replace Mode
======== gR进入virtual replace mode 与普通replace mode区别在于tab被看做8个字符 只有当替换字符超过8个时tab才会被替换

Chapter 4. Virtual Mode
本章要点:始终记住visual mode下命令是对所有行/列/区域执行的 注意命令对所有行/列/区域都适用

20.Grok Visual Mode

21.Define a Visual Selection
======== gv重选上次选择的区域
======== visual mode下o/O切换选择方向

22.Repeat Line-Wise Visual Commands

23.Prefer Operators to Visual Commands Where Possible

24.Edit Tabular Data with Visual-Block Mode
用visual-block mode修改块状数据

25.Change Columns of Text
多列修改

26.Append After a Ragged Visual Block
不规则矩形区域修改
======== visual mode下插入用I/A i/a不是插入键

Chapter 5. Command-Line Mode

27.Meet Vim’s Command Line
======== ex mode命令: delete yank put copy move join normal substitutue global
======== ex mode命令范围符: .当前行 $最后行 %=.,$ 全部范围 't mark t位置 /xxx/ 匹配xxx \/ 上次搜索 \?上次搜索(反向) \& 上次替换 每个命令后均可加+/- num偏移位置
======== ex mode也有<C-w> <C-u>

28.Execute a Command on One or More Consecutive Lines

29.Duplicate or Move Lines Using ‘:t’ and ‘:m’ Commands
======== :copy {addr}命令缩写:co :t 后跟地址表示拷贝到所在行之后 如需拷贝到文件首 使用0表示第0行后(实际并没有第0行)
======== 复制当前行:t. 相当于normal mode下yyp 区别在于yyp使用寄存器 :t.不使用寄存器(重要:不覆盖默认寄存器值)
++++++++ 对于复制较远的行使用:t.无需来回切换光标所在位置 使用较方便
======== 重复最后一次command mode命令@:

30.Run Normal Mode Commands Across a Range
======== :normal {cmd}命令对选定范围内每行执行cmd命令

31.Repeat the Last Ex Command

32.Tab-Complete Your Ex Commands
======== <TAB>补全command mode命令
======== <C-d>列出所有可选命令

33.Insert the Current Word at the Command Prompt
======== <C-r><C-w>在command mode下复制光标所在字符(一个word) 常用于加速替换命令 i.e. %s//<C-r><C-w>/g
======== <C-r><C-a>复制一个WORD

34.Recall Commands from History
======== <C-p> <C-n>在command mode下作用同<UP> <DOWN> 区别在于前者不能根据已键入内容过滤历史命令 但后者难键入 所以建议将前者映射为后者 i.e. cnoremap <C-p> <UP>
======== q:打开command-line window command-line window下可以正常编辑命令(就像编辑文本一样) 然后执行新命令
======== 同理q? q/打开搜索记录窗口
======== <C-f>在command mode下可切换至command-line window

35.Run Commands in the Shell
======== :! 命令是在shell中执行的 :命令是vim中执行的

 

Part 2 ---- Files ==============================================================

Chapter 6. Manage Multiple Files
本章重点:<C-w>命令集

36.Track Open Files with the Buffer List
++++++++ <C-^>作用同:b# 使用前者来替代后者加速切换
======== :bd命令前可加范围 i.e. :%bd 删除所有buffer

37.Group Buffers into a Collection with the Argument List
======== argument list与buffer区别: 后者仅仅是个缓冲文件 前者更像是文件集合 可以对argument list整体操作 但不能对buffer整体操作
======== :args 列出文件列表 如果后面跟参数可向argument list添加文件

38.Manage Hidden Files

39.Divide Your Workspace into Split Windows
======== <C-w>s <C-w>v可横向/纵向分割窗口(才知道 一直都是:vertical split)
======== <C-w>c关闭窗口(close) <C-w>o关闭除此以外窗口(only)
======== <C-w>=均匀显示窗口大小
======== <C-w>_最大化当前窗口高度 如果命令前加数字 按数字大小行数显示
======== <C-w>|最大化当前窗口宽度 如果命令前加数字 按数字大小列数显示

40.Organize Your Window Layouts with Tab Pages
======== <C-w>T将当前buffer在新标签页中打开(必须是多buffer状态)
++++++++ gt gT切换标签页(效果同:tabn :tabp) 使用前者来代替后者加速切换

Chapter 7. Open Files and Save Them to Disk
本章重点:利用find命令直接打开文件 只读文件的保存

41.Open a File by Its Filepath Using ‘:edit’
======== :e % 编辑当前文件
======== :e %:h 编辑当前目录 可以重映射该键cnoremap <expr> %% getcmdtype() == ':' ? expand('%:h').'/' : '%%'

42.Open a File by Its Filename Using ‘:find’
======== :find 直接查找并打开文件
======== :set path+= 添加文件搜索路径 使用**匹配所有搜索目录 *匹配所有当前目录文件名 详情请:h file-searching

43.Explore the File System with netrw
======== :Explore 等于:edit .

44.Save Files to Nonexistent Directories

45.Save a File as the Super User
======== 修改只读文件后保存的办法 :w !sudo tee % > /dev/null 命令详解 :write !{cmd}将文件内容作为输入传给cmd 其中%被vim解析为当前文件 tee命令将输入内容写到输出

 

Part 3 ---- Getting Around Faster ==============================================

Chapter 8. Navigate Inside Files with Motions
本章重点:真实行与显示行的区别 如何在显示行内移动

46.Keep Your Fingers on the Home Row

47.Distinguish Between Real Lines and Display Lines
======== real line与display line区别: j / k在real line之间移动 gj / gk在display line之间移动 0 / ^ / $ 均有类似对应键

48.Move Word-Wise
======== e的反向移动ge

49.Find by Character

50.Search to Navigate
======== 删除一段代码的快捷方式v进入visual mode /xxx跳到文字段末尾 h取消多选中的字符 d删除

51.Trace Your Selection with Precision Text Objects

52.Delete Around, or Change Inside
++++++++ 习惯用ciw 而非viwc

53.Mark Your Place and Snap Back to It
======== m{reg} 添加标记 '{reg} 跳转标记
======== ''跳转上次标记 '.跳转最近修改 '^跳转最近插入 '[ / ']跳转最近复制起始 / 结尾 '< / '>跳转最近选定起始 / 结尾

54.Jump Between Matching Parentheses

Chapter 9. Navigate Between Files with Jumps
与第六章(多文件管理)的命令结合使用

55.Traverse the Jump List

56.Traverse the Change List
======== g; / g,跳转最近修改记录 详情请:h changelist 与53条合用保证来回跳转

57.Jump to the Filename Under the Cursor
======== gf跳转文件
======== 增加文件类型 :set suffixesadd+=.c
======== 查看文件路径 :set path? 增加文件路径 :set path +=

58.Snap Between Files Using Global Marks
======== m{letter} 当letter为小写字母是代表本地mark 不能跳出本文件 当letter为大写字母时代表全局mark 可在文件中跳转

 

Part 4 ---- Registers ==========================================================

Chapter 10. Copy and Paste

59.Delete, Yank, and Put with Vim’s Unnamed Register

60.Grok Vim’s Registers
======== :reg查看register
======== register的使用:在普通命令前加"x x为寄存器名
======== "是unnamed register 保存默认的最近修改的内容
++++++++ 0是yank register 存最近yank的数据 使用0就不用担心先yank后delete会将"寄存器的数据冲掉
======== _是black hole register
++++++++ :version查看vim版本
======== +是system register *是selection register =是expression register

61.Replace a Visual Selection with a Register
++++++++ 注意当使用unnamed register替换文字后寄存器数据就被改变了 若要再次替换需用"0

62.Paste from a Register
++++++++ insert mode下粘贴<C-r>" / <C-r>0

63.Interact with the System Clipboard

Chapter 11. Macros

64.Record and Execute a Macro

65.Normalize, Strike, Abort
++++++++ Normalize the Cursor Position 保证每次执行宏时都能复位到正常位置
++++++++ Strike Your Target with a Repeatable Motion 使用可重复的光标移位技巧
++++++++ Abort When a Motion Fails 利用光标移位失败不执行之后的命令来忽略命令执行次数

66.Play Back with a Count
++++++++ 利用; , .命令结合macro达到高效输入

67.Repeat a Change on Contiguous Lines
======== 串行方式{cnt}@{reg} 每个命令依次执行 需要在命令后增加motion 一次失败后面均不执行
======== 并行方式选定目标行 :normal @{reg} 命令同时执行 无需在命令后增加motion 失败时仅跳过改行
++++++++ 并行命令与串行命令比较: 根据情况灵活使用

68.Append Commands to a Macro
======== 向寄存器x的大写寄存器X键入命令会将其添加在x尾部 而非保存在X中 这个功能可以使我们向寄存器中添加命令

69.Act Upon a Collection of Files

70.Evaluate an Iterator to Number Items in a List
======== :let命令声明变量 :echo打印变量值
======== 在insert mode下插入表达式值 <C-r>=x<CR> 说明:<C-r>插入一个寄存器内容 =表达式寄存器 后跟表达式x

71.Edit the Contents of a Macro
======== normal mode下粘贴宏 :put x(x为register名)

 

Part 5 ---- Patterns ===========================================================

Chapter 12. Matching Patterns and Literals

72.Tune the Case Sensitivity of Search Patterns

73.Use the \v Pattern Switch for Regex Searches
======== \v使用智能正则搜索(very magic search) 即特殊字符无需用backslash转义 默认为特殊含义 \V相反 默认所有字符为字符本义(very nomagic search) 详情:h \v
======== i.e. /\v<leader>匹配leader单词 因为<>被解释为单词首尾界定符(\v默认转义含义) /\V<leader>匹配<leader> 即字面单词
======== i.e. /\v\<leader\>匹配<leader> 因为\< \>被转为普通< > /\V\<leader\>匹配leader单词 因为\< \>被转义为单词首尾界定符

74.Use the \V Literal Switch for Verbatim Searches
======== \V与\v相反 强制认为字符为普通字符 转义字符需用\显示标记出来
======== i.e. /V.ID 可以匹配VOID或V.ID /\VV.ID智能匹配V.ID 注意\V必须加在前面 放在后面无效

75.Use Parentheses to Capture Submatches
======== <>匹配单词边界 \_s匹配空格或换行 \1 \2 ... \9匹配匹配单词的子匹配

76.Stake the Boundaries of a Word
======== <>匹配的单词被称为zero width item 仅匹配单词本身

77.Stake the Boundaries of a Match
======== \zs \ze匹配输入pattern的submatch 即匹配整个函式 对结果匹配\zs \ze之间内容 可被多次使用 最终结果为最后一次起效
======== i.e. /^\s*\zsif 匹配以(包含若干空格的)if开始的行 匹配结果忽略了if前的空格

78.Escape Problem Characters
======== 强制取消字符转义 escape(string, char) 详情请:h escape()
======== 获取命令类型 getcmdtype()

Chapter 13. Search

79.Meet the Search Command
++++++++ 当backward搜索时 可以使用N反转搜索 也可以使用/<CR>反转搜索方向后n继续搜索

80.Highlight Search Matches

81.Preview the First Match Before Execution

82.Count the Matches for the Current Pattern
======== 如何计数最近搜索字符的出现次数:%s///gn
======== 解释 n表示不进行替换 ///表示搜索字符为空(默认使用上次搜索字符) 替换字符为空(n存在时替换字符非空也一样不替换) 注意///不能少一条 否则就变成用gn替换上次搜索的内容了

83.Offset the Cursor to the End of a Search Match
======== //跳转到最近搜索字符的下一次出现位置 //e跳到该字符的末尾

84.Operate on a Complete Search Match

85.Create Complex Patterns by Iterating upon Search History
======== vim搜索策略默认为贪心匹配 解决办法强制跳过相同字符
======== i.e. /\v'.+'会匹配多个'的词组 /\v'[^']+'只能匹配成对的' 改进版/\v(([^']|'\w)+)' 保证能识别won't这类字符
++++++++ 记得使用q/来修改正则表达式
++++++++ 在一组字符/词组/语法外加括号 c{motion}(<C-r>")<ESC> 技巧在于"寄存器保存内容正是c{motion}改变前的内容
======== 将成对的'替换为"的办法 :%s/\v'(([^']|'\w)+)'/“\1”/g
======== \w匹配word等于[0-9A-Za-z_] 类似还有\s匹配space \d匹配digtal等于[0-9] \x匹配hex等于[0-9A-Fa-f] \o匹配octal等于[0-7] \l匹配lowercase等于[a-z] \u匹配uppercase等于[A-Z] 以及以上特殊字符的大写 代表不匹配对应内容

86.Search for the Current Visual Selection
======== xnoremap * :<C-u>call <SID>VSetSearch()<CR>/<C-R>=@/<CR><CR>
======== xnoremap # :<C-u>call <SID>VSetSearch()<CR>?<C-R>=@/<CR><CR>
======== function! s:VSetSearch()
======== let temp = @s
======== norm! gv"sy
======== let @/ = '\V' . substitute(escape(@s, '/\'), '\n', '\\n', 'g')
======== let @s = temp
======== endfunction

Chapter 14. Substitution

87.Meet the Substitute Command
======== 替换命令flag: g全局 c确认 n不做实际替换动作 &使用上次替换命令的flag
======== 替换命令symbol:\0 \1 \2代表匹配整个函式 子函式1 子函式2

88.Find and Replace Every Match in a File

89.Eyeball Each Substitution

90.Reuse the Last Search Pattern

91.Replace with the Contents of a Register
======== :%s//\=@0/g //代表搜索pattern使用最近pattern \=表示获取vim脚本表达式 @0表示寄存器0

92.Repeat the Previous Substitute Command
======== 三种重复替换动作(对整个文件) g& :%&& :%s//~/&

93.Rearrange CSV Fields Using Submatches

94.Perform Arithmetic on the Replacement
======== /\v\<\/?h\zs\d :%s//\=submatch(0)-1/g 前者匹配<h1 <h2 <\h1等单词 后者将数字减1

95.Swap Two or More Words
======== /\v(<man>|<dog>) :%s//\={"dog":"man","man":"dog"}[submatch(1)]/g 前者匹配man与dog 后者交换两个单词位置

96.Find and Replace Across Multiple Files
======== 对整个工程文件执行替换命令 :args **/*.txt :set hidden :argdo %s/.../.../g
======== 第一个命令添加当前路径下所有文件 第二个命令允许修改文件后不保存直接跳转到其它文件 第三个命令执行替换

Chapter 15. Global Commands

97.Meet the Global Command
======== :[range] global[!] /{pattern}/ [cmd] 命令对所有符合匹配的行执行命令 详情请:h :g
======== :g 默认范围为% 整个文件 这与:d :s :normal默认范围为一行的不同
======== [cmd]可为空 此时使用:print打印匹配结果
======== global!或vglobal可强制在所有行执行

98.Delete Lines Containing a Pattern
======== :g//d 删除所有包含满足匹配字符串的行
======== :v(即:vglobal) 为:g相反作用 i.e. :v//d 删除所有不包含满足匹配字符串的行

99.Collect TODO Items in a Register
======== :g/{pattern}/y {reg} 可将匹配行保存至寄存器 注意{reg}必须大写(即以append形式添加在reg里) 如用小写执行每行命令时都会将其替换

100.Alphabetize the Properties of Each Rule in a CSS File
++++++++ 记住[cmd]也支持[range] i.e. :g/{/ .+1,/}/-1 sort 首先匹配{的行对.+1到/}/-1的行执行sort命令 .+1即当前行的下一行 /}/-1即匹配}的前一行

 

Part 6 ---- Tools ==============================================================

Chapter 16. Index and Navigate Source Code with ctags

101.Meet ctags

102.Configure Vim to Work with ctags
======== :set tags? 查看tags路径
======== :autocmd BufWritePost * call system("ctags -R") 保存文件时自动更新tags

103.Navigate Keyword Definitions with Vim’s Tag Navigation Commands

Chapter 17. Compile Code and Navigate Errors with the Quickfix List

104.Compile Code Without Leaving Vim

105.Browse the Quickfix List

106.Recall Results from a Previous Quickfix List

107.Customize the External Compiler

Chapter 18. Search Project-Wide with grep, vimgrep, and Others

108.Call grep Without Leaving Vim

109.Customize the grep Program

110.Grep with Vim’s Internal Search Engine

Chapter 19. Dial X for Autocompletion

111.Meet Vim’s Keyword Autocompletion
======== 自动补全命令 <C-x><C-i>索引头文件关键字补全 <C-x><C-]>索引tags关键字补全 <C-x><C-l>按行补全

112.Work with the Autocomplete Pop-Up Menu

113.Understand the Source of Keywords

114.Autocomplete Words from the Dictionary

115.Autocomplete Entire Lines

116.Autocomplete Filenames

117.Autocomplete with Context Awareness

Chapter 20. Find and Fix Typos with Vim’s Spell Checker

118.Spell Check Your Work

119.Use Alternate Spelling Dictionaries

120.Add Words to the Spell File

121.Fix Spelling Errors from Insert Mode

Chapter 21. Now What?
======== Keep Practicing!
======== Make Vim Your Own
======== Know the Saw, Then Sharpen It

 

posted @ 2018-02-22 15:20  Five100Miles  阅读(481)  评论(0编辑  收藏  举报