• vi 常用命令 (未完)


    • 我想在这儿记录一些自己用到过的命令, 所以这儿不一定是命令大全, 不一定系统, 理解的也不一定完全正确。
    • 本人并不想用vi去做编程工作, 虽然这很酷。 个人还是喜欢那些所谓笨重的 IDE, 但是常常不得不用在command line 环境用vi去修改一些脚本或者进行简单的编辑。

    一. vi 打开多个文件

    1. 在不同窗口中打开多个文件:

    :n 切换到下一个文件 (n=next)
    :N 切换到上一个文件

    vi file1 file2 file3

    2. 拆分窗口打开多个文件:

    如果已经打开一个了一个文件,则在vi的命令输入状态下输入下面命令, 就可以在一个窗口垂直方向打开多个文件了。 据说用:vsp可以水平方向切分窗口,但是我的版本不支持。

    :sp         另外一个文件的路径及文件名

    :new xxx    感觉和 :sp 没什么区别

    ctrl+w s    对当前文档内容分屏显示

    ctrl+w q    关闭所处分屏

    ctrl+w o    仅显示当前分屏内容

    ctrl+w j k  上下选择分屏

    或者一开始就分屏打开多个文件。 可以使用 ctrl + 两次按 w 或者ctrl + w 然后按上下键在上下窗口间切换。

     vi -o file1 file2 file3....

    二. vi 撤销操作

    1. vi 中每次修改都会有个标签 #1 这种, 来标识本次修改。 当你反复按 u 的时候, 是可以撤销和反撤销当前这次修改的。 

    2. ctrl + r 的作用则是在不同的修改之间切换, 也就是在#1, #2 ... 之间切换。 切换之后, 仍然可以按 u 来进行本次修改的撤销和反撤销。

    3. U 的作用是撤销本行的所有操作, 而撤销的范围是本次在这一行的所有更改。也就是说, 如果你在第一行改了2次, 又去第二行改了2次, 又回来第一行改了两次, 那么 U 只能撤销第一行最后这2次操作。

    4. U 和 u 的区别在于, 用u的时候, 是不会增加修改(#NO.)的, 而每次使用U则会被认为是一次修改, 而增加#NO. 。

    上面都是我自己试验的, 如有错误, 恳请指正。

    三. vi 复制 剪切 粘贴 

    (这部分摘录:http://blog.csdn.net/arganzheng/article/details/6260690 写的很好)

    1. 主要步骤:

    1.  mark the region you want to cut/copy
    2.  cut/copy it
    3.  move to where you want to paste
    4.  paste it

    2. 这几个步骤在VI中分别对应VI的几个命令。下面简单介绍步骤一。

    方法1: 使用vi的“书签”命令:

    During a vi session, you can mark your place in the file with an invisible “bookmark,” perform edits elsewhere, and then return to your marked place.

    在命令模式下,在你想要标识的地方敲入命令mx,表示Marks the current position with x (x can be any letter). (The original vi allows only lowercase letters. Vim distinguishes between uppercase and lowercase letters.)。这样相当于在这个地方作了个书签(锚点),之后你就可以快速的跳回来了。
    'x: (Apostrophe.) Moves the cursor to the first character of the line marked by x.
    `x: (Backquote.) Moves the cursor to the character marked by x.
    ``: (Backquotes.) Returns to the exact position of the previous mark or context after a move.
    '': (Apostrophes.) Returns to the beginning of the line of the previous mark or context.
    这是书签的主要作用,但是可以利用他来标识一个区域作删除。
    这是因为d/y命令(事实上是所有VI命令)的一个很明显的特征:action+position。
    于是得到利用书签作cut/copy详细步骤:

    Cut and Paste:

    1. Place the cursor at the beginning of the block you want to CUT.
    2. Mark it with md
    3. Go to the end of the block.
    4. Cut it with d'd
    5. Go to the new location that you want to PASTE those text.
    6. Press P.

    Copy and Paste:

    1. Place the cursor at the beginning of the block you want to COPY.
    2. Mark it with my
    3. Go to the end of the block.
    4. Copy it with y'y
    5. Go to the new location that you want to PASTE those text.
    6. Press P.

    The name of the mark used is related to the operation (d:delete or y:yank).

    方法2: 表示文本区域的方式是使用VI的Visual Mode可视化选取,这个就非常像GUI了,只是是用键盘而不是鼠标来移动光标。

    Cut and paste:

    1. Position the cursor where you want to begin cutting.
    2. Press v (or upper case V if you want to cut whole lines).
    3. Move the cursor to the end of what you want to cut.
    4. Press d.
    5. Move to where you would like to paste.
    6. Press p to paste after the cursor, or P to paste before.

    Copy and paste can be performed with the same steps, only pressing y instead of d in step 4.

    四. vi 配置文件

    在 ~ 目录下创建一个文件 .vimrc , 这个文件会被 vi 当作配置文件:

    set nu               显示行号 
    set ts=4             设置 tab 的长度为4个空格
    set shiftwidth=4     设置缩进的空格数           

    这类配置还有很多, 暂时没有用到别的。 另外, 配置文件可以改变键盘映射, 也就意味着可以自己设置键盘的功能和快捷键吧。

    五. 命令模式下光标的移动

    h         光标左移一个字符 
    l         光标右移一个字符 
    k         光标上移一行 
    j         光标下移一行 
    w         右移一个词首
    W         右移一个以空格分隔的首位置
    b      左移一个词到词首
    B      左移一个以空格分隔的首位置
    e       右移一个词尾
    ge     左移一个词到词尾 E  右移一个以空格分隔的尾位置  ) 光标移至句尾 ( 光标移至句首 } 光标移至段落开头 { 光标移至段落结尾 nG 光标移至第n行首 G 到最后一行 H 光标移至屏幕顶行 M 光标移至屏幕中间行 L 光标移至屏幕最后行
    0 (注意是数字零)光标移至当前行首 $ 光标移至当前行尾 Ctrl+u 向文件首翻半屏 Ctrl+d 向文件尾翻半屏 Ctrl+f 向文件尾翻一屏 Ctrl+b 向文件首翻一屏 :nz 将第n行滚至屏幕顶部,不指定n时将当前行滚至屏幕顶部。

    %     跳转到对应匹配的括号的位置

    六. 文本编辑命令 

    i:  在光标前 
    I:  在当前行首 
    a:  光标后 
    A:  在当前行尾 
    o:  在当前行之下新开一行 
    O:  在当前行之上新开一行 
    
    
    r:  替换当前字符 
    R:  替换当前字符及其后的字符,直至按ESC键 
    s:  从当前光标位置处开始,以输入的文本替代指定数目的字符 
    S:  删除指定数目的行,并以所输入文本代替之 
    ncw或nCW:  修改指定数目的字 
    nCC:     修改指定数目的行 
    
    yy:      将一行文本移到缺省缓冲区
    ynw:      将后面的n个词移到缺省缓冲区中
    p:      如果缺省缓冲区中包含一行文本,则在当前行后面插入一个空行井将缺省缓冲区中的声容粘贴到这一行中;如果缺省缓冲区中包含多个词,把这些词粘贴到光标的右边.
    "?nyy:将当前行及其下n行的内容保存到寄存器?中,其中?为一个字母,n为一个数字 
    "?nyw:将当前行及其下n个字保存到寄存器?中,其中?为一个字母,n为一个数字 
    "?nyl:将当前行及其下n个字符保存到寄存器?中,其中?为一个字母,n为一个数字 
    "?p:  取出寄存器?中的内容并将其放到光标位置处。这里?可以是一个字母,也可以是一个数字 
    
    ndd:       将当前行及其下共n行文本删除,并将所删内容放到1号删
    :n1,n2 co n3:  将n1行到n2行之间的内容拷贝到第n3行下 
    :n1,n2 m n3:   将n1行到n2行之间的内容移至到第n3行下  
    ndw或ndW:      删除光标处开始及其后的n-1个字 
    d0:  删至行首 
    d$:  删至行尾 
    ndd:  删除当前行及其后n-1行 
    x或X: 删除一个字符,x删除光标后的,而X删除光标前的 
    
    要删除大段的内容: 
        首先利用编辑命令“vi file”打开文件,然后将光标移到需要删除的行处按Ctrl+G显示行号,再到结尾处再按Ctrl+G,显示文件结尾的行号。  :23,1045d 假定2次得到的行号为23和1045,则把这期间的内容全删除,也可以在要删除的开始行和结束行中用ma、mb命令标记,然后利用“:a',b'd”命令删除。 

    七. 搜索及替换命令 

    /pattern          从光标开始处向文件尾搜索pattern 
    ?pattern          从光标开始处向文件首搜索pattern 
    n              在同一方向重复上一次搜索命令 
    N              在反方向上重复上一次搜索命令 
    :s/p1/p2/g         将当前行中所有p1均用p2替代 
    :n1,n2s/p1/p2/g     将第n1至n2行中所有p1均用p2替代 
    :g/p1/s//p2/g      将文件中所有p1均用p2替换 
    :%s#/usr/bin#/bin#g  可以把文件中所有路径/usr/bin换成/bin。
    
    注意: 其中s为substitute,%表示所有行,g表示global。

    八. 保存和退出

    :w           保存当前文件 
    :w filename      保存当前内容到另一个文件
    :wq           保存并退出
    :20,59w filename    把20~59行的内容写入另一个文件
    :e filename      打开文件filename进行编辑 
    :x           保存当前文件并退出 
    :q           无修改的情况下退出vi 
    :q!                 不保存文件并退出vi 
    :!command           执行shell命令command 
    :n1,n2 w!command    将文件中n1行至n2行的内容作为command的输入并执行之,若不指定n1,n2,则表示将整个文件内容作为command的输入 
    :r!command          将命令command的输出结果放到当前行 

    九. 保存和退出 

     :pwd           查看vim运行的目录        
    Ctrl + g      查看相对于vim运行目录的文件路径,并显示行号

    === 下面的是摘自 : http://www.worldtimzone.com/res/vi.html  ===

    Cursor movement

    • h - move left
    • j - move down
    • k - move up
    • l - move right
    • w - jump by start of words (punctuation considered words)
    • W - jump by words (spaces separate words)
    • e - jump to end of words (punctuation considered words)
    • E - jump to end of words (no punctuation)
    • b - jump backward by words (punctuation considered words)
    • B - jump backward by words (no punctuation)
    • 0 - (zero) start of line
    • ^ - first non-blank character of line
    • $ - end of line
    • G - Go To command (prefix with number - 5G goes to line 5)

    Note: Prefix a cursor movement command with a number to repeat it. For example, 4j moves down 4 lines.

    Insert Mode - Inserting/Appending text

    • i - start insert mode at cursor
    • I - insert at the beginning of the line
    • a - append after the cursor
    • A - append at the end of the line
    • o - open (append) blank line below current line (no need to press return)
    • O - open blank line above current line
    • ea - append at end of word
    • Esc - exit insert mode

    Editing

    • r - replace a single character (does not use insert mode)
    • J - join line below to the current one
    • cc - change (replace) an entire line
    • cw - change (replace) to the end of word
    • c$ - change (replace) to the end of line
    • s - delete character at cursor and subsitute text
    • S - delete line at cursor and substitute text (same as cc)
    • xp - transpose two letters (delete and paste, technically)
    • u - undo
    • . - repeat last command

    Marking text (visual mode)

    • v - start visual mode, mark lines, then do command (such as y-yank)
    • V - start Linewise visual mode
    • o - move to other end of marked area
    • Ctrl+v - start visual block mode
    • O - move to Other corner of block
    • aw - mark a word
    • ab - a () block (with braces)
    • aB - a {} block (with brackets)
    • ib - inner () block
    • iB - inner {} block
    • Esc - exit visual mode

    Visual commands

    • > - shift right
    • < - shift left
    • y - yank (copy) marked text
    • d - delete marked text
    • ~ - switch case

    Cut and Paste

    • yy - yank (copy) a line
    • 2yy - yank 2 lines
    • yw - yank word
    • y$ - yank to end of line
    • p - put (paste) the clipboard after cursor
    • P - put (paste) before cursor
    • dd - delete (cut) a line
    • dw - delete (cut) the current word
    • x - delete (cut) current character

    Exiting

    • :w - write (save) the file, but don't exit
    • :wq - write (save) and quit
    • :q - quit (fails if anything has changed)
    • :q! - quit and throw away changes

    Search/Replace

    • /pattern - search for pattern
    • ?pattern - search backward for pattern
    • n - repeat search in same direction
    • N - repeat search in opposite direction
    • :%s/old/new/g - replace all old with new throughout file
    • :%s/old/new/gc - replace all old with new throughout file with confirmations

    Working with multiple files

    • :e filename - Edit a file in a new buffer
    • :bnext (or :bn) - go to next buffer
    • :bprev (of :bp) - go to previous buffer
    • :bd - delete a buffer (close a file)
    • :sp filename - Open a file in a new buffer and split window
    • ctrl+ws - Split windows
    • ctrl+ww - switch between windows
    • ctrl+wq - Quit a window
    • ctrl+wv - Split windows vertically

    Another good vim commands cheatsheet

  • 相关阅读:
    2019-2020-2 20175212童皓桢《网络对抗技术》 Exp4 恶意代码分析
    2019-2020-2 20175212童皓桢《网络对抗技术》Exp3 免杀原理与实践
    2019-2020-2 20175212童皓桢《网络对抗技术》Exp2 后门原理与实践
    2019-2020-2 20175212童皓桢《网络对抗技术》 Exp1 PC平台逆向破解
    2019-2020 《信息安全系统设计》20175212童皓桢 ucosii-2
    2019-2020-1 20175212_20175227《信息安全系统设计基础》
    实现mypwd
    2019-2020-1 20175212童皓桢《信息安全系统设计》实验四 外设驱动程序设计
    2019-2020-1 20175212童皓桢《信息安全系统设计》 实验三并发程序
    20175212-20175227 实验二 固件程序设计
  • 原文地址:https://www.cnblogs.com/beautiful-scenery/p/3576349.html
Copyright © 2020-2023  润新知