You can scroll the document without the need to move the cursor to the top/bottom:
Ctrl-e
to scroll down and Ctrl-y
to scroll up.
Sometimes you want to open a file in read-only mode to prevent accidentally saving changes you've done on the fly. (When opening /etc/passwd, some log-files or whatever-config files for testing e.g.). Just type:
$ view /etc/passwd
The file will be opened in vim read-only and protects you from writing ;)
You might already know you can search for a string using /searchpattern
. If you want to
quick switch between multiple results, you can easily search (in the whole file:
:lvim "<input" %
followed by: :lopen
Will open a result buffer on the bottom. In this buffer, every line represents a search result containing
the filename, line number and the content of the whole line itself. You can navigate using hjkl or the cursor buttons
and hit enter to switch to the top buffer to the matching line. With Ctrl-w w
you can switch back to the search results.
If you are editing css or c code you can quickly select the whole block in visual mode:
Just type V%
. If the cursor is inside the {} block, type vaB
.
% will jump to the matching bracket (), {} or []. It will look in the current line and tries to find the matching one in the document.
You maybe already know you can scroll through VIMs history with u
ndo or Ctrl-r
edo. But instead
of stepping through the history tree you can do stuff like :earlier 10m
which recovers the state of the
document 10 minutes ago. From there you can go forward to the future and continue with :later 3m
for example.
Vim has a builtin (and comfortable) file explorer. Type :Texplore
(or shortened :Te
).
Now, a readonly document is generated. If you hit enter on a filename, the file will be opened. If you hit enter on
a directory, the content will be loaded. You can navigate using hjkl, use the search with / or go one
directory up by pressing -
I'm using vim for a couple of years now. To increase your effeciency I suggest you to learn movement using hjkl
!
To force myself, I remapped the arrow keys in the beginning like followed:
nnoremap <Up> ddkP
nnoremap <Down> ddp
nnoremap <Left> <<
nnoremap <Right> >>
This causes the left and right key to indent the current line, and the up down key to move the current line up or down. If you think that's horrible, you might be right. But it shouldn't take long and you don't even think about it to use the arrow keys to navigate.
When in Visual-Mode press o
.
If the cursor is on top, it will be toggled to bottom and back.
If you reopen a file you edited recently, you can simply press g;
to go to the last position where you
modified something.
Another great feature in vim is motions! You don't need to remember every shortcut, even better: There are mnemonics!
You might already know stuff like dd
or cc
. Both commands delete the entire line, but cc
will end up in insert mode. Maybe delete is not the correct word, it moves the line from the screen to register X (from which
you can paste with p
). I try to memorise it like d
elete, c
ut or change and p
aste.
Now back to topic: Motions are very great, imagine you want to change the content of a div-container:
<div class="container">This is a standard div, and we want to change or cut the text inside this tag</div>
|
cursor standing here ---
Pressing cit will cause this:
<div class="container"></div>
If you press cit
in normal-mode, it will do a cut/change (c) from the cursor position to the left until >
and to the right until <. You can remember it by saying: cut in tag ;) The same works for y
ank (copy) in tag or
d
elete in tag. But there are several other possibilities, you can also do viw
- which means:
visual in word, this might not be very useful but it can demonstrate you the power of vim-motions. I give you another example:
print "THIS PRINTED OUTPUT IS COMPLETELY UPPERCASE AND WE WANT TO TOGGLE CASE";
|
cursor standing here ---
Pressing vi"~ in normal-mode causes this:
print "this printed output is completely uppercase and we want to toggle case";
If you press vi"~
which means visual in ", the tilde will toggle the case.
What's also very useful, vat
(visual around tag). If editing html, you can highlight the whole div container e.g.
and pressing o
in visual mode let you jump to the beginning/end of the selected block.
Now show me an editor where you can do something like this without using a mouse!
Summary / Examples
ct< - Clear 'til < (Usefule for html/xml editing)
vis - Visual in Sentence
dib - Delete in Brackets ()
dib - Delete in Curly-Brackets {}
vat - Visual around (html/xml)-tag
You can increment the number under the cursor using Ctrl-a
and decrement using Ctrl-x
Another powerful feature for doing simple steps on a lot of data is record and replay macros. qw
starts
recording a macro in register w. You can use almost each letter (0-9, a-z and A-Z). Now every step you do will be
recorded in the macro you defined. To stop recording press q
and to replay press @q
, you can
define also a count, e.g. 10@q
repeats the macro ten times.
You can combine this cool feature with the tip above about Increment/Decrement to
generate numbers from 1 to 50. To do so, open a new file insert 1 in the first line and hit:
qwyyp Ctrl-a q48@w
What happens here?!
qw - start recording macro in register w
yy - yank (copy) whole line
p - paste line above current line
Ctrl-a - increment number under cursor
q - stops recording
48@w - replay macro saved in register w 48-times
Now you should have lines from 1 until 50. Let's assume you want to delete odd numbers:
ggqeddjq24@e
WTF is happening here?!
gg - goto first line
qe - start recording macro in register e
dd - delete line
j - move one line down
q - stop recording
24@e - replay macro saved in register e 24-times
To list all empty lines within vim: :g/^$
.
If you wish them to be deleted: :g/^$/d
.
Highlight the text you want to manipulate using V
then you can:
u
to convert text to lowercase
U
to convert text to uppercase
~
to switch case
~
will toggle the case of the current character. g~iw
will toggle the case of the current word.
As a mnemonic you can think about go switch case in word for example (see motions for more examples you can combine)
Assume you have a script file and you want to comment out a couple of lines. Use visual-block mode Ctrl-v
and move the cursor up/down to select the range. Press I
and insert # for example. After you pressed Esc
the # sign is inserted in every line you've selected.
In vim you can use advantage of the built-in encryption functionality! So you can protect
sensitive data very easy! Open the document and simply type :X
followed by your secret password.
When you now save, quit and reopen the file you will be asked for your passphrase! Wasn't that easy?!
IMPORTANT:
I recommend you to add set cryptmethod=blowfish2
to your ~/.vimrc to increasy security! If you're running vim less than
v7.4.399 you're unable to use blowfish2 but you can use blowfish instead.
If you're typing filenames, for include in php for example you can let vim complete the path!
In insert mode when typing press Ctrl-x Ctrl-f
to get a popup where you can scroll using
Ctrl-n
for next or Ctrl-p
for previous entry ;)
Delete from current line until end of file: VGd
.
V
Start visual line mode
G
Jump to end of file
d
delete selection
Sometimes, after editing a config file and restarting a service or compiling edited source code, there are errors. To reopen
the file at a specific line type $ vim nginx.conf +24
which will open nginx.conf at line 24.
Did you know? You can edit gzipped files in vim, on the fly!!! Just open them: $ vim passwordlist.gz