VIM常用快捷命令(翻译)

Wesley13
• 阅读 650

自70年代起(哇,比我大很多哎),vi和vim就是程序员们最爱的文本编辑器。五年前我写了一篇文章《程序员应该知道的100条VIM命令》,现在升级版来了!

基本命令

:e filename

打开filename用于编辑

:w

保存文件

:q

退出vim

:q!

退出并且不保存修改

:x

保存更改并退出

:sav filename

另存为filename

.

重做常规模式下的上一个动作

重复五次.命令

在文件里移动

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 file

gg

move the cursor to the begining of the file

L

move the cursor to the bottom of the screen

: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

Cut, copy & paste

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

Replace

:%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

Case

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

Read/Write files

: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

File explorer

: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

Interact with 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

Alignment

:%!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

Window spliting

: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

Auto-completion

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

Abbreviations

:ab mail mail@provider.org

Define mail as abbreviation of mail@provider.org

Text indent

: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 highlighting

:syntax on

Turn on syntax highlighting

:syntax off

Turn off syntax highlighting

:set syntax=perl

Force syntax highlighting

点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Jacquelyn38 Jacquelyn38
3年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
Wesley13 Wesley13
3年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
3年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
3年前
Android蓝牙连接汽车OBD设备
//设备连接public class BluetoothConnect implements Runnable {    private static final UUID CONNECT_UUID  UUID.fromString("0000110100001000800000805F9B34FB");
Stella981 Stella981
3年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Wesley13 Wesley13
3年前
Vim进入和退出命令
本来不想写任何关于vim的文章的,无奈我今天又忘记怎么退出vim(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Fcaibaojian.com%2Ft%2Fvim)了,常用命令是ESC,然后:wq(保存并退出),:q!(不保存并强制退出),i进入vim模式。另外还有其它的,我可能都不会用到
Wesley13 Wesley13
3年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
11个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这