vi&vim 命令详解

vi&vim 用于编辑文件内容

vim的四种模式
• 正常模式 (Normal-mode)
复制、粘贴
• 插入模式 (Insert-mode)
i,进入编辑内容
• 命令模式 (Command-mode)
保存、退出
Esc, shift+:, qw、q!
• 可视模式 (Visual-mode)

1、正常模式
进入其他模式转换命令
• i I a A o O 进入插入模式(几种模式区别在于光标位置不同)
• v V ctrl+v 进入可视化模式
• : 进入命令模式
• esc 从其他模式回到正常模式

基本操作
• h 光标左移
• j 下移
• k 上移
• l 光标右移
• y 复制
yy,复制整行
数字+yy,复制n行
• d 剪切
• p 粘贴
• u 撤销
• ctrl + r 重做
• x 删除单个字符
• r 替换单个字符
• G 定位指定的行
• ^ 定位到行首
• $ 定位到行尾
• :w 写入
• :q 退出
• :! 执行 Shell 命令
• :s 替换
• / 查找
• :set 设置命令
set nu 显示行号

例1,一次复制n行
初始内容,光标定位在b行:
aaa
bbb
ccc
ddd
eee
fff

操作5yy,然后到文档最末尾,按p,结果如下:
aaa
bbb
ccc
ddd
eee
fff


bbb
ccc
ddd
eee
fff

例2,剪切光标位置到结尾的内容
光标定位在第二个f的位置,按d+$(shift+4)
fff

换行,按p,结果如下:
f
ff


例3,vim中显示行号
set nu,结果如下:
  1 aaa
  2 bbb
  3 ccc
  4 ddd
  5 eee
  6 fff
  7
  8
  9 bbb
 10 ccc
 11 ddd
 12 eee
 13 faf

2、命令模式 (Command-mode)
保存、退出、查找等
Esc之后,输入:,进入命令模式

例1,保存当前编辑的文件到指定位置
:w /root/abc.txt

例2,保存当前编辑的文件并退出
:wq 

例3,不保存当前编辑的文件并退出
:q!

例4,直接退出
:q

例5,结合其它命令使用,使用:!+命令
:!ifconfig
可以查看ip,回车返回vim

例6,查找内容
/要查找的内容  按n查找下一个内容,shift+n查找上一个内容

例7,当前行查找并替换内容
原内容:bbb
执行命令:s/b/B
替换后的内容:Bbb

例8,全文查找每行第一个匹配的字符并替换内容
执行命令:%s/b/B

例9,全文查找匹配的字符并全部替换内容
执行命令:%s/b/B/g
vim会给出替换结果:3 substitutions on 2 lines

例10,在指定范围的行内进行查找匹配的字符并全部替换内容
执行命令:1,5s/B/b/g
vim会给出替换结果:3 substitutions on 2 lines

例11,显示和不显示行号
:set nu
:set nonu

3、可视模式
三种进入可视模式的方式
• v 字符可视模式
• V 行可视模式
• ctrl+v 块可视模式
• 配合 d 和 I(大写 i ) 命令可以进入块的便利操作

命令格式:
vim [options] [file ..]
vim [options] –
vim [options] -t tag
vim [options] -q [errorfile]

DESCRIPTION
       Vim is a text editor that is upwards compatible to Vi.  It can be used to edit all kinds of plain text.  It is
       especially useful for editing programs.

       There are a lot of enhancements above Vi: multi level undo, multi windows and  buffers,  syntax  highlighting,
       command  line editing, filename completion, on-line help, visual selection, etc..  See ":help vi_diff.txt" for
       a summary of the differences between Vim and Vi.

       While running Vim a lot of help can be obtained from the on-line help system, with the ":help"  command.   See
       the ON-LINE HELP section below.

       Most often Vim is started to edit a single file with the command

            vim file

       More generally Vim is started with:

            vim [options] [filelist]

       If the filelist is missing, the editor will start with an empty buffer.  Otherwise exactly one out of the fol‐
       lowing four may be used to choose one or more files to be edited.

       file ..     A list of filenames.  The first one will be the current file and read into the buffer.  The cursor
                   will  be  positioned  on  the  first  line of the buffer.  You can get to the other files with the
                   ":next" command.  To edit a file that starts with a dash, precede the filelist with "--".

       -           The file to edit is read from stdin.  Commands are read from stderr, which should be a tty.

       -t {tag}    The file to edit and the initial cursor position depends on a "tag", a sort of goto label.   {tag}
                   is  looked  up  in  the tags file, the associated file becomes the current file and the associated
                   command is executed.  Mostly this is used for C programs, in which case {tag} could be a  function
                   name.   The effect is that the file containing that function becomes the current file and the cur‐
                   sor is positioned on the start of the function.  See ":help tag-commands".

       -q [errorfile]
                   Start in quickFix mode.  The file [errorfile] is read  and  the  first  error  is  displayed.   If
                   [errorfile]  is  omitted,  the  filename  is  obtained  from  the  'errorfile' option (defaults to
                   "AztecC.Err" for the Amiga, "errors.err" on other systems).  Further errors can be jumped to  with
                   the ":cn" command.  See ":help quickfix".

       Vim behaves differently, depending on the name of the command (the executable may still be the same file).

       vim       The "normal" way, everything is default.
 Manual page vi(1) line 1 (press h for help or q to quit)