vim使用

vim-key

官方文档:http://vimdoc.sourceforge.net/htmldoc/ 中文文档:http://vimcdoc.sourceforge.net/doc/

vim配置

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可以查看整个初始化过程。

vim常用的一些配置选项:

" .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

vim光标移动

常用移动:

h,j,k,l:分别对应光标⬅️,⬇️,⬆️,➡️。

w,b,$,^,gg,G:分别对应下一单词,前一单词,行末,行首,文件头,文件末尾。

字符移动

在Vim的Normal模式里(如果你在Visual模式或者Insert模式,可以按<Esc>回到Normal模式), 通过h, j, k, l, i来进行左下上右的光标移动。

在Vim中多数操作都支持数字前缀,比如10j可以向下移动10行。

单词移动

多数情况下单词移动比字符移动更加高效。

w移动光标到下一个单词的词首;

b移动光标到上一个单词的词首;

e移动光标到下一个单词的结尾;

ge移动光标到上一个单词的结尾。

相对屏幕移动

c-f向下翻页,c-b向上翻页;c-e逐行下滚,c-y逐行上滚。这在几乎所有Unix软件中都是好使的,比如manlessH可以移动到屏幕的首行,L到屏幕尾行,M到屏幕中间。

zt可以置顶当前行,通常用来查看完整的下文,比如函数、类的定义。 zz将当前行移到屏幕中部,zb移到底部。

文件中移动

通过:10可以直接移动光标到文件第10行。如果你看不到行号,可以:set numbergg移到文件首行,G移到尾行。

小技巧:拷贝整个文件:ggyG

/xx可以查找某个单词xxn查找下一个,N查找上一个。 在光标跳转之后,可以通过c-o返回上一个光标位置,c-i跳到下一个光标位置。

?xx可以反向查找,q/, q?可以列出查找历史。

vim查找

在normal模式下按下/即可进入查找模式,输入要查找的字符串并按下回车。 Vim会跳转到第一个匹配。按下n查找下一个,按下N查找上一个。

Vim查找支持正则表达式,例如/vim$匹配行尾的"vim"。 需要查找特殊字符需要转义\,例如/vim\$匹配"vim$"

注意查找回车应当用\n,而替换为回车应当用\r(相当于<CR>)。

大小写敏感查找

在查找模式中加入\c表示大小写不敏感查找,\C表示大小写敏感查找。

# 将会查找所有的"foo","FOO","Foo"等字符串。
/foo\c

vim默认使用大小写敏感查找,为了查找方便我们常常配置为大小写不敏感:

编辑vim环境配置文件:

vim ~/.vimrc

设置内容:

# 设置默认进行大小写不敏感查找
set ignorecase
# 如果有一个大写字母,则切换到大小写敏感查找
set smartcase

查找单词

在normal模式下按下*即可查找光标所在单词(word), 要求每次出现的前后为空白字符或标点符号。例如当前为foo, 可以匹配foo bar中的foo,但不可匹配foobar中的foo。 这在查找函数名、变量名时非常有用。

按下g*即可查找光标所在单词的字符序列,每次出现前后字符无要求。 即foo barfoobar中的foo均可被匹配到。

查找替换

:s(substitute)命令用来查找和替换字符串。

该命令的语法为::{作用范围}s/{目标}/{替换}/{替换标志}

例如::%s/foo/bar/g会在全局范围(%)查找foo并替换为bar,所有出现都会被替换(g)。

作用范围

作用范围分为:当前行全文选区

  • 当前行::s/foo/bar/g
  • 全文::%s/foo/bar/g
  • 选区:

    • 从2~12行查找替换::2,12s/foo/bar/g
    • 当前行.与接下来2行+2:.,+2s/foo/bar/g

替换标志

上面命令中结尾的g即是替换标志之一,表示全局global替换(即替换目标的所有出现)。

还有很多其他有用的替换标志:

空替换标志

# 空替换标志表示只替换从光标位置开始,目标的第一次出现
:%s/foo/bar

大小写敏感标志

i表示大小写不敏感查找,I表示大小写敏感

:%s/foo/bar/i

# 等效于模式中的\c(不敏感)或\C(敏感)
:%s/foo\c/bar

确认标志

c表示需要确认,例如全局查找"foo"替换为"bar"并且需要确认

:%s/foo/bar/gc

按下y表示替换,n表示不替换,a表示替换所有,q表示退出查找模式, l表示替换当前位置并退出。^E^Y是光标移动快捷键。

标签: vim

仅有一条评论

  1. 好厉害--看看------~~

添加新评论