记录一些 Vim 基础

2020/05/13

记录自己在使用 vim 中使用的命令和一些方法。

Basic

  1. 在 vi 的环境中,如何将目前正在编辑的文件另存新档名为 newfilename

    :w newfilename
    
  2. 到达指定行 2.1 In normal mode: line_numberG 例:到达第九行即9G 2.2 In command mode: :line_number 例:到达第九行即:9 : 认为第一种方法更为快速,因为如果是第二种方法的话,目光还需要向下移动。

  3. 到达指定行 ( vim <leader>键:\

  4. vim 替换 具体查看 vim 帮助文档:h substitute 全局替换::%s/search_string/substituted_string/g 此处:

    • %: 指范围,相当于1~$,文档的第一行到最后一行
    • g: replace all occurrences in the line. Without this argument, replacement occurs only for the first occurrence in each line.
  5. vim 精确查找 先看一个例子,准确查找字符串 int:

    /\<int\>
    

    其中

    • \<: 匹配单词开头
    • \>: 匹配单词结尾
  6. 拷贝整个文件

    :%y
    ggyG
    
  7. 获得当前文件的名称

    # 在屏幕上显示当前名称
    :echo expand("%:t")
    # 粘贴复制当前文件名称
    :let @f=expand("%:t")
    然后粘贴即可
    
  8. <esc>:= Ctrl - [

  9. 合并上下行shift + j shift + j可以删除行末尾的回车

  10. 查找到结果后 (1) 向下查询:n (2) 向上查询:shift + n(联想到网页的 tab 与 shift tab)

  11. Visual mode in vim (1) v: 选择字符 (1) shift + v: 选择整行 (1) ctrl + v: 选择整块

  12. gUaW:大写一个单词

  13. Paste local timestamp "=strftime("%c")<cr>p "=: expression register

  14. q: Opens a history of previous commands.

  15. vim 宏录制 目的:减少重复动作。 (1) In normal mode input qa(or qb, qc. ..a, b, c, d is your register) (2) And then you will see “starting . ..” singnal below. (3) Quit this function: in normal mode input q, you will end this process. 使用宏: In normal mode input @a, a is your register name. If you want it to run several times, add number in front of @a. For example, 7@a.

  16. 到达行首 In normal mode input 0.

  17. 替换实例 删除行首数字

    :%s/^[0-9]//g
    

    删除行尾数字

    :%s/[0-9]$//g
    
  18. 删除空行

    :g/^$/d
    

    如果空行开头有空格,这种方法就不好用了,使用

    :g/^\s*$/d
    
  19. 在使用 vim 中的y进行复制后,使用p进行粘贴,再次使用p就不能粘贴了,想要粘贴还得去复制一次,很繁琐。

    解决方法: 将使用`p`替换成`"0p`,这样就可以无限粘贴。
    注: 寄存器`0`的位置放置的就是最近复制的内容。
    
  20. vim read

    :read !command
    

    The way to append the output of an external command.

  21. ctrl + f / ctrl + b: 快速浏览

  22. di(: Delete all content inside ()

  23. delete a pairs by using tpope/vim-surround

    • 删除一对括号:ds( # delete surround
    • 删除一对双引号:ds"
    • 删除一对单引号:ds'
    • 添加一对括号:
      • 普通模式: ysiw(
      • 可视模式: S(

以下是tpope/vim-surround节选

It’s easiest to explain with examples. Press cs"' inside

 "Hello world!"

to change it to

 'Hello world!'

Now press cs'<q> to change it to

 <q>Hello world!</q>

To go full circle, press cst" to get

 "Hello world!"

To remove the delimiters entirely, press ds".

 Hello world!
  1. !$ !$是一个特殊的环境变量,它代表了上一个命令的最后一个字符串。如:你可能会这样

    mkdir mydir
    mv mydir yourdir
    cd yourdir
    

    可以改成

    mkdir mydir
    mv !$ yourdir
    cd !$
    
  2. 在 bash 里,使用 Ctrl-R 而不是上下光标键来查找历史命令。

  3. 使用 man ascii 来查看 ASCII 表。

  4. 命令行注释命令:alt + #

  5. vim 中查看缓冲区::ls 然后使用:buffer num进行切换

  6. vim 书签

  1. 在 vim 中访问 shell 30.1 进入 shell: :shell or :sh 30.2 离开 shell: exit

Plug 插件管理器删除插件

  1. 注释掉或删除在. vimrc中的添加的Plug命令;
  2. 运行:source ~/vimrc或重启 vim;
  3. 运行:PlugClean删除插件。

Vim example

  1. How to convert this piece of text(from vim in reddit)

    Arch linux
    Manjaro
    Ubuntu
    

    into

    1. Arch linux
    2. Manjaro
    3. Ubuntu
    

    Ways:

    (1) Make 3 lines of text out first one
    (2) use visual-block mode to insert `0.` for 3 lines
    (3) use visual-block again and `g<Ctrl-a>` to "sequentially" increase your `0`s
    
  2. 正向匹配删除。反向匹配删除 删除文本中包含 day 的行

    day1
    day2
    our
    us
    
    :%g/day/d
    

    删除文本中不包含 day 的行

    :%v/day/d
    

Vim fzf

使用 fzf 模糊查找文件,并使用 vim 编辑。

vim $(fzf)

一些内置的快捷访问手册

:help motion.txt

附:我的.vimrc