vim 配置&进阶用法

316 阅读4分钟

<ESC>键设置

You use the <ESC> key a lot when using Vim: consider remapping Caps Lock to Escape (macOS instructions).

截屏2024-10-18 21.26.23.png

esc键的确有点不方便,在vim的情况下,这个一般的建议是使用Caps Lock来代替esc功能,但是对于中文键盘来说,Caps Lock是(中/英)键,选了它作为esc功能后,就别想在vim中写中文了。所以我用了fn键来代替esc键

高阶命令

  • Words: w (next word), b (beginning of word), e (end of word)

  • Screen: H (top of screen), M (middle of screen), L (bottom of screen)

  • Scroll: Ctrl-u (up), Ctrl-d (down)

  • File: gg (beginning of file), G (end of file)

  • Line numbers: :{number}<CR> or {number}G (line {number}) (直接number+G会更方便?看个人习惯)

  • Misc: % (corresponding item) 光标位于某个开括号(如 (、{、[)时,按下 % 会跳转到该括号的对应闭括号(如 )、}、])。反之亦然,如果光标在闭括号上,按下 % 会跳转回开括号

  • Find: f{character}t{character}F{character}T{character}

    • find/to forward/backward {character} on the current line
    • , / ; for navigating matches
  • Search: /{regex}n / N for navigating matches

Selection

Visual modes:

  • Visual: v
  • Visual Line: V 每次按下移动键时,整行都会被添加到选择中。
  • Visual Block: Ctrl-v 允许用户以矩形块的形式选择文本,适用于选择特定列或矩形区域的文本,尤其是在处理表格或结构化数据时非常有用,也可以选定多行的特定列(例如在代码中选取特定变量)

Edits

  • d{motion} delete {motion}

    • e.g. dw is delete word, d$ is delete to end of line, d0 is delete to beginning of line
  • c{motion} change {motion}

    • e.g. cw is change word cw非常好用
    • like d{motion} followed by i
  • s substitute character (equal to cl)

  • Visual mode + manipulation

    • select text, d to delete it or c to change it
  • u to undo, <C-r> to redo

  • y to copy / “yank” (some other commands like d also copy)

  • p to paste

  •  ~ flips the case of a character

Customizing Vim

Vim is customized through a plain-text configuration file in ~/.vimrc (containing Vimscript commands). There are probably lots of basic settings that you want to turn on.

We are providing a well-documented basic config that you can use as a starting point. We recommend using this because it fixes some of Vim’s quirky default behavior. Download our config here and save it to ~/.vimrc.

(我直接用它作为我的~/.vimrc文件了)

" Comments in Vimscript start with a `"`.

" If you open this file in Vim, it'll be syntax highlighted for you.

" Vim is based on Vi. Setting `nocompatible` switches from the default
" Vi-compatibility mode and enables useful Vim functionality. This
" configuration option turns out not to be necessary for the file named
" '~/.vimrc', because Vim automatically enters nocompatible mode if that file
" is present. But we're including it here just in case this config file is
" loaded some other way (e.g. saved as `foo`, and then Vim started with
" `vim -u foo`).
set nocompatible

" Turn on syntax highlighting.
syntax on

" Disable the default Vim startup message.
set shortmess+=I

" Show line numbers.
set number

" This enables relative line numbering mode. With both number and
" relativenumber enabled, the current line shows the true line number, while
" all other lines (above and below) are numbered relative to the current line.
" This is useful because you can tell, at a glance, what count is needed to
" jump up or down to a particular line, by {count}k to go up or {count}j to go
" down.
set number

" Always show the status line at the bottom, even if you only have one window open.
set laststatus=2



" The backspace key has slightly unintuitive behavior by default. For example,
" by default, you can't backspace before the insertion point set with 'i'.
" This configuration makes backspace behave more reasonably, in that you can
" backspace over anything.
set backspace=indent,eol,start

" By default, Vim doesn't let you hide a buffer (i.e. have a buffer that isn't
" shown in any window) that has unsaved changes. This is to prevent you from "
" forgetting about unsaved changes and then quitting e.g. via `:qa!`. We find
" hidden buffers helpful enough to disable this protection. See `:help hidden`
" for more information on this.
set hidden

" This setting makes search case-insensitive when all characters in the string
" being searched are lowercase. However, the search becomes case-sensitive if
" it contains any capital letters. This makes searching more convenient.
set ignorecase
set smartcase

" Enable searching as you type, rather than waiting till you press enter.
set incsearch

" Unbind some useless/annoying default key bindings.
nmap Q <Nop> " 'Q' in normal mode enters Ex mode. You almost never want this.

" Disable audible bell because it's annoying.
set noerrorbells visualbell t_vb=

" Enable mouse support. You should avoid relying on this too much, but it can
" sometimes be convenient.
set mouse+=a

" Try to prevent bad habits like using the arrow keys for movement. This is
" not the only possible bad habit. For example, holding down the h/j/k/l keys
" for movement, rather than using more efficient movement commands, is also a
" bad habit. The former is enforceable through a .vimrc, while we don't know
" how to prevent the latter.
" Do this in normal mode...
" nnoremap <Left>  :echoe "Use h"<CR>
" nnoremap <Right> :echoe "Use l"<CR>
" nnoremap <Up>    :echoe "Use k"<CR>
" nnoremap <Down>  :echoe "Use j"<CR>
" ...and in insert mode
" inoremap <Left>  <ESC>:echoe "Use h"<CR>
" inoremap <Right> <ESC>:echoe "Use l"<CR>
" inoremap <Up>    <ESC>:echoe "Use k"<CR>
" inoremap <Down>  <ESC>:echoe "Use j"<CR>

" 使+y可以将选中的内容复制到系统剪切板
if has("clipboard")
  set clipboard=unnamed
endif

" 上移当前行
nnoremap <Space>k :m .-2<CR>==
" 下移当前行
nnoremap <Space>j :m .+1<CR>==

set hls

" 自定义命令,需要大写字母开头, jq是一个json格式化的工具,需自行下载
command! JJ %!jq .

Vim is heavily customizable, and it’s worth spending time exploring customization options. You can look at people’s dotfiles on GitHub for inspiration, for example, your instructors’ Vim configs (AnishJon (uses neovim), Jose). There are lots of good blog posts on this topic too. Try not to copy-and-paste people’s full configuration, but read it, understand it, and take what you need.

Extending Vim

There are tons of plugins for extending Vim. Contrary to outdated advice that you might find on the internet, you do not need to use a plugin manager for Vim (since Vim 8.0). Instead, you can use the built-in package management system. Simply create the directory ~/.vim/pack/vendor/start/, and put plugins in there (e.g. via git clone).

Here are some of our favorite plugins:

We’re trying to avoid giving an overwhelmingly long list of plugins here. You can check out the instructors’ dotfiles (AnishJonJose) to see what other plugins we use. Check out Vim Awesome for more awesome Vim plugins. There are also tons of blog posts on this topic: just search for “best Vim plugins”.

Others

some popular ones are Vimium for Google Chrome and Tridactyl for Firefox

浏览器插件