The Great Vim
I have been using vim for a long time as my favorite text editor but havent gone through its introductory tutorial
vimtutor
Check it out, good stuff there. However, it’s really a small subset from what Vim can do. There is also extensive help when you excute the command :help
from within Vim. Check out :h quickref
for a quick reference, :h index
for the complete list of commands. I recommend this three part series.
Notable features
Basics
Vim has differet modes: Normal, Insert, and Visual modes. Unless otherwise stated, the following commmands all run in normal mode.
Terminology
- A buffer is the in-memory text of a file.
- A window is a viewport on a buffer.
- A tab page is a collection of windows.
Navigation
This will help you browse large files
- Page up
CTRL-U
- Page down
CTRL-D
Splitting Windows
- To split horizontally
:split
- To split vertically
:vsplit
These two commands show two views of the same file when used without an argument. Just add the filename you want if you’d like to see it side by side
:vs filename
I used the abbreviated form of :vsplit
. To close windows :close
To navigate through split windows,
CTRL-W + navigation keys
To close all other windows CTRL-W + o
.
Tab page
You can open tabpages instead. As follows:
tabnew filename
See :h tabpage
for more info.
Visual mode
I will give an example of moving text around within a file.
- Press
v
to enter visual mode - Highlight the text using navigation keys
- Press
d
to delete it (it would would move it to a register) - Move the cursor to where you want to put it, and press
p
.
Numbering lists
Using commands, you can do the following:
command! -nargs=0 -range=% Number <line1>,<line2>s/^\s*\zs/\=(line('.') - <line1>+1).'. '
Put this in your .vimrc
.
Visual blocks
This is a nice feature to have to copy visual columns from a list, stored as a text file or printed to the screen. Use CTRL-V
.
Registers
:dis
to see what’s in the registers. You can use this as follows: the register ":
contains the value of the last entered command, so press ":p
to display the last entered command on the text file.
Moving lines
Place the cursor on a line then use
:m10
to move it to the 10th line. To display line numbers,
:set number
and use :set nonumber
to undo this.
Putting text
The following puts ‘hi’ on the first line.
:0put='hi'
The put
command puts things after the specified line number.
Navigation
In normal mode, point at a filename such as ~/.vimrc
, then press gf
. It will take you to that file (everything needs to be saved first).
Removing Control Characters
:%s/^V^M//g
Making a list of numbers
You can do this with
:put=range(1,5)
See more about this here. You can also use python’s range function
:r !python -c "print([k for k in range(10)])"
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
This is good for quick calculations. You can also use bc
if you have it:
Quick calculations
Similar to the above, you can do
:r !echo 12+12/13+2*8|bc -l
28.92307692307692307692
Or ruby (which doesn’t need to import functions like python),
:r !ruby -e 'puts(Math.exp(1))'
2.718281828459045
More Vim tricks
This one matches around a pattern
\zs.*\ze
jump lists, change lists,
ctrl e, ctrl y
browsing the change list
g;
g,
Delete in parenthesis
di(
Nice plugins
The following plugins are of interest for programmers
- NERDTree, to display the file tree
- Taglist, good for browsing through tags
- Ultisnip, for auto completion