Vastiny

Mar 12, 2024

常用 Vim 技巧

常常使用但容易忘的 Vim 使用技巧。

移动

  • 移到屏幕顶行后的 n 行

    1
    nH
  • 移到屏幕底行的 n 行

    1
    nL
  • 让当前行居中,z 让当前行居顶部

    1
    zz
  • 折叠

    1
    2
    zo 展开一层,zO 递归展开
    zc 折叠一层,zC 递归折叠
  • 快速选词

    1
    c3w, 而不是 dws, vws
    • 实现删除当前单词 daw 或者 diw,如果删除后要插入,就 ciw,或者 caw,不过为了均衡手指的点击,建议用 diw 和 ciw
    • 比如 w 是快速移动一个单词,W 是快速移动一个“字”,那什么才是字呢? 空格隔开的算啥? “‘;: 这些符号隔开的算啥?我的经验是,这些都不必去记,如果一个操作你需要思考的话,速度就已经慢下来了,还影响你的思路。你的脑子应该用在考虑代码逻辑上,而不是用在怎么把代码打出来。拿刚刚这个例子来说吧,就简单的理解成“W 是更快的 w ”就好啦!
  • goto

    • 指定位置

      1
      goto 21490
    • goto 指定位置(命令行)

1
2
3
4
vim +21490 script.py
vim -c "goto 1553" soft/lsof.md
vim -c ":goto 1553" soft/lsof.md
# grep -b -r "str" . 获取 byte-offset

+{command} 或者 -c {command}
{command} will be executed after the first file has been read. {command} is interpreted as an Ex command. If the {command} contains spaces it must be enclosed in double quotes (this depends on the shell that is used).

  • 行排序
    1
    2
    %!sort -t, -k3
    # 占位符为 ",",第三列的内容

常用快捷键

  • 使用 Ctrl + o 可以上一个文件
  • 使用 Ctrl+Shift+o 就向前
  • 手册中的 yank 其实就是 copy 的意思 ref
    • Yanking is just a Vim name for copying. The “c” letter was already used for the change operator, and “y” was still available. Calling this operator “yank” made it easier to remember to use the “y” key.
  • fx - jump to next occurrence of character x
  • tx - jump to before next occurrence of character x
  • Fx - jump to previous occurence of character x
  • Tx - jump to after previous occurence of character
  • ; - repeat previous f, t, F or T movement
  • , - repeat previous f, t, F or T movement, backwards 这两个都被其它快捷键覆盖了
  • vaw : aw - mark a word
  • Registers (信息在 ~/.viminfo)
    • :reg - show registers content
    • “xy - yank into register x
    • “xp - paste contents of register x
  • Marks
    • :marks - list of marks

修改

  • 删除当前行开始的 11 行

    1
    d11d  
  • 删除重复行的几种解决方法

    1
    2
    3
    4
    5
    :sort
    :g/^\(.*\)$\n\1$/d
    或者
    :sort
    :%s/^\(.*\)\(\n\1\)\+$/\1/
  • 替代 multiple cursors

选择 text object

1
/var<cr>

选中并修改

1
2
3
cgn
# 输入替代内容
<esc>

快速编辑函数

快速编辑函数

  • 删除一个单词并进入插入模式

    1
    cw
  • 删除一行并进入插入模式

    1
    cc
  • 修改函数的第一个参数

    1
    f(lct,
  • 删除最后一个参数

    1
    f)dF,
  • 删除花括号的内容,并进入写入模式。和 vi{s 类似,但可以少按一个按钮

    1
    ci{

    ya{ 复制括号所有的内容,所以以此类推 yi{ 是复制不包括花括号的内容,总结为 abc 公式

    1
    2
    3
    a 可选:c、d、y、v
    b 可选:a、i
    c 可选:(、[、{、w、l、s 或者其它

    或者:

    1
    2
    3
    a 可选:d
    b 可选:a、i、s(删除选中两遍的内容)
    c 可选:(、[、{

正则异同

其它

  • 在文件打开命令前面加上 noautocmd 或者 noau,可以用开阻止打开 zip 结构

    1
    vim -c 'noau e foo.xlsx'
  • 复制到系统粘贴板

    1
    2
    3
    4
    5
    6
    7
    noremap <Leader>y "*y
    noremap <Leader>p "*p
    noremap <Leader>Y "+y
    noremap <Leader>P "+p

    可以设置默认的
    set clipboard=unnamed
  • 经常把退出 vim 的 :q 敲成 q: 然后进入 vim 的 command-line-window

  • 编码:vim 重载编码

    1
    2
    3
    set fenc=cp936
    set enc=cp936 设置当前文档的编码,并且立即生效,但效果不是很彻底,要用 :e ++enc=cp936 , 具体可以查文档 :help ++enc
    set ff=unix mac dos,这三种,代表了换行的格式
  • 让文件保存不要自动添加 eol (Automatic Newline At End Of File)

    • 默认情况下 vim 会加 eol 的标记,0x0A,但像 VSCode 就没有加
    • 只有在 vim -b file.txt 的情况下,执行 :set noeol 才能去掉,nofixeol 无效

附录

OLDER >