About Vim

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

Vim的第一个版本由布萊姆·米勒在1991年发布。最初的简称是Vi IMitation,随着功能的不断增加,正式名称改成了Vi IMproved。现在是在开放源代码方式下发行的自由软件。

1. Content List

2. Vim Tips

2.1. Show Line Numbers

in Command Mode

:set number
##简写
:set nu
##关闭行号显示
:set nonumber/nonu

2.2. Search Topics

2.2.1. 搜索区分大小写

in Command Mode

:set ignorecase
:set ic
:set noignorecase/noic

  Search STRING forward :   / STRING.
  Search STRING backward:   ? STRING.

  Repeat search:   n
  Repeat search in opposite direction:  N  (SHIFT-n)

2.2.3. Replace

Same as with sed, Replace OLD with NEW:

 First occurrence on current line:      :s/OLD/NEW
 Globally (all) on current line:        :s/OLD/NEW/g 

 Between two lines #,#:                 :#,#s/OLD/NEW/g
 Every occurrence in file:              :%s/OLD/NEW/g 

2.2.3.1. Replace by a newline

https://stackoverflow.com/questions/71323/how-to-replace-a-character-by-a-newline-in-vim :s/OLD/\r/g

2.3. Run command in Vim

in Command Mode

:!pwd

Press ENTER or type command to continue
/srv/www/moin/wiki/datas/main/pages

2.4. Toggle auto-indenting for code paste

add in Vimrc

" press F2 to change autoindent setting
nnoremap <F2> :set invpaste paste?<CR>
set pastetoggle=<F2>
set showmode

3. Skills

3.1. 乱码问题

现象:

export LC_CTYPE=zh_CN.UTF-8
-bash: warning: setlocale: LC_ALL: cannot change locale (zh_CN.UTF-8)

参考: http://ubuntuforums.org/showthread.php?t=1720356

解决方法:

apt-get install --reinstall language-pack-zh-hans
locale-gen zh_CN
export LC_CTYPE=zh_CN.UTF-8

3.2. Vim Can't Save File (E212)

http://stackoverflow.com/questions/14948441/vim-cant-save-file-e212

##This will ask you for the root password
:w !sudo tee %
##Then type (L)oad at the prompt, to re-load the file after it is saved

3.3. 禁止黏贴时的自动缩进

:set paste

4. Reference


CategoryTool

MainWiki: Vim (last edited 2011-12-07 17:33:38 by twotwo)