Vi

Vi 编辑器基础操作

Editors

  • Programs that let you interactively edit text files,即交互式编辑文本文件的程序有:
    • DOS: edlin, edit
    • Windows: notepad, wordpad
    • Unix: ed, vi, vim
    • GNU: emacs
    • xedit, gedit, nano, pico
  • 文字处理器,带排版、字体、图片、分页等能力,保存的通常不是纯文本(如 .doc/.docx/.odt/.pdf 等)
    • WordStar, MSWord, Framemaker, Acrobat…
    • StarOffice, OpenOffice, LibreOffice

vi Modes

  • Command Mode 命令模式
    • vi 启动后为命令模式
    • 每一个按键执行一个编辑命令
    • 输入 : 进入命令行模式(command line mode)
  • Insert Mode
    • 只有在 Insert mode 下,才可以做文字输入, [ESC] 键可回到命令模式
    • 通过输入几个插入命令之一来进入
  • 简单 vi 示例
    • vi tmp.txt #在命令模式启动
    • a #进入输入模式,在光标后追加append内容
    • ESC #退出输入模式,回到命令模式
    • :wq #保存write并退出quit

Easier Ways for Practicing

  • vim:vi 的升级版
  • vimtutor:参考教材,这是 Vim 自带的交互式教程,它不是编辑器,而是教你用编辑器的课本
  • gvim:带 GUI 的 vi

Cursor Movement(命令模式)

  • 可以用方向箭头键进行光标移动
  • 可以用 hjkl 代替箭头
    • [n] h #左移 n 位
    • [n] j #下移 n 位
    • [n] k #上移 n 位
    • [n] l #右移 n 位
  • 还有许多更高级的光标移动命令

屏幕间移动(命令模式)

  • ^F #Forward one screen
  • ^B #Back one screen
  • ^D #Down half screen
  • ^U #Up half screen

行内移动(命令模式)

  • $ #end of current line,到行
  • ^ #beginning of text on current line,到本行第一个非空字符,主要是跳过Tab或者空格等
  • 0 #beginning of current line,到行

单词间移动(命令模式)

  • [n] w #forward [n] word(s),跳到下第n个词开头
  • [n] b #back [n] word(s),跳到上第n个词开头
  • e #end of word,走到当前光标所在的这个词的最后

搜索跳转

行内字符搜索(命令模式)
  • f character #forward to next this character,移动到下一个这个字母
全文搜索(命令模式)
  • / pattern string #go to matched pattern downward,向下搜索pattern string
  • ? pattern string #go to matched pattern upward,向上搜索pattern string
  • 上面这两个和man里面的查找操作是一样的

下面这两个是在/ 时使用的

  • n:跳到下一个匹配(next)
  • N:跳到上一个匹配(反方向的 next)
  • 一般来说,如果小写是对后面进行操作,大写就是反向

行间跳转(命令模式)

  • G Go to last line of file
  • [n] G go to line [n],Go 到第n行

Inserting Text

这些命令都是在命令模式下按的,只是进入插入模式的位置不同而已

  • i #insert text before the cursor,在当前的光标前插入东西
  • a #append text after the cursor,在当前的光标后插入东西
  • I #Insert text at beginning of line,在行首加入东西 第一个非空字符
  • A #Append text at end of line,在行末加入东西
  • o #open new line after current line,在当前行后面加一行,插入东西
  • O #Open new line before current line,在当前行前面加一行,插入东西

大写一般都是对的操作

Deleting Text

这些命令是如何在命令模式下删东西

  • dd #delete current line,会删到缓存区buffer,可以用 p 粘贴(复制到光标后面),所以删除其实也等于剪切
  • [n] dd or d [n] d #delete [n] line(s),3dd或者d3d从当前行开始删3行
  • [n] dw #delete [n] word(s)
  • D #Delete from cursor to end of line,从光标删到行尾,一般大写都是对行的操作
  • x #delete current character,删除当前的字符
  • [n] x #delete [n] characters
  • [n] X #delete previous [n] character,删除光标之前的字符,相当于backspace

Changing Commands

这些命令也都还是在命令模式下执行

  • s/S #Substitute,将该处 character 删除,并进入插入模式,等待你输入新的字符,S是删除一整行
  • r,把光标下的字符直接替换replace成你接下来输入的那一个字符,但仍然是命令模式
  • R,进入替换模式:你打字会持续覆盖后面的字符,直到按ESC键退出
  • cw #change word,从光标位置开始,把“到单词结束”的内容删掉,然后进入插入模式让你输入新词
  • [n]cw #change next [n] word(s),删除n个单词,然后进入插入模式让你输入新词
  • c$ #change from cursor to end of line,从光标位置开始,到行尾$全部清掉,然后进入插入模式
  • ~ #change case of character,把光标下字母大小写翻转
  • J #Join current line and next line,把下一行移到该行后面
  • u #undo the last command just done,即撤回
  • . #repeat last change, redo,重复上一次动作
  • [n]yy or y[n]y #yank [n] line(s) to buffer,yank复制
  • [n]yw #yank [n] word(s) to buffer,复制单词
  • p #put yanked or deleted text after cursor,复制到光标后面(如果是一整行,就复制到光标所在行的后面一行)
  • P #Put yanked or deleted text before cursor,复制到前面,小写对后操作,大写一般就是对前面操作
  • 4dd10jp
    • #1 4dd 删除 4 行
    • #2 10j 光标下移 10 行
    • #3 p 将删除的 4 行复制到光标后

Command Line Mode命令行模式

  • Enter command line mode by typing a :,输入:可以到命令模式
  • Line indicators 行定位符
    • :``20:移动到第 20 行,如 :20
    • :``. current line:将光标移至本行首
    • :``$ last line:将光标移至整文的行末
    • :``+, -, .+5, -10:光标向上向下移动几行;$-3 意思为光标先移动到最后一行,然后再向上移动 3 行

File Manipulation

  • :wq #write changes and quit
  • :w! # ! force overwrite of file,即强制保存
  • :q #quit if no changes made
  • :q! #quit without saving changes ! ,即强制退出,不保存退出
  • :![cmd] #shell escape,即暂时跳到外面的 shell 里 ! 执行[cmd]例如 :!ls:!pwd
  • :r [file] # r=read insert file at cursor position,即把其它文件的内容插入到后面,默认会换行

Configuring vi Session

  • :set all #display all option settings,显示所有的选项设置(q退出)
  • :set ignorecase #ignore the case of a character in a search,先设置再搜索,忽略大小写查找,使用 set noignorecase 进行关闭
  • :set number #display line numbers,显示行号 或者:set nu
  • :set nonumber #turn off line numbers,关闭行号显示

Line Manipulation

  • :number #locate,移动到对应行
  • :n1,n2 m n3 #move,将 n1 到 n2 行,移动到 n3 行后的位置,会自动换行
  • :n1,n2 d #delete
    • :.,$-2 d #删除当前行到倒数第 3 行,.当前行,$最后一行
    • :1,. d #删除第 1 行到当前行

Substitution

  • :[Addr]s/old-expr/new-string/[g]
    • [Addr]表示“在哪些行做替换”,不写只对当前行
    • s:表示替换substitude操作,g 表示全局global替换,省略 option 时仅对每行第一个匹配串进行替换
    • :s/hte/the/g #当前行所有 hte 换为 the
    • :2,30s/use/used #将 2-30 行每行第一个 use 换位 used
    • :1,$s/``Oct\.``/Nov. #将 1 到最后一行每行第一个 Oct. 换成 Nov.。注意,这里将 . 视为普通的点,所以文本匹配的是 Oct. 而不是 Oct
    • :1,$-3s/Oct\./Nov./g #将 1 到倒数第 4 行所有 Oct. 换成Nov.
Linux & Unix
Vi(详细版)

评论区