Vim: Difference between revisions
No edit summary |
No edit summary |
||
Line 7: | Line 7: | ||
= Commands = | = Commands = | ||
== Saving and Exiting vim == | == Saving and Exiting vim == | ||
Line 81: | Line 82: | ||
<code>y</code> Copies (yanks) the selected text. | <code>y</code> Copies (yanks) the selected text. | ||
<code>d</code> Deletes the selected text (also the same as | <code>yy</code> Copies the whole line | ||
<code>d</code> Deletes the selected text (also the same as the cut command in most other programs, as in you can paste the deleted text) | |||
<code>ciw</code> cuts the current word | <code>ciw</code> cuts the current word | ||
Line 92: | Line 95: | ||
<code>p</code> Paste after the cursor | <code>p</code> Paste after the cursor | ||
=== Substitution === | |||
<code>:s<code> substitutes | |||
'''Example:''' | |||
<code>:%s/something/something_else/g</code> This finds the word “something” and replaces it with “something_else” in the entire document. | |||
<code>:s/something/something_else/g</code> Does the same thing, but for only the current line | |||
<code>:%s/something/something_else/gc</code> Does the same thing as the first example, but asks for confirmation (that's what the “c” is there for.) | |||
Revision as of 00:59, 21 December 2016
Modes
i
goes into insert mode
Esc
goes back to command mode.
Commands
Saving and Exiting vim
ZZ
Save and exit vim
:w
Write the file to disk, stay in vim
:wq
Save the file and exit vim
:w !sudo tee %
Saves the file, even you don't have permission to write to the file (do this instead of closing the file, opening with sudo, and re-doing your changes)
:q</code Quit vim (Note: if you've made changes, you'll get a message that the file has changed. To quit without saving changes, use:
:q!
)
/string
search forward for string
?string
search backword for string
CTRL + f
scroll forward one screen
CTRL + b
scroll backword one screen
%
go to the "mate", if one exists, of the parenthesis, brace, or bracket.
zz
Scroll down the screen to make the current line appear in the middle.
Manipulating Text
vim + linenumber
Open a file at the specified line number (Run from the command line, not from inside vim)
:set number
show line numbers
:set nonumber
hide line numbers.
:syntax on
turns on syntax color highlighting for code and HTML
Selecting Text
V
Select a whole line (Press an up or down arrow to select multiple lines.)
v</code Select a range of text.
CTRL + v
select a rectangular block
Undo / Redo
:earlier 15m
reverts the document to how it was 15 minutes ago. You can substitute any valid time instead of 15m.
u
undo the latest change
U
Undo all changes in the last modified line.
CTRL + R
Redo last change (undo the last undo... kinda...)
Deleting
d
Deletes the selected text (also the same as "cut", as in you can paste the deleted text)
de
does the same thing as diw, however, you can delete every next word by pressing the period key ( . )
dd
delete the entire current line
diw
delete the current word
di"
delete the text between the quotes
Cut, Copy, and Paste
y
Copies (yanks) the selected text.
yy
Copies the whole line
d
Deletes the selected text (also the same as the cut command in most other programs, as in you can paste the deleted text)
ciw
cuts the current word
ci"
cuts the word inside the quotes
C
cuts to the end of the line
P
Paste before the cursor
p
Paste after the cursor
Substitution
:s substitutes
Example:
:%s/something/something_else/g
This finds the word “something” and replaces it with “something_else” in the entire document.
:s/something/something_else/g
Does the same thing, but for only the current line
:%s/something/something_else/gc
Does the same thing as the first example, but asks for confirmation (that's what the “c” is there for.)
References
- vim man page
- Run
vimtutor
at the command line for an interactive vim tutorial