03-Vim编辑器
Fizzbuzz
Fizz buzz 是一款让孩子们学习除法的集体文字游戏。 从一开始,玩家轮流逐步数数,将任何能被三整除的数字替换为“fizz”,将任何能被五整除的数字替换为“buzz”,将任何能同时被三和五整除的数字替换为“fizzbuzz”。
Vim的编辑模式
- 正常模式:在文件中四处移动光标进行修改
- 插入模式:插入文本
i - 替换模式:替换文本
R - 可视化模式(一般
v,行V,块Ctrl-V):选中文本块
可视化模式:- 可视化:
v - 可视化行:
V - 可视化块:
Ctrl+v
- 可视化:
可以用移动命令来选中。
- 命令模式:用于执行命令
:
<ESC>(退出键)从任何其他模式返回正常模式
Vim的命令行模式
:q退出(关闭窗口):w保存(写):wq保存然后退出:e {文件名}打开要编辑的文件:ls显示打开的缓存:help {标题}打开帮助文档:help :w打开:w命令的帮助文档:help w打开w移动的帮助文档
Vim的一些快捷键
Vim的移动
- 基本移动:
hjkl(左, 下, 上, 右) - 词:
w(下一个词),b(词初),e(词尾) - 行:
0(行初),^(第一个非空格字符),$(行尾) - 屏幕:
H(屏幕首行),M(屏幕中间),L(屏幕底部) - 翻页:
Ctrl-u(上翻),Ctrl-d(下翻) - 文件:
gg(文件头),G(文件尾) - 行数:
:{行数}<CR>或者{行数}G({行数}为行数) - 杂项:
%(找到配对,比如括号或者 /* */ 之类的注释对) - 查找:
f{字符},t{字符},F{字符},T{字符}- 查找/到 向前/向后 在本行的
,/;用于导航匹配
- 搜索:
/{正则表达式},n/N用于导航匹配
Vim的编辑
i进入插入模式- 但是对于操纵/编辑文本,不单想用退格键完成
O/o在之上/之下插入行d{移动命令}删除- 例如,
dw删除词,d$删除到行尾,d0删除到行头。
- 例如,
c{移动命令}改变- 例如,
cw改变词 - 比如
d{移动命令}再i
- 例如,
x删除字符(等同于dl)s替换字符(等同于xi)- 可视化模式 + 操作
- 选中文字,
d删除 或者c改变
- 选中文字,
u撤销,<C-r>重做y复制 / “yank” (其他一些命令比如d也会复制)p粘贴- 更多值得学习的: 比如
~改变字符的大小写
Vim的计数
你可以用一个计数来结合“名词”和“动词”,这会执行指定操作若干次。
3w向后移动三个词5j向下移动 5 行7dw删除 7 个词
Vim的修饰语句
你可以用修饰语改变“名词”的意义。修饰语有 i,表示“内部”或者“在内”,和 a, 表示“周围”。
ci(改变当前括号内的内容ci[改变当前方括号内的内容da'删除一个单引号字符串, 包括周围的单引号
几个输入模式
-
i:在光标所在字符前开始插入 -
a:在光标所在字符后开始插入 -
o:在光标所在行的下面另起一新行插入 -
s:删除光标所在的字符并开始插入 -
I:在光标所在行的行首开始插入 如果行首有空格则在空格之后插入 -
A:在光标所在你行的行尾开始插入 -
O:在光标所在行的上面另起一行开始插入 -
S:删除光标所在行并开始插入
vimtutor教程总结
Lesson1
Lesson 1 SUMMARY
- The cursor is moved using either the arrow keys or the hjkl keys.
h (left) j (down) k (up) l (right)
h,j,k,l分别代表左下上右 - To start Vim from the shell prompt type:
vim FILENAME <ENTER>,vim编辑文件 - To exit Vim type:
<ESC>:q!<ENTER>to trash all changes.
OR type:<ESC>:wq<ENTER>to save the changes. - To delete the character at the cursor type:
x删除一个字符 - To insert or append text type:
itype inserted text<ESC>insert before the cursor 光标前插入
Atype appended text<ESC>append after the line 行末插入
NOTE: Pressing <ESC> will place you in Normal mode or will cancel
an unwanted and partially completed command.
Lesson2
Lesson 2 SUMMARY
- To delete from the cursor up to the next word type:
dw - To delete from the cursor up to the end of the word type:
de - To delete from the cursor to the end of a line type:
d$ - To delete a whole line type:
dd
-dw删除从光标开始到下一个单词的开始
-de删除光标到下一个单词的最后一个字母
-d$删除光标开始到行末,
-dd删除整行 - To repeat a motion prepend it with a number:
2w
2w即移动两个单词 - The format for a change command is:
operator [number] motion
where:
operator - is what to do, such as d for delete
[number] - is an optional count to repeat the motion
motion - moves over the text to operate on, such as w (word),
e (end of word), $ (end of the line), etc.
**就是dw、de、d$中间都可以加上数字,比如执行前面这个操作operator,多少次[number],然后移动到哪里motion``** 注意是2dd`删除两行,数字在前面,因为d不是移动的位置 - To move to the start of the line use a zero: 0
按0移动到行首 - To undo previous actions, type:
u(lowercase u)
To undo all the changes on a line, type:U(capital U)
To undo the undo's, type:CTRL-R
-u撤销上一步
-U撤销这一行的所有操作**
-CTRL-R撤销撤销的操作,即不撤销
Lesson3
Lesson 3 SUMMARY
- To put back text that has just been deleted, type p . This puts the
deleted text AFTER the cursor (if a line was deleted it will go on the
line below the cursor).
即按下p键,会把之前dd或者2dd删除掉的行数,呈现在光标后面 - To replace the character under the cursor, type r and then the
character you want to have there.
即按下r键,比如rx会把这个字母替换成x - The change operator allows you to change from the cursor to where the
motion takes you. eg. Type ce to change from the cursor to the end of
the word, c$ to change to the end of a line.
ce就是换掉光标开始到这个词结束的内容
c$就是换掉光标开始到这行结束的所有文字
cc就是换掉一整行文字 - The format for change is:
c [number] motion
可以加上c+数字+操作,使得动作重复一点
Lesson4
Lesson 4 SUMMARY
CTRL-Gdisplays your location in the file and the file status.
Gmoves to the end of the file.
number Gmoves to that line number.
ggmoves to the first line.
CTRL-G说明当前是哪一行,G去文件最末尾,gg前往文件最开头,但是数字+G会跳转到文件的第数字- Typing
/followed by a phrase searches FORWARD for the phrase.
Typing?followed by a phrase searches BACKWARD for the phrase.
After a search type n to find the next occurrence in the same direction
or N to search in the opposite direction.
CTRL-Otakes you back to older positions,CTRL-Ito newer positions./是往下寻找对应字符,?是往上寻找对应字符,寻找时n是下一个,N是上一个,但是如果是?则n和N结果相反
- **`CTRL-O`跳转到寻找之前的位置,一直按一直往前,`CTRL-I`跳转到寻找之后的位置,即一直按一直往后**
- Typing % while the cursor is on a (,),[,],{, or } goes to its match.
当光标所在一个括号(或者[或者{的位置上时,按下%会跳转到与其对应的另外一个括号 - To substitute new for the first old in a line type
:s/old/new
To substitute new for all 'old's on a line type:s/old/new/g
To substitute phrases between two line #'s type:#,#s/old/new/g
To substitute all occurrences in the file type:%s/old/new/g
To ask for confirmation each time add 'c':%s/old/new/gc:s/old/new:替换光标所在行的第一个匹配的词:s/old/new/g:替换光标所在行的所有匹配的词:#,#s/old/new/g:替换第#行到第#行的所有匹配的词:%s/old/new/g:替换整篇文章里面所有匹配的词:%s/old/new/gc:替换整篇文章里面所有匹配的词,同时需要确认%说明整篇文章 ,c是confirm,即确认,g是global,即所有
Lesson5
Lesson 5 SUMMARY
:!command executes an external command.
Some useful examples are:
(Windows) (Unix)
:!dir :!ls - shows a directory listing.
:!del FILENAME :!rm FILENAME - removes file FILENAME.
:!这个字母加上后面的shell的命令,可以直接在vim编辑器里面执行外部的shell命令,比如直接:!python3 python.py:w FILENAMEwrites the current Vim file to disk with name FILENAME.
使用:w FILENAME这个命令可以把当前的vim文件另存为FILENAME的名字v motion :w FILENAMEsaves the Visually selected lines in file
FILENAME.
v motion :w FILENAME使用这个命令会把当前v选中的行,写入FILENAME这个文件当中:r FILENAMEretrieves disk file FILENAME and puts it below the
cursor position.
:r FILENAME会把FILENAME这个文件里面的内容直接贴到光标后面,如果是:r !ls,则是把ls的输出直接贴到光标后面- :r !dir reads the output of the dir command and puts it below the
cursor position.
Lesson6
Lesson 6 SUMMARY
- Type
oto open a line BELOW the cursor and start Insert mode.
TypeOto open a line ABOVE the cursor. - Type
ato insert text AFTER the cursor.
TypeAto insert text after the end of the line. - The
ecommand moves to the end of a word. - The
yoperator yanks (copies) text, p puts (pastes) it. - Typing a capital
Renters Replace mode untilESCis pressed. - Typing
:set xxxsets the option "xxx". Some options are:
ic'ignorecase' ignore upper/lower case when searching
is'incsearch' show partial matches for a search phrase
hls'hlsearch' highlight all matching phrases
You can either use the long or the short option name. - Prepend "no" to switch an option off:
:set noic
-o后一行插入,O前一行插入
-a单词后插入,A行末插入
-e移动到单词末尾
-y复制,p粘贴,yw复制光标到下一个词开头,yy复制一整行
-R替换光标后面的东西,一直替换直到ESC
-:set ic搜索的时候不区分大小写
-:set noic取消不区分大小写
-:set is搜索的时候就是高亮当前搜索结果
-:set nois搜索的时候取消高亮当前搜索结果
-set hls高亮所有搜索到的词语
-set nohls取消高亮所有搜索到的词语
- 即在不需要的命令前面加no
Lesson7
Lesson 7 SUMMARY
- Type
:helpor press<F1>or<HELP>to open a help window. - Type
:help cmdto find help on cmd . - Type
CTRL-Wto jump to another window. - Type
:qto close the help window. - Create a vimrc startup script to keep your preferred settings.
- When typing a : command, press
CTRL-Dto see possible completions.
Press<TAB>to use one completion.
补全:使用CTRL-D或者<TAB>
配置自己的脚本
Lesson 7.2: CREATE A STARTUP SCRIPT
** Enable Vim features **
Vim has many more features than Vi, but most of them are disabled by
default. To start using more features you should create a "vimrc" file.
- Start editing the "vimrc" file. This depends on your system:
:e ~/.vimrcfor Unix
:e ~/_vimrc for Windows - Now read the example "vimrc" file contents:
:r $VIMRUNTIME/vimrc_example.vim
r读取内容并插入,$VIMRUNTIME是一个 Vim 的内置环境变量,代表 Vim 的安装目录
整个命令会把官方的 vimrc 示例配置贴到你的配置文件里 - Write the file with:
:w
The next time you start Vim it will use syntax highlighting.
You can add all your preferred settings to this "vimrc" file.
For more information type :help vimrc-intro
Vim 进阶
这里我们提供了一些展示这个编辑器能力的例子。我们无法把所有的这样的事情都教给你,但是你 可以在使用中学习。一个好的对策是: 当你在使用你的编辑器的时候感觉 “一定有更好的方法来做这个”, 那么很可能真的有:上网搜寻一下。
搜索和替换
:s (替换)命令(文档)。
%s/foo/bar/g- 在整个文件中将 foo 全局替换成 bar
%s/\[.*\](\(.*\))/\1/g- 将有命名的 Markdown 链接替换成简单 URLs
多窗口
- 用
:sp/:vsp来分割窗口 - 同一个缓存可以在多个窗口中显示。
宏
q{字符}来开始在寄存器{字符}中录制宏q停止录制@{字符}重放宏- 宏的执行遇错误会停止
{计数}@{字符}执行一个宏{计数}次- 宏可以递归
- 首先用
q{字符}q清除宏 - 录制该宏,用
@{字符}来递归调用该宏 (在录制完成之前不会有任何操作)
- 首先用
- 例子:将 xml 转成 json (file)
- 一个有 “name” / “email” 键对象的数组
- 用一个 Python 程序?
- 用 sed / 正则表达式
g/people/d%s/<person>/{/g%s/<name>\(.*\)<\/name>/"name": "\1",/g- …
- Vim 命令 / 宏
ggdd,Gdd删除第一行和最后一行- 格式化最后一个元素的宏 (寄存器
e)- 跳转到有
<name>的行 qe^r"f>s": "<ESC>f<C"<ESC>q
- 跳转到有
- 格式化一个 的宏
- 跳转到有
<person>的行 qpS{<ESC>j@eA,<ESC>j@ejS},<ESC>q
- 跳转到有
- 格式化一个 标签然后转到另外一个 的宏
- 跳转到有
<person>的行 qq@pjq
- 跳转到有
- 执行宏到文件尾
999@q
- 手动移除最后的
,然后加上[和]分隔符
浙公网安备 33010602011771号