Wiki LogoWiki - The Power of Many
Infravim

Vim 初级: 配置和使用

Vim是从vi发展出来的一个文本编辑器. 代码补全, 编译及错误跳转等方便编程的功能特别丰富, 在程序员中被广泛使用. 和Emacs并列成为类Unix系统用户最喜欢的编辑器.

文档参见: http://vimdoc.sourceforge.net/htmldoc/

中文文档: http://vimcdoc.sourceforge.net/doc/

基础使用

vim-key

查看帮助: :help, <Ctrl>] 进入标签, <Ctrl>O 返回.

初始化过程

正如多数 linux 软件, vim 的配置文件分为系统配置文件 /etc/vimrc, /usr/share/vim/ 和用户配置文件 ~/.vimrc, ~/.vim/.

vim 的配置文件载入过程为:

  1. /etc/vimrc
  2. $HOME/.vim/, $HOME/.vimrc
  3. $VIMRUNTIME/.vim, $VIMRUNTIME/.vimrc
  4. $HOME/.vim/after/

通过运行 vim -V 可查看整个初始化过程.

示例配置文件

一个示例配置文件如下:

" .vimrc
" See: http://vimdoc.sourceforge.net/htmldoc/options.html for details

" For multi-byte character support (CJK support, for example):
" set fileencodings=ucs-bom,utf-8,cp936,big5,euc-jp,euc-kr,gb18030,latin1
    
set tabstop=4    " Number of spaces that a <Tab> in the file counts for.
 
set shiftwidth=4  " Number of spaces to use for each step of (auto)indent.
 
set expandtab    " Use the appropriate number of spaces to insert a <Tab>.
          " Spaces are used in indents with the '>' and '<' commands
          " and when 'autoindent' is on. To insert a real tab when
          " 'expandtab' is on, use CTRL-V <Tab>.
 
set smarttab    " When on, a <Tab> in front of a line inserts blanks
          " according to 'shiftwidth'. 'tabstop' is used in other
          " places. A <BS> will delete a 'shiftwidth' worth of space
          " at the start of the line.
 
set showcmd     " Show (partial) command in status line.

set number     " Show line numbers.

set showmatch    " When a bracket is inserted, briefly jump to the matching
          " one. The jump is only done if the match can be seen on the
          " screen. The time to show the match can be set with
          " 'matchtime'.
 
set hlsearch    " When there is a previous search pattern, highlight all
          " its matches.
 
set incsearch    " While typing a search command, show immediately where the
          " so far typed pattern matches.
 
set ignorecase   " Ignore case in search patterns.
 
set smartcase    " Override the 'ignorecase' option if the search pattern
          " contains upper case characters.
 
set backspace=2   " Influences the working of <BS>, <Del>, CTRL-W
          " and CTRL-U in Insert mode. This is a list of items,
          " separated by commas. Each item allows a way to backspace
          " over something.
 
set autoindent   " Copy indent from current line when starting a new line
          " (typing <CR> in Insert mode or when using the "o" or "O"
          " command).
 
set textwidth=79  " Maximum width of text that is being inserted. A longer
          " line will be broken after white space to get this width.
 
set formatoptions=c,q,r,t " This is a sequence of letters which describes how
          " automatic formatting is to be done.
          "
          " letter  meaning when present in 'formatoptions'
          " ------  ---------------------------------------
          " c     Auto-wrap comments using textwidth, inserting
          "      the current comment leader automatically.
          " q     Allow formatting of comments with "gq".
          " r     Automatically insert the current comment leader
          "      after hitting <Enter> in Insert mode. 
          " t     Auto-wrap text using textwidth (does not apply
          "      to comments)
 
set ruler      " Show the line and column number of the cursor position,
          " separated by a comma.
 
set background=dark " When set to "dark", Vim will try to use colors that look
          " good on a dark background. When set to "light", Vim will
          " try to use colors that look good on a light background.
          " Any other value is illegal.
 
set mouse=a     " Enable the use of the mouse.
 
filetype plugin indent on
syntax on

On this page