Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Vim is a terminal text editor. It is an extended version of vi with additional features, including syntax highlighting, a comprehensive help system, native scripting (Vim script), a visual mode for text selection, comparison of files (vimdiff(1)), and tools with restricted capabilities such as rview(1) and rvim(1).

Vim stands for Vi IMproved. Most of Vim was made by Bram Moolenaar, but only through the help of many others.


MODE

:set showmode
  • normal mode
  • insert mode
  • visual mode
    • visual line mode
    • visual block mode

normal mode i -> insert mode <Esc> -> normal mode normal mode v -> visual mode <Esc> -> normal mode normal mode V -> visual line mode <Esc> -> normal mode normal mode CTRL-V -> visual block mode <Esc> -> normal mode normal mode R -> replace mode <Esc> -> normal mode

editing commands
aappend 拼接到光标位置iinsert 插入到光标位置
A光标行尾拼接I光标行首插入
o光标下方新增一行O光标上方新增一行
s删除光标所在字符S删除光标所在行

undo & redo

undo & redo
uundoes the last edit
CTRL-R(redo) to reverse the preceding command.undoes the undo
U(undo line) undoes all the changes made on the last line that was edited

Getting out

  • ZZ command writes the file and exits
  • :wq write and quit
  • :w write
  • :q quit
  • :q! fource quit without write

Moving around

moving
wto move the cursor forward to the start of the next wordbto move the cursor backward to the start of the previous word
eto move the cursor forward to the end of the next wordgeto move the cursor backward to the end of the previous word
0to move the cursor to the very first character of the line$to move the cursor to the end of a line
^to move the cursor to the first non-blank character of the line
one of the most useful movement commandssingle-character search command
fxsearches forward in the line for the single character xFxthe backward version of fx command
txworks like the fx command, except it stops one character before the searched character.Txthe backward version of tx command
%括号()[]{}匹配跳转
G跳转到末行nG跳转到n行
gg跳转到首行
H移动光标到当前页的第一行
M移动光标到当前页的中间行
L移动光标到当前页的最后一行
to scroll one line at a time一次滚动一行
CTRL-Escroll upCTRL-Yscroll down
To scroll a whole screen at a time一次滚动一屏
CTRL-FTo scroll forwardCTRL-BTo scroll backward
zzTo see the context of the line with the cursor将光标所在行滚动到屏幕中间位置
ztput the cursor line at the top
zbput the cursor line at the bottom
simple searches
*从上往下搜索光标所在单词#从下往上搜索光标所在单词
/includen 从上往下搜索 includeN 从下往上搜索 include
3/includegoes to the third match of include
?includen 从下往上搜索 includeN 从上往下搜索 include
4?includegoes the the fouth match of include
.*[]^%/\?~$特殊字符需要使用 \ 转义
<\only matches at the begining of a word\>only matches the end of a word
/the\>搜索 the 结尾的单词/<\the搜索 the 开头的单词
/<\the\>仅搜索 the
/the$以 the 开头的行/^the以 the 结尾的行
/^the$仅有 the 的行,前后不能有空格等其他字符
c.mcom、cam、cum等the\.转义点 搜索 the.
named marks
mamarks the place under the cursor as mark a:marks查看所有marks;最多设置26个marks a到z
`a反引号移动到 a mark 所在位置’a单引号 移动到 a mark所在行的开头
:set ignorecase搜索时忽略大小写:set noignorecase搜索时不忽略大小写
:set hlsearch高亮显示搜索结果:set nohlsearch关闭高亮
:nohlsearchonly remove the highlightdoes not reset the option
        This is a line with example text
          x-->-->->----------------->
             w  w w    3w

        This is a line with example text
        <----<--<-<---------<--x
           b   b b    2b      b

        This is a line with example text
           <----<----x---->------------>
           2ge   ge     e       2e

                  ^
             <-----------x
        .....This is a line with example text
        <----------------x   x-------------->
                0                  $

        To err is human.  To really foul up you need a computer.
        ---------->--------------->
            fh           fy

        To err is human.  To really foul up you need a computer.
                  --------------------->
                           3fl

        To err is human.  To really foul up you need a computer.
                  <---------------------
                            Fh

        To err is human.  To really foul up you need a computer.
                   <------------  ------------->
                        Th              tn

                            %
                         <----->
                if (a == (b * c) / d)
                   <---------------->
                            %

            |   first line of a file   ^
            |   text text text text    |
            |   text text text text    |  gg
        7G  |   text text text text    |
            |   text text text text
            |   text text text text
            V   text text text text    |
                text text text text    |  G
                text text text text    |
                last line of a file    V

                        +---------------------------+
                H -->   | text sample text          |
                        | sample text               |
                        | text sample text          |
                        | sample text               |
                M -->   | text sample text          |
                        | sample text               |
                        | text sample text          |
                        | sample text               |
                L -->   | text sample text          |
                        +---------------------------+

                                       +----------------+
                                       | some text      |
                                       | some text      |
                                       | some text      |
        +---------------+              | some text      |
        | some text     |  CTRL-U  --> |                |
        |               |              | 123456         |
        | 123456        |              +----------------+
        | 7890          |
        |               |              +----------------+
        | example       |  CTRL-D -->  | 7890           |
        +---------------+              |                |
                                       | example        |
                                       | example        |
                                       | example        |
                                       | example        |
                                       +----------------+

        +------------------+             +------------------+
        | earlier text     |             | earlier text     |
        | earlier text     |             | earlier text     |
        | earlier text     |             | earlier text     |
        | earlier text     |   zz  -->   | line with cursor |
        | earlier text     |             | later text       |
        | earlier text     |             | later text       |
        | line with cursor |             | later text       |
        +------------------+             +------------------+

        /the

        the solder holding one of the chips melted and the
        xxx                       xxx                  xxx

       /the$

        the solder holding one of the chips melted and the
                                                       xxx

       /^the

        the solder holding one of the chips melted and the
        xxx

        c.m

        We use a computer that became the cummin winter.
                 xxx             xxx      xxx

        ter.

        We use a computer that became the cummin winter.
                      xxxx                          xxxx

        ter\.

        We use a computer that became the cummin winter.
                                                    xxxx

             |  example text   ^             |
        33G  |  example text   |  CTRL-O     | CTRL-I
             |  example text   |             |
             V  line 33 text   ^             V
             |  example text   |             |
       /^The |  example text   |  CTRL-O     | CTRL-I
             V  There you are  |             V
                example text

        '       The cursor position before doing a jump
        "       The cursor position when last editing the file
        [       Start of the last change
        ]       End of the last change

编辑

编辑
ddelete 删除指令(剪切)x=dlxdl 的快捷指令
dw从光标位置开始删除一个单词(包括单词后面的空格,不包括光标前内容)2d4w执行两次,删除4个单词
de从光标位置开始删除一个单词(不包括单词后面的空格,不包括光标前内容)d2e执行一次,删除两个单词
d$=D从光标位置开始删除到行尾d^从光标位置开始删除到行首
dh=Xdjdkdl=x
dd删除光标所在行
df>d 删除到 f> 搜索 ***>***的位置
cchange 修改指令(开启 Insert mode)
c2wbec change 操作符2w 移除两个单词,并开启Insert modebe 输入内容
cwd, 会开启 Insert mode2c2w
ced, 会开启 Insert modec2e
c$d, 会开启 Insert modec^d, 会开启 Insert mode
cc=S删除光标所在行并开启 Insert mode
cis删除光标所在句子(不包括句子末尾的空格)cas删除光标所在句子(包括句子末尾的空格)
chcjckcl=s
rreplace 替换指令等待输入,替换光标位置的字符,不会切换到 Insert mode
rT将光标位置替换为 TclT<Esc> or sT<Esc>功能同 rT
5rx从光标位置开始,将五个字符替换为 x
r<Enter>将光标位置替换为换行4r<Enter>将光标位置开始的四个字符,替换为换行
.重复执行变更指令
ppaste 粘贴指令xp快速转换光标字符与其后字符的位置
yyank 复制指令
ywdy2w
yedy2e
y$dy^d
yy复制光标所在行
yhyjykyl
"*yy复制到 * 号粘贴板"*p* 粘贴板粘贴
"+yy复制到 + 号粘贴板"+p+ 粘贴板粘贴
*寄存器,与系统粘贴板交互+寄存器与系统粘贴板交互
        To err is human. To really foul up you need a computer.
                         ------------------>
                                 d4w

        To err is human. you need a computer.
###
        To err is human. you need a computer.
                        -------->
                           d2e

        To err is human. a computer.
###
        To err is human. a computer.
                       ------------>
                            d$

        To err is human
###
        To err is human
           ------->
             c2wbe<Esc>

        To be human
###
        c       the change operator
        2w      move two words (they are deleted and Insert mode started)
        be      insert this text
        <Esc>   back to Normal mode
###
        x  stands for  dl  (delete character under the cursor)
        X  stands for  dh  (delete character left of the cursor)
        D  stands for  d$  (delete to end of the line)
        C  stands for  c$  (change to end of the line)
        s  stands for  cl  (change one character)
        S  stands for  cc  (change a whole line)
###
        there is somerhing grong here
        rT           rt    rw

        There is something wrong here
###
        There is something wrong here
                           5rx

        There is something xxxxx here
###
                              To <B>generate</B> a table of <B>contents
        f<   find first <     --->
        df>  delete to >         -->
        f<   find next <           --------->
        .    repeat df>                     --->
        f<   find next <                       ------------->
        .    repeat df>                                     -->
###
        /four<Enter>    find the first string "four"
        cwfive<Esc>     change the word to "five"
        n               find the next "four"
        .               repeat the change to "five"
        n               find the next "four"
        .               repeat the change
                        etc.
###
        teh     th     the
         x       p
###
x       delete character under the cursor (short for "dl")
X       delete character before the cursor (short for "dh")
D       delete from cursor to end of line (short for "d$")
dw      delete from cursor to next start of word
db      delete from cursor to previous start of word
diw     delete word under the cursor (excluding white space)
daw     delete word under the cursor (including white space)
dG      delete until the end of the file
dgg     delete until the start of the file

vim
按边界*操作
yi*复制边界内内容yi“复制双引号内的内容
ya*复制边界以及内容ya{复制双引号及其内的内容
di*删除边界内内容di“删除双引号内的内容
da*删除边界以及内容da{删除双引号及其内的内容
ci*删除边界内内容,并切换为i模式ci“-
ca*删除边界以及内容,并切换为i模式ca{-
-
copy & cut
ggyG复制全部gg“+yG复制全部到系统粘贴板
:7,10y复制[7,10]行不支持指定粘贴板
:7,10 copy 12相当于先复制[7,10]行,然后在第12行按p:7,10 co 12相当于先复制[7,10]行,然后在第12行按p
:7,10d剪切[7,10]行不支持指定粘贴板
:7,10 move 12相当于先剪切[7,10]行,然后在第12行按p:7,10 m 12相当于先剪切[7,10]行,然后在第12行按p
yy复制行yy、y1y复制光标所在行
yw复制单词yw、y1w复制光标及其后的1个单词
dd剪切行dd、d1d剪切光标所在行
dw剪切单词dw、d1w剪切光标及其后的1个单词
p在当前行下方粘贴内容P在当前行上一行粘贴内容
-
:reg查看所有粘贴板中内容
“+y将内容复制到系统粘贴板
“+p粘贴系统粘贴板中的内容
-
:s/old/new将当前行第一个old替换成new
:s/old/new/g当前行所有old替换成new
:%s/old/new所有行第一个old替换成new
:%s/old/new/g所有old替换成new
-
visual model
v切换到visual model
ggvG全选
vjjdv切换到visual model,jj选择从光标所在行开始的往下两行,d剪切
vjjyv切换到visual model,jj选择从光标所在行开始的往下两行,y复制
-
visual block mode
Ctrl v切换到visual block model
yblock yink将选择的块,复制到粘贴板
pblock paste粘贴粘贴板中的内容
xblock delete删除选择的块
cblock insert输入后 使用ESC完成insert
-
split
:sp水平分割窗口:vsp垂直分割窗口
Ctrl+w+s水平分割窗口Ctrl+w+v垂直分割窗口
:sp xxx.txt水平分割窗口并在新窗口打开xxx.txt:vsp xxx.txt垂直分割窗口并在新窗口打开xxx.txt
-
vim -On-O垂直分割、-n分割窗口数vim -on-o水平分割、-n分割窗口数
vim -O2 file1 file2垂直分割窗口打开file1 file2vim -o2 file1 file2水平分割窗口打开file1 file2
-
:only仅保留当前分屏:hide关闭当前分屏
Ctrl w c关闭当前窗口Ctrl w q关闭当前窗口,如果只剩最后一个窗口,则退出vim
-
Ctrl w, h、j、k、l切换窗口Ctrl w, H、J、K、L移动窗口到最左下上右
Ctrl w r互换窗口
:resize 30调整当前窗口高度
Ctrl w =所以窗口统一高度
Ctrl w 1+当前窗口高度+1Ctrl w -当前窗口高度-1
Ctrl f向前翻一页Ctrl b向后翻一页
Ctrl u向前翻半页Ctrl d向后翻半页

set

:set
:set ff=unixunix格式 行结尾 \n:set ff=dosdos格式 行结尾 \r\n:set ff查看行结尾格式
:set nu or :set number显示行号:set nonu or :set nonumber隐藏行号
:set wrap折行:set nowrap取消折行
:set hlsearch高亮显示搜索结果:set nohlsearch取消高亮显示搜索结果
:set autoread文件更新自动读取新文件:set noautoread取消文件更新自动读取新文件

.vimrc

~/.vimrc

set nu
set nocompatible
set laststatus=2
set statusline=%F%m%r%h%w%=(%{&ff}/%Y)\ (line\ %l\/%L,\ col\ %c)
set linespace=3
set cursorline
set cursorcolumn

highlight CursorLine term=reverse,bold cterm=reverse,bold ctermfg=DarkGray ctermbg=black guibg=Grey90
highlight CursorColumn term=reverse,bold cterm=reverse,bold ctermfg=DarkGray ctermbg=black guibg=Grey90

syntax enable
filetype plugin indent on
let g:rustfmt_autosave = 1

~/_vimrc

" 将当前行向上移动一行
inoremap <M-k> <Esc>kddpk
nnoremap <M-k> kddpk
" 将当前行向下移动一行
inoremap <M-j> <Esc>ddp
nnoremap <M-j> ddp

set ff=unix
" 去掉有关vi一致性模式,避免以前版本的bug和局限
set nocompatible
" indent :set indent :set ai等自动缩进,想使用backspace将字段缩进的删除,必须设置这个选项。否则不响应
" eol 如果 insert 模式再行开头想使用backspace合并两行,需要设置eol
" start 想要删除此次插入前的输入,需要设置start
set backspace=indent,eol,start
set nu
set guifont=JetBrains_Mono_SemiBold:h12
set laststatus=2  "显示状态栏
set statusline=%F%m%r%h%w%=(%{&ff}/%Y)\ (line\ %l\/%L,\ col\ %c)
"上面的状态栏展示信息比较多,,可以如上所示进行集合性配置,如果懒得一一理解,可直接复制进配置文件,因为所有配置对于提升你编程效率都有帮助。当然如果你不嫌麻烦,也可以以下面所示形式单独配置(注意去掉前面”号)
"set statusline+=%{&ff}  "显示文件格式类型
"set statusline+=%h  "帮助文件标识
"set statusline+=%m  "可编辑文件标识
"set statusline+=%r  "只读文件标识
"set statusline+=%y  "文件类型
"set statusline+=%c  "光标所在列数
"set statusline+=%l/%L  "光标所在行数/总行数
"set statusline+=\ %P  "光标所在位置占总文件百分比

" Show a few lines of context around the cursor. Note that this makes the text
" scroll if you mouse-click near the start or end of the window.
set scrolloff=5
set linespace=3
" Don't use Ex mode, use Q for formatting.
map Q gq

" rust.vim start "
syntax enable
filetype plugin indent on
" let current_compiler = 'rustc'
let g:rustfmt_autosave = 1
let g:rustfmt_fail_silently = 0
" let g:syntastic_rust_checkers = ['cargo']
" rust.vim end"

plugin

rust.vim

注意

vim的~/.vimrc配置项,屏蔽掉下面这句话:

set fileencodings=utf-8,gb2312,gbk,gb18030,ucs-bom

再用 vim 打开jpeg文件,显示ffd8 ffc0 0011 0804ffd9 0a,显示正确
vim 为了支持识别和显示中文,规定了 vim 的 fileencodings
当vim打开文件时,会使用规定的编码格式对数据进行解析
jpeg的文件头FFD8、尾FFD9 不是任何一个中文的编码,vim找不到对应的中文字,就显示为??,即:3f3f