As promised here are the much wanted Vim tips. This is just a SNIPPET.
Full tutorial freely available on github (which will be updated regularly, this post not so much)
Wizardly Tips Vim
A fast way to benefit from vim’s power. Ideally you should read a good book and the :h
section. But if you’re in a hurry this should serve you well.
This is a list of somewhat unknown but highly useful Vim tips and tricks. I try and keeep the list succint by not showing well known or less useful commands.
HOW TO READ SHORTCUTS:
<Esc>
– Escape. Usually you should press the key, not type it. When you see ii<Esc>
you type ii and then Escape, not ‘ii` literally.
<C-g>
– means Ctrl+G
g<C-g>
– means press g then press Ctrl+g
<S-g>
– means press SHift+g
<C-Z>
– means Ctrl+Shift+Z (Shift is not shown but implied since we have an uppercase Z)
<zZ>
– z followed by Shift+Z
<C-m><CR>
– type Ctrl+M followed by Enter. <CR>
is Carriage Return (another word for Enter key)
Find files and send them to vim.
find ~ -size +10M | vim -
- find files and open the results in vim. While in vim you can press
gf
to open a file (from the results provided by find) in vim. - HINT: if your file has spaces vim will complain that the file is not in the path. This happens because it truncates the file when a space appears, thus trying to reach the file at an erroneous path. So instead use
<S-v>
to select the hole line and then pressgf
- HINT2 – you can use any other command that outputs to stdout and send it to vim.
- HINT3 – you can open the file under cursor in a new window with
<C-w>
Encrypt
- You decide to write your secret journal using vim. Trouble is – anyone can read your unencrypted text files. Or can they? By using
vim -x mysecretfile.txt
you use encryption on file. You’ll be asked for a key (password) to encrypt/decrypt your file. Warning – if you forget your key bye bye file. - HINT – you can also encrypt a file after you open it by typing
:X
- I’m still amazed by how many vim users totally ignore the power of register playback. That is – you record your actions into a register and play those actions back. It’s a bit weird to wrap your head around it but once you do – you’ll be amazed.
Here’s how it works - press
qa
to start recording your actions into registera
- Perform some actions that you want repeated. Here’s a very useful example for coders – custom line by line editing. Let’s say that you need to go line by line, search for the word “foo” and place it in tags () and place those tags again on the same line at the end of the line.
- This is a pretty complex scenario. Doing it by hand for thousands of lines it’s PITA (Pain In The A*s). What can you do? Write an awk script? A Python script? NOOOOO. You use vim register playback.
- So you start your action by implementing a movement that will go to the next line or the next pattern. This is very important because it will make the playback “smart”, as it will go on the next line or pattern and do the work.
- In your case you would start by typing
/foo
and pressing<CR>
(Enter). This takes care of always finding the next pattern and working on it. - No do your desired operations.
i<<Esc>ea>
which will insert the bracket, then to normal mode, go te end of the word, append the closing bracket. - next you copy the whole bracketed foo by pressing
ya>
(yank all in between brackets) followed by$p
(go to end of the line and paste) - Your work is done. You press
q
to stop recording. - time for PAYBACK …ah, I meant PLAYBACK.
- you press
@a
to playback the contents of registera
(into which you recorded your actions). You watch amazed how vim repeats “smartly” the operations you just performed previously. - Now you can press
@@
to repeat the previous playback as many times as you want. Or you could press10@@
to repeat the playback for 10 times. Or if you want this applied to your whole file type:% norm @a
– which means – apply the macro to all lines.
Open files in own tab/win
- Your girlfriend’s father comes to visit you. You want to impress him by opening a bunch of text files, each in it’s own window in vim (and show him that you’re very smart and tech savvy)
- You write
vim -o f1 f2 f3 f4
to open the specified files with windows split horizontally. usevim -O f1 f2 f3
to split them vertically. If you have lots of files and you dont want a hundred narrow windows usevim -o5 *.txt
to open a max of 5 windows (horizontally stacked). - You could instead open them in tabs by using the same syntax
vim -p f1 f2
orvim -p5 *.txt
Info
- The
<C-g>
magic – it displays file name and position.g<C-g>
will display number of words, num of lines and other useful stats.
Enter special chars
- Enter digraphs (speciar chars). Use
<C-k>
in Insert mode and type the digraph code. For example you can enter the pound sign using ‘Pd’ (£). Type:h digraph-table
for more info. To just view the codes type:dig
Quickly write buffer to disk
ZZ
– write the buffer to disk if modified and exit. I bet you didn’t knew that – but if you did BRAVO, you know how to quit Vim!
Sort
:%!sort -u
– sort all lines and remove duplicates.%
refers to the range and it means all the lines. You could use1,5
for example and it would only sort lines 1 to 5. The!
in this context means – execute an external shell command (in our case sort) with the range as stdin, when you’re done put back in place the stdout from that command.
Paste register
- While in insert mode press
<C-r>
followed by a registry, such asa
orb
or@
or:
or+
. eg:<C-r>a
. This will insert the contents of the registry at cursor.
Insert mode navig
- While in insert mode press
<C-h>
to backspace a char,<C-w>
to delete a word backwards,<C-u>
to delete until the beginning of the line (all without leaving Insert mode)
Autocomplete
- Did you knew you also have autocomplete by default? While in insert mode press
<C-n>
or<C-p>
to cycle through possible autocompletions.
How to quit Vim
- There are lots of famous memes about people not knowing how to quit Vim. Here’s a little secret – you can quit the current buffer(if you opened just a single file) without writting to disk by pressing
ZQ
.
Repeat Insert
- Everyone knows that you can insert a character repeatedly using a command such as
20i-<Esc>
– which will insert 20 ‘-‘. Or you could do something like10aHello World Of Vim.<Esc>
. This will do 10 appends of the phrase ‘Hello World Of Vim’