Difference between revisions of "Vim (正體中文)"

From ArchWiki
Jump to: navigation, search
Line 1: Line 1:
[[Category: 正體中文)]]
[[Category: 正體中文)]]
Vim ('''V'''i '''IM'''proved)是Vi衍生的文字編輯器。因為它陡峭的學習曲線及不親切的介面而臭名遠播,但由於它的效率,多元化的插件及客製化的選項,Vim是 *nix 用戶眼中其中一個最好的文字編輯器(與Emacs齊名)。gVim是Vim的圖形版本,它為用戶提供了一個選單。
Vim ('''V'''i '''IM'''proved)是Vi衍生的文字編輯器。因為它陡峭的學習曲線及不親切的介面而臭名遠播,但由於它的效率,多元化的插件及客製化的選項,Vim是 *nix 用戶眼中其中一個最好的文字編輯器(與Emacs齊名)。gVim是Vim的圖形版本,它為用戶提供了一個選單。

Revision as of 05:17, 18 May 2011

This template has only maintenance purposes. For linking to local translations please use interlanguage links, see Help:i18n#Interlanguage links.

Local languages: Català – Dansk – English – Español – Esperanto – Hrvatski – Indonesia – Italiano – Lietuviškai – Magyar – Nederlands – Norsk Bokmål – Polski – Português – Slovenský – Česky – Ελληνικά – Български – Русский – Српски – Українська – עברית – العربية – ไทย – 日本語 – 正體中文 – 简体中文 – 한국어

External languages (all articles in these languages should be moved to the external wiki): Deutsch – Français – Română – Suomi – Svenska – Tiếng Việt – Türkçe – فارسی

Vim (Vi IMproved)是Vi衍生的文字編輯器。因為它陡峭的學習曲線及不親切的介面而臭名遠播,但由於它的效率,多元化的插件及客製化的選項,Vim是 *nix 用戶眼中其中一個最好的文字編輯器(與Emacs齊名)。gVim是Vim的圖形版本,它為用戶提供了一個選單。

*Note that The Cult of Vi has determined that using emacs may cause dry mouth, blurred vision, drowsiness, dizziness, profuse sweating, tremors, sexual problems, headache, nausea and abdominal pain.


  • vim依賴vi,因此vi需要先安裝。但vi是基本安裝的其中一部份,所以大部份用戶應該已把它安裝好。
pacman -S vim


vim的設定檔是用戶的家目錄(~/)下的 .vimrc。一個 .vimrc的範例可以在 /etc/vimrc 中找到。

"Sample .vimrc
set nocompatible
set showmatch
set incsearch
set ignorecase  
set smartcase
set history=100
set backspace=eol,start,indent
set ruler
set tabstop=4
set shiftwidth=4
set expandtab
set virtualedit=all
set background=dark
set vb t_vg=
set mouse=v
set textwidth=79
set formatoptions=tcrq
  • 安裝 gVim (與vim相似但擁有gtk2圖形介面及根據 /etc/gvimrc 及 ~/.gvimrc 去設置)
pacman -S gvim
  • 這裏有一系列常用的插件,並可通過 pacman 安裝。
pacman -S vim-plugins

VIM 即時使用指南


  • 啟動vim編輯一個文件(新文件或已存在的文件)
vim filename
  • 啟動vim並開新文件



Vim 有很多模式,以下是最基本的:

  • Insert mode, in which anything you type (except some special keys) will appear on the screen and become part of your file buffer. There are many insert modes. The most basic form of insert mode is entered by pressing i.
  • Command mode (also called normal mode), in which your key strokes are interpreted as commands. Command mode is entered by pressing <ESC>
  • ex mode, where you may save a file, split the screen, open additional files, etc. ex mode commands are preceded by a colon, :
  • Visual mode, which allows you to expediently cut, copy and paste large areas of text with the keyboard or mouse. Visual mode is entered by pressing v

Open a new text file with vim:

vim mytext

After you start vim, you're in command mode.

  • Switch between modes

1. From command mode to insert mode, press i

Enter some text.

2. from insert mode to command mode, press <ESC>

You are now in command mode. Vim is waiting for your commands. Notice if you try typing, you get weird and unexpected results, because, well, you need to learn some commands, and Vim is not in insert mode.

3. Press <ESC> again to make sure you are truly in command mode and press : (colon)

Now you are in ex mode, which will allow us to save your first file. Type:


for write and quit.

Your file is written and vim will exit. You have just utilized 3 of the main modes of vim.

We'll focus on visual mode later.


In both the command mode and the insert mode, the arrow keys function to move the cursor, and with gvim you can mouse click to get to a new position. However, this is not the vim way. The most effective way of moving the cursor is to first enter the command mode by pressing ESC and then use vim's cursor-moving commands to move around. The 4 basic commands are

  • j move down one line
  • k move up one line
  • h move left one character
  • l move right one character

Remember: these commands work only in command mode. At first you may feel a bit uncomfortable. After you get familiar using these commands you will stick to them and forget the arrow keys.

Advanced movement:

  • 0 (zero) move to the first character of a line
  • $ move to the last character of a line
  • w move to the first character of the next word
  • e move to the last character of the next word
  • ( move to the beginning of the previous sentence
  • ) move to the beginning of the next sentence
  • { move to the beginning of the current paragraph
  • } move to the beginning of the next paragraph
  • PGUP or <CTRL>F move up one page
  • PGDOWN or <CTRL>B move down one page
  • H move cursor to the top left
  • L move to the bottom of the screen.
  • :25 go to line 25
  • gg move to beginning of file
  • G move to end of file


The DELETE key always works, and the BACKSPACE key works with newly typed text in insert mode. However, it is suggested you not use them. Instead, learn to use vim's deletion commands.

1. Make sure you are in command mode by pressing <ESC>

2. move the cursor to the character you want to delete

3. press x , this character disappears

x is just one of the many powerful deletion commands. Remember, try to use the cursor motion commands j k h l to locate your target, and don't leave the command mode.


While in command mode, move the cursor to the desired location.

  • i enter insert mode. This will insert before the current character.
  • a enter append mode. This will append text after the current character.
  • I move the cursor to the beginning of the current line and subsequently enter insert mode.
  • A move the cursor to the end of the current line and subsequently enter insert append mode.
  • o create a new blank line below the current line into which you can insert text.
  • O (capital O) To create a new line above the current line.
  • cc cut and replace the entire current line with a new line.
  • c$ cut and replace everything from the current cursor position to the end of the line.
  • c0 replace everything from the current cursor position to the beginning of the line.

To re-enter command mode, press <esc>


If you run the GUI version of vim, gvim, you can use mouse and the pull-down menus to do that---the same fashion with other editors. However, that is not the preferred style. You'll feel better off if you can live without a mouse.

1. Enter command mode by pressing ESC

2. Move the cursor to the line which you want to copy, by pressing j or k

3. press


to make a copy of the line, or


to cut it and make a copy

4. now move cursor (by pressing k or j) to the the location where you want to put this copy

5. press


to put the buffer after the current line, or


to put the buffer before the current line

If you want to copy or cut several lines, put a number before the yy or dd command, like


to copy 8 lines.

CUT, COPY and PASTE with Visual mode

  • Visual mode is like Command mode, but the movement commands extend a highlighted area. When a non-movement command is used, it is executed for the highlighted area. Cutting, copying and pasting large sections of text is more efficient in visual mode.

Hit v to enter visual mode from command mode, then use the cursor to go up or down. As you do, the lines of text will become highlighted. (You may also use the mouse to highlight areas of text)

To copy the highlighted section or 'yank':


To cut the highlighted section or 'delete':


To paste or 'put'


to place it before, or


for after.

Undo and Redo

Now that you have learned how to cut and delete, you may need to undo some mistakes


will undo the last function. Pressing it repeatedly will undo successive functions. Likewise, use


to redo.



1. 請確定你正在命令模式下,按下ESC建

2. 輸入



3. 在您找到第一個apple後,您可以鍵入



Suppose you want to search for the word under the current cursor position there's no need to type the word. Instead

1. Make sure you are in command mode by hitting ESC

2. type


this will highlight the word under the curser. Then just like before use


to jump to the next occurence of word you're searching for.

Hint: With default settings the search stops when you hit the end of the file. To make searches wrap around the end of a file add

 set wrapscan

to your ~/.vimrc.

How to substitute text

First make sure you're in command mode by pressing <ESC>.

  • to replace a single character, move the cursor over the character and hit r followed by its replacement.

The following commands utilize ex. Recall from above that ex commands are all preceded by a colon, :

  • replace first occurrence of old in current line with new
  • replace all occurrence of old in current line with new
  • replace the first occurrence of old in each line between line n1 and n2 with new
  • replace all occurrence of oldbetween line n1 and n2 with new
  • replace all occurrence of old in the whole buffer with new, prompt for confirmation.
  • replace all occurrence of old in the whole buffer with new, prompt for confirmation.


  • To save and exit: press <ESC> to enter command mode, then use ex:



or, from command mode, without ex:

  • to save your file as newname before exiting:

press <ESC> to enter command mode, then type

:wq newname
  • To exit without saving, press <ESC>, then type
  • Forced quit

If :q doesn't work, it's probably because you didn't save the change. If you want to save, use :wq. If you don't want to save the changes, type


Line Numbers and Jumping to Lines

  • Showing all line numbers - to show line numbers for a document, in insert mode, type :set number
  • Jumping to a line - If you want to jump to a particular line in a file, in insert mode, type :<line number>, for example, :43 to jump to line #43.

Comparing or Merging 2 or more files (vimdiff and vsplit)

Occasionally, you may need to compare or merge multiple files.

  • Hypothetical 1

While editing ~/.bashrc,

vim ~/.bashrc

you recall there are some global environment variables set within /etc/profile that you would like to merge into ~/.bashrc.

Simply enter ex mode and invoke vsplit:


which will split the screen, and then re-enter ex mode and load /etc/profile with the e command:

:e /etc/profile

to load /etc/profile into the newly split window.

Recall from above that


and then h and l will allow you to switch windows.

Subsequently invoking vsplit from ex mode will again split your sessions, so it is possible to compare 3, 4 or more files.

  • Hypothetical 2

After a pacman -Syu, pacman warns you that there are files to be merged, saved with the extension .pacnew

Invoke vimdiff from the shell:

$ vimdiff filename filename.pacnew

Recall from above that


and then h and l will allow you to switch windows.

Cursor location will indicate the window for which commands will affect.

Spell checking

Vim's built-in spell checking (available since version 7) can be enabled by entering

:set spell

Only English language dictionaries are installed by default, but you can find many more in the vim ftp archive. After downloading the spl files for the given language (e.g. hu.cp1250.spl, hu.iso-8859-2.spl and hu.utf-8.spl for Hungarian) into ~/.vim/spell, you can run

:set spell spelllang=LL

to activate spell checking for the selected language (i.e. ':set spell spelllang=hu' for the above mentioned example). For more information, see

:help spell

in vim or read the html documentation on vim's sourceforge page.

Vim Tutor

For more, run:


from a shell. Vim will open the tutor file.

External links