没有理想的人不伤心

快速上手 Vim 编辑器

2024/06/07
2
0

image.png

vimtutor

vim 自带教程

终端输入 vimtutor 进入 vim 教程

vim 和系统不共用一个剪切板

从 vim 粘贴到系统剪切板:"+y

从系统剪切板复制到 vim:"+p

lesson 1

光标控制:hjkl 分别代表左下上右

normal 模式 “ESC”

退出 “:q”

强制退出(不做更改)“:q!”

insert 插入模式 “i”

apend 模式 “A” (光标自动移到行尾的插入模式)

delete 删除 “x”

保存并退出 “:wq”

summary

  1. The cursor is moved using either the arrow keys or the hjkl keys.
         h(left)j (down)k (up)l (right)

  2. To start Vim from the shell prompt type:vim FILENAME <ENTER>

  3. To exit Vim type:     <ESC>   :q!   <ENTER>  to trash all changes.
             OR type:      <ESC>   :wq   <ENTER>  to save the changes.

  4. To delete the character at the cursor type:x

  5. To insert or append text type:
         i   type inserted text   <ESC>         insert before the cursor
         A   type appended text   <ESC>         append after the line

NOTE:Pressing <ESC> will place you in Normal mode or will cancel
      an unwanted and partially completed command.

Now continue with lesson 2.

lesson 2

w 和 e 的区别:一个是词首,一个是词尾

删除单词 “dw” (删除光标所在单词的光标之后的部分,删除整个单词将光标放在单词前)

删除一行中光标之后的所有内容 “d$”

删除光标之后的整个单词 “de”

光标跳跃:

  • “数字 w” 跳指定数量单词,跳到单词首字母
  • “数字 e” 跳指定数量单词,跳到单词末尾字母
  • “0” 光标跳到行首
  • “$” 光标跳到行尾
  • “数字” 跳指定多少行

删除多个: “d 数字 行为”,如 d2w,d3e,2de,3dw,数字在前也可以

删除光标所在整行 “dd”

删除多行 “d 数字 d” 如 d2d,2dd

撤销上一个命令 “u”

撤销上一个撤销 CTRL+r

撤销光标所在行的操作 “U”

summary

  1. To delete from the cursor up to the next word type:dw
  2. To delete from the cursor up to the end of the word type:de
  3. To delete from the cursor to the end of a line type:d$
  4. To delete a whole line type:dd

  5. To repeat a motion prepend it with a number:2w
  6. The format for a change command is:
               operator   [number]   motion
     where:
       operator - is what to do,such as  d  for delete
       [number] - is an optional count to repeat the motion
       motion   - moves over the text to operate on,such as  w(word),
                  e(end of word),  $ (end of the line),etc.

  7. To move to the start of the line use a zero:0

  8. To undo previous actions,type:u  (lowercase u)
     To undo all the changes on a line,type:U  (capital U)
     To undo the undos,type:CTRL-R

lesson 3

粘贴上一个删除命令删除的内容 put"p"

删除命令(x\dd\dw\de\2dd\2dw\2de\d2w\d2d)

replace 替换单个字母 “r”+“字母或数字”

进入替换 replace 模式 “R”

change 删除光标所在的单词光标之后的部分并进入 insert 模式 “ce” ( ==de+i)

change:c [number] motion

  • c$:删除该行在光标之后的所有内容并进入 insert 模式
  • cw
  • ce

summary

  1. To put back text that has just been deleted,type   p .  This puts the
     deleted text AFTER the cursor(if a line was deleted it will go on the
     line below the cursor).

  2. To replace the character under the cursor,type   r   and then the
     character you want to have there.

  3. The change operator allows you to change from the cursor to where the
     motion takes you.  eg. Type  ce  to change from the cursor to the end of
     the word,c$  to change to the end of a line.

  4. The format for change is:

         c   [number]   motion

lesson 4

显示光标所在行在文件中的位置(行数、列数、文件路径等信息)ctrl+g

"/tmp/tutorhNJmLz"line 485 of 972 --49%-- col 1

跳到文件最后一行 “G”

跳到文件第一行 “gg”

跳到文件指定行 “数字 G"或"数字 gg”

查找命令 “/” 或 “?”,查找下一个 “n” ,反方向查找 “N”

光标返回操作之前的行 ctrl+o,逆操作 ctrl+i

光标跳到与之对应的另一半括号()[]{} “%”

若没有与之匹配的另一半,则不会跳转,在程序编写中很重要

查找替换 substitute 规则 “:s/old/new/gc”

  • :30,100s/old/new/gc 指定查找 30-100 行的 old 内容替换为 new 内容,参数 g 表示 globally,全部替换,c 表示 conform,替换前询问是否替换
  • :%s/old/new/g 在整个文件中查找并替换全部,没有参数 c 则直接替换不询问,%表示在整个文件
  • :s/old/new 在当前行查找,并只替换第一个找到的。

summary

  1. CTRL-G  displays your location in the file and the file status.
             G  moves to the end of the file.
     number  G  moves to that line number.
            gg  moves to the first line.

  2. Typing  /  followed by a phrase searches FORWARD for the phrase.
     Typing?  followed by a phrase searches BACKWARD for the phrase.
     After a search type  n  to find the next occurrence in the same direction
     or  N  to search in the opposite direction.
     CTRL-o takes you back to older positions,CTRL-i to newer positions.

  3. Typing  %  while the cursor is on a(,),[,],{,or } goes to its match.

  4. To substitute new for the first old in a line type:s/old/new
     To substitute new for all'old's on a line type:s/old/new/g
     To substitute phrases between two line #'s type:#,#s/old/new/g

lesson 5

执行外部命令 “:!”+命令,如":!whoami"

另存为 “:w filename”

选中内容(会高亮显示) “v” ,使用上下左右进行选择

  • “V” 快速选中当前行
  • ctrl+v 垂直选中,选中列

选中后可以进行任何操作,如删除、复制等

将选中内容另存为:选中后输入":“,出现”:‘<,’>“,再输入"w filename”

插入指定文件中的内容 “:r filename”

插入外部指令的执行结果 “:r!命令” ,如:r!ls

summary

  1.  :!command  executes an external command.

      Some useful examples are:
         (Windows)        (Unix)
          :!dir:!ls            -  shows a directory listing.
          :!del FILENAME:!rm FILENAME   -  removes file FILENAME.

  2.  :w FILENAME  writes the current Vim file to disk with name FILENAME.

  3.  v  motion:w FILENAME  saves the Visually selected lines in file
      FILENAME.

  4.  :r FILENAME  retrieves disk file FILENAME and puts it below the
      cursor position.

  5.  :r!dir  reads the output of the dir command and puts it below the
      cursor position.

lesson 6

open 在光标方下插入一空行并进入 insert 模式 “o”

在光标上方插入一空行并进入 insert 模式 “O”

append 在光标后面进入 insert 模式 “a”

在行尾进入 insert 模式 “A”

在当前光标位置进入 insert 模式 “i”

复制选中内容 “y” ,先用"v"选中内容,在复制

复制整行 “yy”

粘贴 “p”

set 设置 “:set 参数”

参数:(ic\is\hls\noic\nois\nohls)

  • ic ignorecase 忽略大小写
  • is incsearch 显示搜索短语的部分匹配项
  • hls hlsearch 高亮显示搜索结果

单次忽略大小写,即不 set ic"/搜索内容\c"

summary

  1. Type  o  to open a line BELOW the cursor and start Insert mode.
     Type  O  to open a line ABOVE the cursor.

  2. Type  a  to insert text AFTER the cursor.
     Type  A  to insert text after the end of the line.

  3. The  e  command moves to the end of a word.

  4. The  y  operator yanks(copies)text,p  puts(pastes)it.

  5. Typing a capital  R  enters Replace mode until  <ESC>  is pressed.

  6. Typing":set xxx"sets the option"xxx".  Some options are:
        'ic' 'ignorecase'ignore upper/lower case when searching
        'is' 'incsearch'show partial matches for a search phrase
        'hls' 'hlsearch'highlight all matching phrases
     You can either use the long or the short option name.

  7. Prepend"no"to switch an option off:   :set noic

lesson 7

获取帮助(打开 help.txt) “:help”

“:help 参数”

如: :help w

    :help c_CTRL-D

    :help insert-index

    :help user-manual

ctrl+w 切换光标所在的窗口(to jump from one window to another)

ctrl+d 可以查看所有命令(输入":字母"在 ctrl+d)

1700913394388-1d5204de-6f78-4292-abe9-f8b791ef0f4a.png

更新:2024-07-18 15:33:55
原文: https://www.yuque.com/c10wn/svr7uf/ecp6bmdhpsc8mu2f