24.04.2021
Seit etwa den 70er Jahren ist vi und vim zu den beliebtersten Text Editoren. Die folgende Sammlung enthält eine Liste von mehr als Hundert teils sehr nützlichen vim Kommandos.
Allgemein
:e filename
Öffnet eine neue Datei. Sie können die Tab-Taste für die automatische Dateinamensvervollständigung genauso wie an der Shell-Eingabeaufforderung.nutzen.
:w
Speichern Sie die Änderungen in einer Datei. Wenn Sie keinen Dateinamen angeben, speichert Vim als Dateiname Sie. Für die Speicherung der Datei unter einem anderen Namen, geben Sie den Dateinamen ein.
:q
Wenn nicht gespeicherte Änderungen haben, weigert sich Vim vor der Beendigung.
:q!
Beenden ohne zu speichern
:x
Fast das gleiche wie :wq - die Datei speichern und verlassen, falls Sie Änderungen an der Datei vorgenommen haben. Wenn Sie keine Änderungen an der Datei vorgenommen haben, beendet sich Vim ohne die Datei zu schreiben.
:sav filename
Speichert die Datei als Dateinamen
.
Wiederholt die letzte Änderung die im Normal Mode gemacht wurde
Wiederholt 5 mal die letzte Änderung im Normal Mode
Shift+ZZ
Alternative Art und Weise um zu speichern und Vim zu beenden
:wq
Schreiben der Datei und Exit
Bewegung in Dateien
k or Up Arrow
move the cursor up one line
j or Down Arrow
move the cursor down one line
h or Left Arrow
Left one character.
l or Right Arrow
Right one character.
e
move the cursor to the end of the word
E
To the end of a whitespace-delimited word.
b
move the cursor to the begining of the word
B
To the beginning of a whitespace-delimited 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
Ctrl+F
Vi page down – Moves forward a page
Ctrl+B
Vi page up – Moves back a page
Ctrl+D
Moves forward half a page
Ctrl+U
Moves backward a half-page
^
To the first non-whitespace character of a line.
$
To the end of a line.
H
To the first line of the screen.
M
To the middle line of the screen.
L
To the the last line of the screen.
:n
Jump to line number n. For example, to jump to line 42, you’d type :42
Ausschneiden, Kopieren und Einfügen
y
Copy the selected text to clipboard
p
Paste clipboard contents p – Pastes the contents of the unnamed buffer to the left of the cursor
dd
Cut current line
yy
Copy current line
y$
Copy to end of line
3yy
Copies 3 lines of text to the unnamed buffer
yw
Copies a word (under the cursor) to the unnamed buffer
3yw
Copies 3 words to the unnamed buffer
P
Pastes the contents 0f the unnamed buffer to the right of the cursor
D
Cut to end of line
i
Insert before cursor.
I
Insert to the start of the current line.
a
Append after cursor.
A
Append to the end of the current line.
o
Open a new line below and insert.
O
Open a new line above and insert.
C
Change the rest of the current line.
r
Overwrite one character. After overwriting the single character, go back to command mode.
R
Enter insert mode but replace characters rather than inserting.
[ESC]
Exit insert/overwrite mode and go back to command mode. Exit visual mode and return to command mode.
x
Delete characters under the cursor.
X
Delete characters before the cursor.
Entering visual mode
v
Start highlighting characters. Use the normal movement keys and commands to select text for highlighting.
V
Start highlighting lines.
Editing blocks of text
Note: the Vim commands marked with (V) work in visual mode, when you’ve selected some text. The other commands work in the command mode, when you haven’t selected any text.
~
Change the case of characters. This works both in visual and command mode. In visual mode, change the case of highlighted characters. In command mode, change the case of the character uder cursor.
> (V)
Shift right (indent).
< (V)
Shift left (de-indent).
c (V)
Change the highlighted text.
y (V)
Yank the highlighted text. In Windows terms, “copy the selected text to clipboard.”
d (V)
Delete the highlighted text. In Windows terms, “cut the selected text to clipboard.”
yy or :y or Y
Yank the current line. You don’t need to highlight it first.
dd or :d
Delete the current line. Again, you don’t need to highlight it first.
p
Put the text you yanked or deleted. In Windows terms, “paste the contents of the clipboard”. Put characters after the cursor. Put lines below the current line.
P
Put characters before the cursor. Put lines above the current line.
Undo and redo
u
Undo the last action.
U
Undo all the latest changes that were made to the current line. U – Vi Undo, easy to remember, enter U in command mode to undo the last command.
Ctrl + r
Redo.
Suche
/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
/pattern
Search the file for pattern.
n
Scan for next search match in the same direction.
N
Scan for next search match but opposite direction.
Shift+N
Search Backward
Ersetzen
:rs/foo/bar/a
Substitute foo with bar. r determines the range and a determines the arguments.
The range (r) can be
nothing
Work on current line only.
number
Work on the line whose number you give.
%
The whole file.
Arguments (a) can be
g
Replace all occurrences in the line. Without this, Vim replaces only the first occurrences in each line.
i
Ignore case for the search pattern.
I
Don’t ignore case.
c
Confirm each substitution. You can type y to substitute this match, n to skip this match, a to substitute this and all the remaining matches (“Yes to all”), and q to quit substitution.
:%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/bob/BOB/
Replaces the first instance of bob with BOB
:s/bob/BOB/g
Replaces all instances of bob with BOB in that line (note g stands for global)
:%s/bob/BOB/g
Replaces all instances of bob with BOB in that file no matter how many exist or how many changes made to each 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
:%s/\(.*TEXT.*)/\/\/\1/g
Commenting all lines on matching pattern TEXT
:452s/foo/bar/
Replace the first occurrence of the word foo with bar on line number 452.
:s/foo/bar/g
Replace every occurrence of the word foo with bar on current line.
:%s/foo/bar/g
Replace every occurrence of the word foo with bar in the whole file.
:%s/foo/bar/gi
The same as above, but ignore the case of the pattern you want to substitute. This replaces foo, FOO, Foo, and so on.
:%s/foo/bar/gc
Confirm every substitution.
:%s/foo/bar/c
For each line on the file, replace the first occurrence of foo with bar and confirm every substitution.
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
Lesen/Schreiben von Dateien
: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
Datei 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
Interaktion mit Linux
:!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
:+X+!
In command mode this will undo everything you have done since the last disk write.
Ctrl+G
Shows the file name, total number of lines and the current position expressed as a percentage of the total number of lines in the file.
$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 [email protected]
Define mail as abbreviation of [email protected]
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
Sonstiges
:set nu
:set nu!
And to turn them off
:help index
all the essential vim commands (out of the box, anyways) (and all the unessential ones as well).
:help toc
:help holy-grail
Vi Search for Part of a Word
A fuzzy search allows you to find something that you only know part of, for example if you wanted to find all instances of lines starting with the word “Uber” you would use the following:
/^Uber
To find all instances of the word “ninja” at the end of a line you would use:
/ninja$
In some instances you’ll need to find what’s called a metacharacter. For example, say you wanted to find the instances in a file for the asterisk character (*), because it stands for many characters. You could use something like this:
/The \* character
Another example might be finding the text ninja, with the period being treated only as a period. Otherwise, you’d find ninjas, ninja?, ninja! and so on. To find JUST ninja you would use the following:
/ninja\.
Finally, matching a range of characters is handy, such as trying to find all instances of the version number string v2.9. You either have to perform several searches of use something like this:
/v2.[1-9]
The square brackets denote a single character, stretching from the first character to the one after the dash. If you wanted instead to find all versions of the word the, including THE, THe and tHE, you would use the following:
/ [tT] [hH [eE]
Options in Vi
set number set tabstop=5 set noh1search
The above code should be placed in the .exrc file which is located in the users home dir.
There are more than 60 options available in vi, to view them all type
:set all
To find out about an options status type
:set optionname? :set number
- turns on line numbers
:set nonumber
- turns the number option off
Advanced Vi commands
How to run external commands in vi:
Say for example you want to run “ls -l” inside of vi as you can’t remember a file name, you would enter:
:! ls -l
Pressing enter or command will return you to the vi session. If the output is more than one screen it is piped to the more command.
Joining lines in vi
Back space only works on current lines, so to join lines in vi you need to position the curser in either line and press Shift+J
Split windows in vi
When you are editing a file and want to see a different section of the file or even a different file altogether, you can use the following:
:split
- This splits the window horizontally
:vsplit
- this splits the file vertically, with the same file on each side
To switch between the windows, hit Ctrl+W
To edit two files in vi at the same time, open the first file and then type:
:split file2
To set the hight of the split window:
:15split /blah/file
The above will split the top 15 lines of the screen and display the contents of the /blah/file.
To close the split window, take focus by hitting Ctrl+W and then enter :close
- Or to close all the other split windows, take focus of the main window and enter:
only
This will close all other windows apart from your window :p