1. 关于我

个人信息

1.1. Vim

1.1.1. Edit a file

1.1.1.1. Move around quickly

  • If you set the incsearch option, Vim will show the first match for the pattern, while you are still typing it. This quickly shows a typo in the pattern.

  • If you set hlsearch option, Vim will highlight all matches for the pattern with a yellow background. :nohlsearch will close it.

  • If you see a specific word and want to search for other occurrences of the same word, use the * command to grab the word from under the cursor and search for the next one, while # search for the previous one.

  • In structured text(C/C++)
    • % can jump to many different matching items. It is very useful to check if() and {} constructs are balanced properly.

    • Use [{ to jump back to the "{" at the start of the current code block, and ]} for '}'.

    • Use gd to jump from the use of variable to its local declaration.

  • 可视(visual)模式,用于选定文本块;可以在正常模式下输入v(小写)来按字符选定,输入V(大写)来按行选定,或输入Ctrl-V来按方块选定。

  • `.(跳转到最近修改过的位置)

  • ZQ(无条件退出)

  • ZZ(存盘退出)

  • ga(显示光标下的字符在当前使用的 encoding 下的内码)

  • guw(光标下的单词变为小写)

  • gUw(光标下的单词变为大写)

  • :TOhtml (根据 Vim 的语法加亮的方式生成 HTML 代码)

  • K (显示光标下单词的 man 手册页)

  • 将当前行提到页首:zt,页尾:zb

  • gg 定位到文件开头,G 定位到文件的结尾。

1.1.1.2. Don't type it twice

  • If you want to change one word into another in the whole file, you can use the :s(substitute) command.

    • 去掉所有的行尾空格:“:%s/\s\+$//”。

    • 去掉所有的空白行:“:%s/\(\s*\n\)\+/\r/”。

    • 去掉所有的“//”注释:“:%s!\s*//.*!!”。

    • 去掉所有的“/* */”注释:“:%s!\s*/\*\_.\{-}\*/\s*! !g”。

  • If you want to change one word into another only a few locations, a quick method is to use the * command to find the next occurrence of the word and use cw to change the word. Then type n to find the next word and .(dot) to repeat the cw command.

  • CTRL-N can auto complete function and variable names. Vim looks up words in the file you are editing, and also in #include'd files.

  • Vim has a mechanism to record a macro. You type qa to start recording into register 'a'. Then you type your commands as usual and finally hit q again to stop recording. When you want to repeat the recorded commands you type @a.

  • 寄存器
    • 除了有一个无名寄存器外,Vim 还有一大堆有名的寄存器,可以通过“"”(参见“:help "”)或“Ctrl-R”(参见“:help i_CTRL-R”和“:help c_CTRL-R”)加寄存器名(字母、数字和某些特殊字符,参见“:help registers”;“无名”寄存器的名字是“"”)来访问。比如,你先使用“"ayy”复制了一行,然后使用“dd”删掉了一行,然后移动光标到要复制到的位置,就可以使用“"aP”把先前复制的内容粘贴上去了。
    • 在使用 X Window 系统时,有两个特殊的寄存器是需要注意一下的:“"*”访问的寄存器是 X 的主选择区域(primary selection),“"+”访问的寄存器是 X 的剪贴板(clipboard)。
    • 还有一个很特殊的“寄存器”:“=”。在插入模式或命令模式中,键入“Ctrl-R=”,Vim 会提示你输入一个表达式,普通的整数运算在此完全有效。

1.1.1.3. Fix it when it's wrong

  • It's normal to make errors while typing, this can be corrected with abbreviations: :abbr Lunix Linux.

  • The same mechanism can be used to type a long word with just a few characters: :abbr pn pinguin.

  • To find errors in your text Vim has a clever hightlighting mechanism. This was actually meant to be used to do syntax hightlighting of programs, but it can catch and highlight errors as well.
  • Modeline for C coding style(see above): /* vim:shiftwidth=8:tabstop=8:filetype=c:norl: */

    • 把光标移到要重新格式化的文本开头,使用gq命令后面跟一个光标移动命令确定重新格式化的范围。比如gq}(格式化一段),gq5j(格式化 5 行),gqG(格式化至文件末尾)。

  • :set list 查看所有不可见字符; :set nolist 关闭

  • A more complex example: English text.

1.1.2. Edit more files

1.1.2.1. A file seldom comes alone

  • Tag mechanism works for jumping between files.
    • :tag 关键字(跳转到与“关键字”匹配的标记处)
    • :tselect [关键字](显示与“关键字”匹配的标记列表,输入数字跳转到指定的标记)
    • :tjump [关键字](类似于“:tselect”,但当匹配项只有一个时直接跳转至标记处而不再显示列表)
    • :tn(跳转到下一个匹配的标记处)
    • :tp(跳转到上一个匹配的标记处)
    • Ctrl-](跳转到与光标下的关键字匹配的标记处;除“关键字”直接从光标位置自动获得外,功能与“:tags”相同)
    • g](与“Ctrl-]”功能类似,但使用的命令是“:tselect”)
    • g Ctrl-](与“Ctrl-]”功能类似,但使用的命令是“:tjump”)
    • Ctrl-T(跳转回上次使用以上命令跳转前的位置)
  • Another powerful mechanism is to find all occurrences of a name in a group of files, using the :grep command. Vim makes a list of all matches, and jumps to the first one. The :cn command takes you to each next match.

  • Positions the cursor on the name of the function in your file and type [i: Vim will show a list of all matches for the function name in the included files.

  • In vim you can split the text erea in several parts to edit different files.
    • :split 上下分屏; :vsplit 左右分屏。

  • There are more uses of multiple window. The preview-tag mechanism is a very good example.
  • 可以使用“gf”命令方便地跳转到光标下的文件名所代表的文件中。使用“Ctrl-O”(参见“:help CTRL-O”)可返回到原先的文件中。

1.1.2.2. Let's work together

  • Select some structured text in a list and sort it: !sort.

  • 如果想把外部命令执行的结果插入到当前编辑的缓冲区中,可以考虑使用“:r!”。
  • 当使用可视模式选中文本行后然后键入“:!”(命令行上将出现“:'<,'>!”,表示命令的范围是选定的文本),或者使用“:%!”(表示命令的范围是整个缓冲区中的文本),Vim 在执行后面的命令时,将把命令范围里的文本行作为后面执行的命令标准输入,并用命令执行后的标准输出替换当前缓冲区中的这些文本行。

    • 要对所有的非空行进行编号,只需要“:%!nl”;要对包含空行的所有行进行编号?OK,“:%!nl -ba”。
  • Replaces in more then one files:
    1. vim *.h *.c
    2. :bufdo :g/prefix_[a-z_]\+ *(/ :s/(int index/(char *name/|:w^j

1.1.2.3. Text is structured

  • One of the simpler things is to speed up the edit-compile-fix cycle. Vim has the :make command, which starts your compilations, catches the errors it produces and lets you jump to the error locations to fix the problems.

    • :cn(显示下一个错误)
    • :cp(显示上一个错误)
    • :cl(列出所有的错误及其编号)
    • :cc(跳转到指定编号的错误)
    • :copen(打开快速修订窗口,在其中显示所有错误,可在错误上双击鼠标或按回车键跳转至该错误)

1.1.3. Sharpen the saw

1.1.3.1. Make it a habit

  • You need to learn new commands and turn them into a habit.

1.1.4. Task

1.1.4.1. Editing in hex mode

  1. vim -b NSLU2?_v23R29.bin
  2. :%!xxd
  3. When completed, :%!xxd -r

2. Misc

3. 反馈

欢迎大家对我说三道四哪