echo

作用: 类似 python 中的 print, 打印到 minibuffer

  • 打印变量
  • 打印字符串

echom

作用:类似 echo, 打印结果在 :messages 中保存一份

set

作用: 设置 options

options:

  • 代码行号

    • number : 代码行号
    • nonumber : 无行号
    • number! : toggle

      • 自动切换
    • number? : 询问,现在的设置值,即现在是什么值
  • wrap : 字符自动换行
  • numberwidth : 行号显示字符宽度
  • relativenumber
  • shiftwidth

    • 即 tabwidth
  • shiftround

    • 是否圆整缩进到 shiftwidth 的整数倍
    • 帮助: :h 'shiftround'

特殊用法:

  • ?

    • 查询现在的配置
  • !

    • 切换配置
  • no<option>

    • 反向设置, 取反

按键绑定

特殊按键:

  • <nop> : no operation 无操作, 用于禁用按键
  • <esc>
  • <cr> : enter
  • <c-i> : Ctr + i
  • <A-i> : Alt + i
  • <left>
  • <right>

普通绑定 map

语法: :map [key] [original key]

1
2
3
4
:map - x
:map - dd
:map <space> dd
:map <c-d> dd

modal 模式绑定,nmap

参考:Modal Mapping / Learn Vimscript the Hard Way

作用:特定 mode 下的绑定

  • nmap

    • normal mode
  • vmap

    • visual mode
  • imap

    • insert mode

删除绑定 nunmap

  • :nunmap [key-sequence] : normal unmap
  • :iunmap
  • :vunmap

避免重复绑定 nnoremap

特点: 按键只会绑定最初的意思, 避免了 map 和 nmap 等的循环和递归绑定

  • nnoremap : normal no recursive map
  • vnoremap
  • inoremap

用法:

1
2
3
4
:nnoremap \ x

" 允许绑定按键序列
:noremap -c ddO " 绑定 -c 到 dd 和 O

leader key

设置 leader:

1
:let mapleader = "-"

绑定 leader

1
:nnoremap <leader>d dd

localleader

1
:let maplocalleader = "\\"

buffer local 局域设置

mapping

1
2
:nnoremap Q dd                  " 全局设置
:nnoremap <buffer> Qdd          " buffer local 设置

帮助:

  • :h map-local

localleader

使用 localleader 绑定,对于 buffer local 的按键

settings options

1
:setlocal nowrap

作用域覆盖

本地 buffer local 绑定,会覆盖全局绑定

auto commands 自动触发命令

参考:Autocommands / Learn Vimscript the Hard Way 作用: 向事件关联相应的命令,当事件发生时,自动执行关联命令

1
:autocmd BufNewFile * :write

事件

:h {event}

与 FileType 连用, buffer local 绑定按键

1
2
:autocmd FileType javascript nnoremap <buffer> <localleader>c I//<esc>
:autocmd FileType python     nnoremap <buffer> <localleader>c I#<esc>

augroup

作用: 避免 source vimrc 造成 autocmd 重复加载

1
2
3
4
:augroup testgroup
:    autocmd!                   " 清空之前的 testgroup 加载
:    autocmd BufWrite * :echom "Cats"
:augroup END

onoremap

参考: Operator-Pending Mappings / Learn Vimscript the Hard Way 帮助: :H omap-info 用处: 在 v, d, y, c 等命令之后输入的按键,类似 d, y, c 等的输入参数

举例:

1
:onoremap il( :<c-u>normal! F)vi(<cr>

注意:

statusline 选项

参考: Status Lines / Learn Vimscript the Hard Way 作用: 即 emacs 中的 modeline 举例: :set statusline=%f\ -\ FileType:\ %y 特点:

  • 类似 C 语言的 printf 格式

格式:

  • %f : 文件名
  • %F : 文件名的全路径,full path
  • %y : FileType 值
  • 空格: 需要转义
  • %l : 当前行
  • %L : 总行数
  • 填充,对齐,宽度:

    • %4l : 显示宽度
    • %-4l : 右对齐
    • %04l : 通过 0 填充多余空格
    • %.20F : 只显示路径的最后 20 个字符
    • 总体格式: %-0{minwid}.{maxwid}{item}

变量

参考: Variables / Learn Vimscript the Hard Way

设置

  • let

    • let &textwidth = 3 : 设置全局变量
    • let &l:number = 1 : 设置本地变量 number
  • set
  • setlocal

查看变量的值

  • echo &myvariable
  • set option_name?

获取变量的值

  • &my_variable

以变量形式访问 register

  • let @a = 'hello!'
  • echo @a

buffer local 变量

  • :let b:hello = "world"
  • :echo b:hello