work with vim

80
 Work with VIM the editor belltoy<[email protected]>

Upload: zhongqiu-zhao

Post on 30-Jun-2015

2.433 views

Category:

Technology


2 download

DESCRIPTION

Introduction to Vim, especially for programmers.

TRANSCRIPT

Page 1: Work with Vim

  

Work with VIM

the editor

belltoy<[email protected]>

Page 2: Work with Vim

  

What is VIM?

Page 3: Work with Vim

  

Vi IMpoved A Screen-based Text Editor Not an IDE Can be seen in many places bla bla bla

Page 4: Work with Vim

  

神器In a word:

Page 5: Work with Vim

  

Learning Curves

Page 6: Work with Vim

  

How to Learn

:help vimtutor Use it

●Tips: http://vimcdoc.sourceforge.net/

Page 7: Work with Vim

  

Starting Vim

$ vim [filename] # 默认为普通模式

$ view [filename] # 只读方式打开文件

$ vimdiff file1 file2 # 对比两个文件的差异

$ gvim #GUI

建议使用终端下的 vim

Page 8: Work with Vim

  

:help starting.txt

Page 9: Work with Vim

  

Features

Page 10: Work with Vim

  

6 BASIC modes Normal Visual Select Insert Command-line Ex-mode

5 ADDITIONAL modes

:help vim-modes

VIM Modes

Page 11: Work with Vim

  

Normal Mode

默认的模式 所有的按键被解释为指令,而不是输入文本,

直到执行了切换模式的指令,转入其它模式。 一般指令结构

[count]command[motion] [count] 数字,表示重复次数 [motion] 移动指令,代表 command 作用范围 . 点号用来重复执行上一个指令

从其它模式返回按 Esc 键

Page 12: Work with Vim

  

Motion

Page 13: Work with Vim

  

Page 14: Work with Vim

  

← ↓ ↑ →H J K L

Page 15: Work with Vim

  

按单词移动 w 移动到下一个单词的词首 b 移动到上一个单词的词首 e 移动到当前单词的词尾 ge 移动到上一个单词的词尾

Note : 单词由非单词字符分隔,例如: .,-) 标点符号,空白字符等。:help iskeyword

Page 16: Work with Vim

  

按行移动 0 到行首(第一列) ^ 到行首第一个非空白字符 $ 到行尾 nG 到第 n 行,例如 30G 表示移动到第 30 行 G 到最后一行 gg 到第一行

Note: vim 指令中的大写字母和上档键都必须按 Shift 键输入。

Page 17: Work with Vim

  

行内移动 fx f 表示向前(右)查找( Find )

x 表示指定的字符 Fx 大写 F 表示向后(左)查找 tx t 表示向前查找( To )

光标停留在目标字符的前一个位置 Tx 大写 T 表示向后查找

Note: vim 指令中很多大写指令表示与小写指令相反方向的动作。

Page 18: Work with Vim

  

按屏幕移动 CTRL-F 向下滚动一屏 CTRL-B 向上滚动一屏 CTRL-D 向下半屏 CTRL-U 向上半屏 CTRL-L 刷新屏幕 CTRL-E 向下滚动一行 CTRL-Y 向上滚动一行

Note: vim 指令中与 CTRL 组合的按键不用按大写

Page 19: Work with Vim

  

屏幕内移动 H 移动到屏幕内的第一行 L 移动到屏幕内的最后一行 M 移动到屏幕内的中间一行 zt 把光标所在的行滚动到屏幕顶部 zb 把光标所在的行滚动到屏幕底部 zz 把光标所在的行滚动到屏幕中部

Note: zt, zb, zz 这三个指令不会移动光标,只是滚屏。还有一些 z 开头的指令是用于代码折叠

Page 20: Work with Vim

  

简单的搜索 /regex 向前搜索,并移动光标到匹配的位置 ?regex 向后搜索,与 /regex 反向的动作 如果有多个匹配,可以用 n/N 来回查找

搜索替换

:n,ms/regex/replace/gi从第 n 到第 m 行,搜索 regex 并替换成 replace ,第三个 /

后面的 g 表示全局, i 表示忽略大小写

Note: :help regex

Page 21: Work with Vim

  

使用标记移动光标

用 G 或者搜索指令跳转光标到其它的地方,可以用 ~ 或者 ' 跳回之前的位置

命名的标记 用 ma 标记一个位置为 a ,之后就可以用 ~a或

者 'a 跳回位置 a 了。 可以使用 a-z 共 26 个名字

Note: :help bookmark

Page 22: Work with Vim

  

:help motion.txt

Page 23: Work with Vim

  

Changing

删除文本 x 删除光标下的字符 X 删除光标前的字符 dd 删除整行 dw 删除到下一个单词的词首 d{motion} 删除 {motion} 跨越的文本 D 删除到行尾

Note: 删除指令实际上是将文本暂存在默认寄存器中:help registers

Page 24: Work with Vim

  

复制文本 y 和 d 指令类似

粘贴文本 p 将寄存器中的文本粘贴在光标位置之后 P 显然是粘贴在光标之前

Note: d, y, p 指令都可以指定寄存器,例如 "ayy 表示把当前行复制到寄存器 a 中,然后用 "ap 就可以粘贴出来。

Page 25: Work with Vim

  

使用 "+、 "*寄存器与 GUI交互

"+y # 复制 (类似 CTRL-C) 到系统剪切板

"+p #粘贴 (类似 CTRL-V)剪切板到 Vim

"*y #Linux 下类似选中复制

"*p #Linux 下类似中键粘贴

Note: 试试 "_ 寄存器,它是个黑洞,有去无回,很好玩吧 :-p

Page 26: Work with Vim

  

撤销操作 u 撤销上一次的操作 CTRL-R 重做上一次的撤销

Note: :help undo

Page 27: Work with Vim

  

:help change.txt

Page 28: Work with Vim

  

Insert Mode

从 Normal mode进入 Insert modei 在光标前插入

a 在光标后插入

I 在行首插入

A 在行尾插入

o 在光标所在行之后插入

O 在光标所在行之前插入

s 删除当前字符,并进入插入模式

S 删除当前行的所有字符,并进入插入模式

c{motion} 删除,进入插入模式

C 删除到行尾,进入插入模式

Page 29: Work with Vim

  

Visual Mode

v 进入可视模式,以字符为单位选择 V 进入可视模式,以行为单位选择 CTRL-V 进入列块可视模式

选择之后可以接 y / d / c 等 change类的指令

Page 30: Work with Vim

  

Command-line Mode

按 : 进入 Cmd-line mode

:help {subject}[@lang] 帮助

:n 跳转到第 n 行

:w 保存

:q 退出

:wq 保存并退出

:r [filename] 插入文件到当前文件

:e [filename] 编辑文件

:tabe [filename] 从新的 tab 打开文件

:w !sudo tee % 用 root保存

Page 31: Work with Vim

  

Options

三种类型的选项 布尔型 可以打开或关闭 数值型 值为数值 字符串 值为字符串

:set {option}? 显示选项的值

:set {option} 布尔型开,其它类型显示值

:set no{option} 布尔型关

:set inv{option} 反转

:set {option}+={value} append

:set {option}-={value} reduce

Page 32: Work with Vim

  

:help options.txt

Page 33: Work with Vim

  

Syntax Highlight

Page 34: Work with Vim

  

$ ls -l /usr/share/vim/vim72/syntax/ | wc -l

524

Page 35: Work with Vim

  

:help syntax.txt

Page 36: Work with Vim

  

Encoding

Page 37: Work with Vim

  

:set fencs=ucs-bom,utf-8,gbk,gb2312,latin1 :set fenc=utf-8:set enc=utf-8 :set tenc=utf-8  :help encoding

Page 38: Work with Vim

  

Key Mapping

Page 39: Work with Vim

  

:map all

:nmap normal mode ☻

:vmap visual mode ☻

:imap insert mode ☻

:smap select mode

(more...)

Page 40: Work with Vim

  

缩排

nmap <Tab> v>nmap <bs> v<

vmap <Tab> >gvvmap <bs> <gv

Normal模式下用Tab和 Backspace键缩排

Visual模式下用Tab和 Backspace键缩排

Page 41: Work with Vim

  

 1  2 " Mappings *************************************************************** {{{ 3 let mapleader = "," 4 " Professor VIM says '87% of users prefer jj over esc', jj abrams disagrees 5 imap jj <Esc> 6 imap uu _ 7 imap hh => 8 imap aa @ 9 imap <up> <C-o>gk10 imap <down> <C-o>gj11 imap .. ->12 13 " 选中后按 /向后 (?向前 )直接搜索14 vmap / y/<C-R>"<CR>15 vmap ? y?<C-R>"<CR>16 nmap <silent> <Leader>, :nohlsearch<CR>17 18 " 缩排19 nmap <Tab> v>20 nmap <bs>  v<21 vmap <Tab> >gv22 vmap <bs>  <gv23 24 " 查找 QuickFix的下一个 /上一个匹配点25 nmap <C-n> :cn<CR>26 nmap <C-p> :cp<CR>27 28 " 插入模式下按F2插入时间戳29 imap <F2> <C-R>=strftime("%Y-%m-%d %H:%M:%S")<CR>30 " }}}

Page 42: Work with Vim

  

:help map.txt

Page 43: Work with Vim

  

Text Object

Page 44: Work with Vim

  

word string paragraph block

action(yank,delete,change ...etc)

Page 45: Work with Vim

  

v c d i a { [ ( < " '

visaulchangedelete

inner objectan object

{...}[...](...)

<...>"..."'...'

Operator Region

Page 46: Work with Vim

  

va{

Page 47: Work with Vim

  

va{

Page 48: Work with Vim

  

:help text-objects

Page 49: Work with Vim

  

Tab Pages

Page 50: Work with Vim

  

:set mouse=a 的话,可以用鼠标点这里来关闭 tab窗口

Page 51: Work with Vim

  

:tabnew:tabedit path/to/file:tabfind path/to/file:tab help tabpage.txt

NERD_tree 或者其它一些插件中按 t 在新的 tab中打开文件tab 间的切换 :ngtngT

Page 52: Work with Vim

  

Folds

Page 53: Work with Vim

  

Page 54: Work with Vim

  

Fold Methods

manual 手工定义折叠 indent 更多的缩进表示更高级别的折叠 expr 用表达式来定义折叠 syntax 用语法高亮来定义折叠 diff 对没有更改的文本进行折叠 marker 对文中的标志折叠,默认 {{{,}}}

Page 55: Work with Vim

  

:help fold.txt

Page 56: Work with Vim

  

viminfo & Session

Page 57: Work with Vim

  

Envy session in IDE?

Page 58: Work with Vim

  

viminfo: 如果你退出 Vim 以后又再次启动,通常你会丢失许多信息。 viminfo 文件可以用来记住这些信息,从而允许你继续上次退出的编辑。

session: Vim 会话存放着所有跟你的编辑相关的信息。这包括诸如文件列表、窗口布局、全局变量、选项、以及其它信息。

Page 59: Work with Vim

  

:mksession! /path/to/sessionfile.vim:wviminfo /path/to/viminfofile.viminfo

:source /path/to/sessionfile.vim:rviminfo /path/to/viminfofile.viminfo

保存当前工作区的会话和viminfo

加载保存的会话和viminfo,重建工作环境

Page 60: Work with Vim

  

.vimrc

Page 61: Work with Vim

  

Refer: https://github.com/belltoy/vimrc

Page 62: Work with Vim

  

Plugins

Page 63: Work with Vim

  

Standard Plugins

pi_getscript.txt 下载 Vim 脚本的最新版本 pi_gzip.txt 读写 gzip压缩过的文件 pi_netrw.txt 通过网络读写文件 pi_paren.txt 高亮匹配括号 pi_tar.txt Tar 文件浏览器 pi_vimball.txt 创建自安装的 Vim脚本 pi_zip.txt Zip归档文件浏览器

Page 64: Work with Vim

  

:help standard-plugin

Page 65: Work with Vim

  

SnipMate

来自 TextMate ,代码片段功能

Page 66: Work with Vim

  

NERD_tree

树状目录浏览

Page 67: Work with Vim

  

ctags+TagList将代码结构以树的方式查看可以在函数变量定义处跳转

Page 68: Work with Vim

  

NERD_commenter

代码注释工具

Page 69: Work with Vim

  

vcscommand版本控制系统的集成插件

支持 cvs/svn/svk/hg/git/bzr还支持扩展

Page 70: Work with Vim

  

AutoClose

自动关闭括号的小插件

Page 71: Work with Vim

  

matchit

%匹配增强工具

Page 72: Work with Vim

  

acp

代码自动完成工具

Page 73: Work with Vim

  

bufexplorer

缓冲区浏览器

Page 74: Work with Vim

  

FuzzyFinder

快速查找 buffer/file/command/bookmark/tag 的工具

Page 75: Work with Vim

  

Command-T类似 FuzzyFinder ,不过更强大

同样来自 TextMate

Page 76: Work with Vim

  

Align

代码对齐工具

Page 77: Work with Vim

  

MatrixMatrix screensaver for VIM :-D

Page 78: Work with Vim

  

Demo

Page 79: Work with Vim

  

Q & A

Page 80: Work with Vim

  

Thanks