небольшая подборка полезных команд прекрасного текстового редактора.
Basics
:e filename
|
<td>
Open <em>filename</em> for edition
</td>
:w
|
<td>
Save file
</td>
:q
|
<td>
Exit Vim
</td>
:w!
|
<td>
Write file and quit
</td>
Search
/word
|
<td>
Search <em>word</em> from top to bottom
</td>
?word
|
<td>
Search <em>word</em> from bottom to top
</td>
/jo[ha]n
|
<td>
Search <em>john</em> or <em>joan</em>
</td>
/\< the
|
<td>
Search the, theatre or <em>then</em>
</td>
/the\>
|
<td>
Search <em>the</em> or <em>breathe</em>
</td>
/\< the\>
|
<td>
Search <em>the</em>
</td>
/\< ¦.\>
|
<td>
Search all words of 4 letters
</td>
/\/
|
<td>
Search <em>fred</em> but not <em>alfred</em> or <em>frederick</em>
</td>
/fred\|joe
|
<td>
Search <em>fred</em> or <em>joe</em>
</td>
/\<\d\d\d\d\>
|
<td>
Search exactly 4 digits
</td>
/^\n\{3}
|
<td>
Find 3 empty lines
</td>
:bufdo /searchstr/
|
<td>
Search in all open files
</td>
Replace
:%s/old/new/g
|
<td>
Replace all occurences of <em>old</em> by <em>new</em> in file
</td>
:%s/old/new/gw
|
<td>
Replace all occurences with confirmation
</td>
:2,35s/old/new/g
|
<td>
Replace all occurences between lines 2 and 35
</td>
:5,$s/old/new/g
|
<td>
Replace all occurences from line 5 to EOF
</td>
:%s/^/hello/g
|
<td>
Replace the begining of each line by <em>hello</em>
</td>
:%s/$/Harry/g
|
<td>
Replace the end of each line by <em>Harry</em>
</td>
:%s/onward/forward/gi
|
<td>
Replace <em>onward</em> by <em>forward</em>, case unsensitive
</td>
:%s/ *$//g
|
<td>
Delete all white spaces
</td>
:g/string/d
|
<td>
Delete all lines containing <em>string</em>
</td>
:v/string/d
|
<td>
Delete all lines containing which didn’t contain <em>string</em>
</td>
:s/Bill/Steve/
|
<td>
Replace the first occurence of <em>Bill</em> by <em>Steve</em> in current line
</td>
:s/Bill/Steve/g
|
<td>
Replace <em>Bill</em> by <em>Steve</em> in current line
</td>
:%s/Bill/Steve/g
|
<td>
Replace <em>Bill</em> by <em>Steve</em> in all the file
</td>
:%s/\r//g
|
<td>
Delete DOS carriage returns (^M)
</td>
:%s/\r/\r/g
|
<td>
Transform DOS carriage returns in returns
</td>
:%s#<[^>]\+>##g
|
<td>
Delete HTML tags but keeps text
</td>
:%s/^\(.*\)\n\1$/\1/
|
<td>
Delete lines which appears twice
</td>
Ctrl+a
|
<td>
Increment number under the cursor
</td>
Ctrl+x
|
<td>
Decrement number under cursor
</td>
ggVGg?
|
<td>
Change text to Rot13
</td>
Case
Vu
|
<td>
Lowercase line
</td>
VU
|
<td>
Uppercase line
</td>
g~~
|
<td>
Invert case
</td>
vEU
|
<td>
Switch word to uppercase
</td>
vE~
|
<td>
Modify word case
</td>
ggguG
|
<td>
Set all text to lowercase
</td>
:set ignorecase
|
<td>
Ignore case in searches
</td>
:set smartcase
|
<td>
Ignore case in searches excepted if an uppercase letter is used
</td>
:%s/\<./\u&/g
|
<td>
Sets first letter of each word to uppercase
</td>
:%s/\<./\l&/g
|
<td>
Sets first letter of each word to lowercase
</td>
:%s/.*/\u&
|
<td>
Sets first letter of each line to uppercase
</td>
:%s/.*/\l&
|
<td>
Sets first letter of each line to lowercase
</td>
Read/Write files
:1,10 w outfile
|
<td>
Saves lines 1 to 10 in <em>outfile</em>
</td>
:1,10 w >> outfile
|
<td>
Appends lines 1 to 10 to <em>outfile</em>
</td>
:r infile
|
<td>
Insert the content of <em>infile</em>
</td>
:23r infile
|
<td>
Insert the content of <em>infile</em> under line 23
</td>
File explorer
:e .
|
<td>
Open integrated file explorer
</td>
:Sex
|
<td>
Split window and open integrated file explorer
</td>
:browse e
|
<td>
Graphical file explorer
</td>
:ls
|
<td>
List buffers
</td>
:cd ..
|
<td>
Move to parent directory
</td>
:args
|
<td>
List files
</td>
:args *.php
|
<td>
Open file list
</td>
:grep expression *.php
|
<td>
Returns a list of .php files contening <em>expression</em>
</td>
gf
|
<td>
Open file name under cursor
</td>
Interact with Unix
:!pwd
|
<td>
Execute the <em>pwd</em> unix command, then returns to Vi
</td>
!!pwd
|
<td>
Execute the <em>pwd</em> unix command and insert output in file
</td>
:sh
|
<td>
Temporary returns to Unix
</td>
$exit
|
<td>
Retourns to Vi
</td>
Alignment
:%!fmt
|
<td>
Align all lines
</td>
!}fmt
|
<td>
Align all lines at the current position
</td>
5!!fmt
|
<td>
Align the next 5 lines
</td>
Tabs
:tabnew
|
<td>
Creates a new tab
</td>
gt
|
<td>
Show next tab
</td>
:tabfirst
|
<td>
Show first tab
</td>
:tablast
|
<td>
Show last tab
</td>
:tabm n(position)
|
<td>
Rearrange tabs
</td>
:tabdo %s/foo/bar/g
|
<td>
Execute a command in all tabs
</td>
:tab ball
|
<td>
Puts all open files in tabs
</td>
Window spliting
:e filename
|
<td>
Edit <em>filename</em> in current window
</td>
:split filename
|
<td>
Split the window and open <em>filename</em>
</td>
ctrl-w up arrow
|
<td>
Puts cursor in top window
</td>
ctrl-w ctrl-w
|
<td>
Puts cursor in next window
</td>
ctrl-w_
|
<td>
Maximise current window
</td>
ctrl-w=
|
<td>
Gives the same size to all windows
</td>
10 ctrl-w+
|
<td>
Add 10 lines to current window
</td>
:vsplit file
|
<td>
Split window vertically
</td>
:sview file
|
<td>
Same as <strong>:split</strong> in readonly mode
</td>
:hide
|
<td>
Close current window
</td>
:nly
|
<td>
Close all windows, excepted current
</td>
:b 2
|
<td>
Open #2 in this window
</td>
Top Vim commands you should know
Auto-completion
Ctrl+n Ctrl+p (in insert mode)
|
<td>
Complete word
</td>
Ctrl+x Ctrl+l
|
<td>
Complete line
</td>
:set dictionary=dict
|
<td>
Define <em>dict</em> as a dictionnary
</td>
Ctrl+x Ctrl+k
|
<td>
Complete with dictionnary
</td>
Marks
mk
|
<td>
Marks current position as <em>k</em>
</td>
˜k
|
<td>
Moves cursor to mark <em>k</em>
</td>
d™k
|
<td>
Delete all until mark <em>k</em>
</td>
Abbreviations
Text indent
:set autoindent
|
<td>
Turn on auto-indent
</td>
:set smartindent
|
<td>
Turn on intelligent auto-indent
</td>
:set shiftwidth=4
|
<td>
Defines 4 spaces as indent size
</td>
ctrl-t, ctrl-d
|
<td>
Indent/un-indent in insert mode
</td>
>>
|
<td>
Indent
</td>
<<
|
<td>
Un-indent
</td>
Syntax highlighting
:syntax on
|
<td>
Turn on syntax highlighting
</td>
:syntax off
|
<td>
Turn off syntax highlighting
</td>
:set syntax=perl
|
<td>
Force syntax highlighting
</td>