超过 130 个你需要了解的 vim 命令-凯发app官方网站

凯发app官方网站-凯发k8官网下载客户端中心 | | 凯发app官方网站-凯发k8官网下载客户端中心
  • 博客访问: 6397869
  • 博文数量: 579
  • 博客积分: 1548
  • 博客等级: 上尉
  • 技术积分: 16635
  • 用 户 组: 普通用户
  • 注册时间: 2012-12-12 15:29
个人简介

http://www.csdn.net/ http://www.arm.com/zh/ https://www.kernel.org/ http://www.linuxpk.com/ http://www.51develop.net/ http://linux.chinaitlab.com/ http://www.embeddedlinux.org.cn http://bbs.pediy.com/

文章分类

全部博文(579)

文章存档

2018年(18)

2015年(91)

2014年(159)

2013年(231)

2012年(80)

相关博文
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·

分类: linux

2015-10-21 13:55:55

从 1970 年开始,vi 和 vim 就成为了程序员最喜爱的文本编辑器之一。5年前,我写了一个问自己名为 “每个程序员都应该知道的 100 个 vim 命令” 这次算是之前那篇文章的改进版,希望你会喜欢。

基础

:e filename open filename for edition
:w save file
:q exit vim
:q! quit without saving
:x write file (if changes has been made) and exit
:sav filename saves file as filename
. repeats the last change made in normal mode
5. repeats 5 times the last change made in normal mode

在文件中移动

k or up arrow move the cursor up one line
j or down arrow move the cursor down one line
e move the cursor to the end of the word
b move the cursor to the begining of the word
0 move the cursor to the begining of the line
g move the cursor to the end of the line
gg move the cursor to the begining of the file
l move the cursor to the end of the file
:59 move cursor to line 59. replace 59 by the desired line number.
20| move cursor to column 20.
% move cursor to matching parenthesis
[[ jump to function start
[{ jump to block start

剪切、复制和粘贴

y copy the selected text to clipboard
p paste clipboard contents
dd cut current line
yy copy current line
y$ copy to end of line
d cut to end of line

搜索

/word search word from top to bottom
?word search word from bottom to top
* search the word under cursor
/\cstring search string or string, case insensitive
/jo[ha]n search john or joan
/\< the search the, theatre or then
/the\> search the or breathe
/\< the\> search the
/\< ?.\> search all words of 4 letters
/\/ search fred but not alfred or frederick
/fred\|joe search fred or joe
/\<\d\d\d\d\> search exactly 4 digits
/^\n\{3} find 3 empty lines
:bufdo /searchstr/ search in all open files
bufdo %s/something/somethingelse/g search something in all the open buffers and replace it with somethingelse

替换

:%s/old/new/g replace all occurences of old by new in file
:%s/onward/forward/gi replace onward by forward, case unsensitive
:%s/old/new/gc replace all occurences with confirmation
:2,35s/old/new/g replace all occurences between lines 2 and 35
:5,$s/old/new/g replace all occurences from line 5 to eof
:%s/^/hello/g replace the begining of each line by hello
:%s/$/harry/g replace the end of each line by harry
:%s/onward/forward/gi replace onward by forward, case unsensitive
:%s/ *$//g delete all white spaces
:g/string/d delete all lines containing string
:v/string/d delete all lines containing which didn’t contain string
:s/bill/steve/ replace the first occurence of bill by steve in current line
:s/bill/steve/g replace bill by steve in current line
:%s/bill/steve/g replace bill by steve in all the file
:%s/^m//g delete dos carriage returns (^m)
:%s/\r/\r/g transform dos carriage returns in returns
:%s#<[^>]\ >##g delete html tags but keeps text
:%s/^\(.*\)\n\1$/\1/ delete lines which appears twice
ctrl a increment number under the cursor
ctrl x decrement number under cursor
ggvgg? change text to rot13

大小写

vu lowercase line
vu uppercase line
g~~ invert case
veu switch word to uppercase
ve~ modify word case
gggug set all text to lowercase
gggug set all text to uppercase
:set ignorecase ignore case in searches
:set smartcase ignore case in searches excepted if an uppercase letter is used
:%s/\<./\u&/g sets first letter of each word to uppercase
:%s/\<./\l&/g sets first letter of each word to lowercase
:%s/.*/\u& sets first letter of each line to uppercase
:%s/.*/\l& sets first letter of each line to lowercase

读写文件

:1,10 w outfile saves lines 1 to 10 in outfile
:1,10 w >> outfile appends lines 1 to 10 to outfile
:r infile insert the content of infile
:23r infile insert the content of infile under line 23

文件浏览器

:e . open integrated file explorer
:sex split window and open integrated file explorer
:sex! same as :sex but split window vertically
:browse e graphical file explorer
:ls list buffers
:cd .. move to parent directory
:args list files
:args *.php open file list
:grep expression *.php returns a list of .php files contening expression
gf open file name under cursor

和 unix 系统交互

:!pwd execute the pwd unix command, then returns to vi
!!pwd execute the pwd unix command and insert output in file
:sh temporary returns to unix
$exit retourns to vi

对齐

:%!fmt align all lines
!}fmt align all lines at the current position
5!!fmt align the next 5 lines

tabs/windows

:tabnew creates a new tab
gt show next tab
:tabfirst show first tab
:tablast show last tab
:tabm n(position) rearrange tabs
:tabdo %s/foo/bar/g execute a command in all tabs
:tab ball puts all open files in tabs
:new abc.txt edit abc.txt in new window

分屏显示

:e filename edit filename in current window
:split filename split the window and open filename
ctrl-w up arrow puts cursor in top window
ctrl-w ctrl-w puts cursor in next window
ctrl-w_ maximize current window vertically
ctrl-w| maximize current window horizontally
ctrl-w= gives the same size to all windows
10 ctrl-w add 10 lines to current window
:vsplit file split window vertically
:sview file same as :split in readonly mode
:hide close current window
:­nly close all windows, excepted current
:b 2 open #2 in this window

自动完成

ctrl n ctrl p (in insert mode) complete word
ctrl x ctrl l complete line
:set dictionary=dict define dict as a dictionnary
ctrl x ctrl k complete with dictionnary

marks

m {a-z} marks current position as {a-z}
' {a-z} move to position {a-z}
'' move to previous position

缩写

:ab mail mail@provider.org define mail as abbreviation of mail@provider.org

文本缩进

:set autoindent turn on auto-indent
:set smartindent turn on intelligent auto-indent
:set shiftwidth=4 defines 4 spaces as indent size
ctrl-t, ctrl-d indent/un-indent in insert mode
>> indent
<< un-indent
=% indent the code between parenthesis
1gvg= indent the whole file

语法高亮

:syntax on turn on syntax highlighting
:syntax off turn off syntax highlighting
:set syntax=perl

force syntax highlighting

阅读(4332) | 评论(0) | 转发(2) |
给主人留下些什么吧!~~
")); function link(t){ var href= $(t).attr('href'); href ="?url=" encodeuricomponent(location.href); $(t).attr('href',href); //setcookie("returnouturl", location.href, 60, "/"); }
网站地图