Полезные команды VIM

2013-07-01

небольшая подборка полезных команд прекрасного текстового редактора.

Basics

<td>
  Open <em>filename</em> for edition
</td>
<td>
  Save file
</td>
<td>
  Exit Vim
</td>
<td>
  Write file and quit
</td>
:e filename
:w
:q
:w!
<td>
  Search <em>word</em> from top to bottom
</td>
<td>
  Search <em>word</em> from bottom to top
</td>
<td>
  Search <em>john</em> or <em>joan</em>
</td>
<td>
  Search the, theatre or <em>then</em>
</td>
<td>
  Search <em>the</em> or <em>breathe</em>
</td>
<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>
<td>
  Search <em>fred</em> or <em>joe</em>
</td>
<td>
  Search exactly 4 digits
</td>
<td>
  Find 3 empty lines
</td>
<td>
  Search in all open files
</td>
/word
?word
/jo[ha]n
/\< the
/the\>
/\< the\>
/\< ¦.\>
/\/
/fred\|joe
/\<\d\d\d\d\>
/^\n\{3}
:bufdo /searchstr/

Replace

<td>
  Replace all occurences of <em>old</em> by <em>new</em> in file
</td>
<td>
  Replace all occurences with confirmation
</td>
<td>
  Replace all occurences between lines 2 and 35
</td>
<td>
  Replace all occurences from line 5 to EOF
</td>
<td>
  Replace the begining of each line by <em>hello</em>
</td>
<td>
  Replace the end of each line by <em>Harry</em>
</td>
<td>
  Replace <em>onward</em> by <em>forward</em>, case unsensitive
</td>
<td>
  Delete all white spaces
</td>
<td>
  Delete all lines containing <em>string</em>
</td>
<td>
  Delete all lines containing which didn’t contain <em>string</em>
</td>
<td>
  Replace the first occurence of <em>Bill</em> by <em>Steve</em> in current line
</td>
<td>
  Replace <em>Bill</em> by <em>Steve</em> in current line
</td>
<td>
  Replace <em>Bill</em> by <em>Steve</em> in all the file
</td>
<td>
  Delete DOS carriage returns (^M)
</td>
<td>
  Transform DOS carriage returns in returns
</td>
<td>
  Delete HTML tags but keeps text
</td>
<td>
  Delete lines which appears twice
</td>
<td>
  Increment number under the cursor
</td>
<td>
  Decrement number under cursor
</td>
<td>
  Change text to Rot13
</td>
:%s/old/new/g
:%s/old/new/gw
:2,35s/old/new/g
:5,$s/old/new/g
:%s/^/hello/g
:%s/$/Harry/g
:%s/onward/forward/gi
:%s/ *$//g
:g/string/d
:v/string/d
:s/Bill/Steve/
:s/Bill/Steve/g
:%s/Bill/Steve/g
:%s/\r//g
:%s/\r/\r/g
:%s#<[^>]\+>##g
:%s/^\(.*\)\n\1$/\1/
Ctrl+a
Ctrl+x
ggVGg?

Case

<td>
  Lowercase line
</td>
<td>
  Uppercase line
</td>
<td>
  Invert case
</td>
<td>
  Switch word to uppercase
</td>
<td>
  Modify word case
</td>
<td>
  Set all text to lowercase
</td>
<td>
  Ignore case in searches
</td>
<td>
  Ignore case in searches excepted if an uppercase letter is used
</td>
<td>
  Sets first letter of each word to uppercase
</td>
<td>
  Sets first letter of each word to lowercase
</td>
<td>
  Sets first letter of each line to uppercase
</td>
<td>
  Sets first letter of each line to lowercase
</td>
Vu
VU
g~~
vEU
vE~
ggguG
:set ignorecase
:set smartcase
:%s/\<./\u&/g
:%s/\<./\l&/g
:%s/.*/\u&
:%s/.*/\l&

Read/Write files

<td>
  Saves lines 1 to 10 in <em>outfile</em>
</td>
<td>
  Appends lines 1 to 10 to <em>outfile</em>
</td>
<td>
  Insert the content of <em>infile</em>
</td>
<td>
  Insert the content of <em>infile</em> under line 23
</td>
:1,10 w outfile
:1,10 w >> outfile
:r infile
:23r infile

File explorer

<td>
  Open integrated file explorer
</td>
<td>
  Split window and open integrated file explorer
</td>
<td>
  Graphical file explorer
</td>
<td>
  List buffers
</td>
<td>
  Move to parent directory
</td>
<td>
  List files
</td>
<td>
  Open file list
</td>
<td>
  Returns a list of .php files contening <em>expression</em>
</td>
<td>
  Open file name under cursor
</td>
:e .
:Sex
:browse e
:ls
:cd ..
:args
:args *.php
:grep expression *.php
gf

Interact with Unix

<td>
  Execute the <em>pwd</em> unix command, then returns to Vi
</td>
<td>
  Execute the <em>pwd</em> unix command and insert output in file
</td>
<td>
  Temporary returns to Unix
</td>
<td>
  Retourns to Vi
</td>
:!pwd
!!pwd
:sh
$exit

Alignment

<td>
  Align all lines
</td>
<td>
  Align all lines at the current position
</td>
<td>
  Align the next 5 lines
</td>
:%!fmt
!}fmt
5!!fmt

Tabs

<td>
  Creates a new tab
</td>
<td>
  Show next tab
</td>
<td>
  Show first tab
</td>
<td>
  Show last tab
</td>
<td>
  Rearrange tabs
</td>
<td>
  Execute a command in all tabs
</td>
<td>
  Puts all open files in tabs
</td>
:tabnew
gt
:tabfirst
:tablast
:tabm n(position)
:tabdo %s/foo/bar/g
:tab ball

Window spliting

<td>
  Edit <em>filename</em> in current window
</td>
<td>
  Split the window and open <em>filename</em>
</td>
<td>
  Puts cursor in top window
</td>
<td>
  Puts cursor in next window
</td>
<td>
  Maximise current window
</td>
<td>
  Gives the same size to all windows
</td>
<td>
  Add 10 lines to current window
</td>
<td>
  Split window vertically
</td>
<td>
  Same as <strong>:split</strong> in readonly mode
</td>
<td>
  Close current window
</td>
<td>
  Close all windows, excepted current
</td>
<td>
  Open #2 in this window
</td>
:e filename
:split filename
ctrl-w up arrow
ctrl-w ctrl-w
ctrl-w_
ctrl-w=
10 ctrl-w+
:vsplit file
:sview file
:hide
:­nly
:b 2

Top Vim commands you should know

Auto-completion

<td>
  Complete word
</td>
<td>
  Complete line
</td>
<td>
  Define <em>dict</em> as a dictionnary
</td>
<td>
  Complete with dictionnary
</td>
Ctrl+n Ctrl+p (in insert mode)
Ctrl+x Ctrl+l
:set dictionary=dict
Ctrl+x Ctrl+k

Marks

<td>
  Marks current position as <em>k</em>
</td>
<td>
  Moves cursor to mark <em>k</em>
</td>
<td>
  Delete all until mark <em>k</em>
</td>
mk
˜k
d™k

Abbreviations

<td>
  Define <em>mail</em> as abbreviation of <em>[email protected]</em>
</td>
:ab mail [email protected]

Text indent

<td>
  Turn on auto-indent
</td>
<td>
  Turn on intelligent auto-indent
</td>
<td>
  Defines 4 spaces as indent size
</td>
<td>
  Indent/un-indent in insert mode
</td>
<td>
  Indent
</td>
<td>
  Un-indent
</td>
:set autoindent
:set smartindent
:set shiftwidth=4
ctrl-t, ctrl-d
>>
<<

Syntax highlighting

<td>
  Turn on syntax highlighting
</td>
<td>
  Turn off syntax highlighting
</td>
<td>
  Force syntax highlighting
</td>
:syntax on
:syntax off
:set syntax=perl
*nixIT worldПолезности*bsdlinuxshellvim

Подборка курьёзных и достоверных, радиопереговоров

И опять про русских