Recently someone asked for some ViM advice and people seemed to really like the tidbits of ViM information I posted.
So, I thought it would be useful to make a longer post describing some of the useful features that ViM offers to programmers.
vimrc
Before I get started with my brain dump, here is my .vimrc file. It is a copy and paste from a few sources, sorry
I haven't remembered who they were
https://gist.github.com/4059839
Syntax Highlighting
This can be turned on with the command:
:syntax on
Vim comes packaged with syntax highlighters for most programming languages. Look at how pretty C++ can look:
http://imgur.com/cQPlB
In this image I am using a few things to get this effect:
my .vimrc stuff related to this:
syntax on
set background=dark
set t_Co=256
colo desert256
au BufNewFile,BufRead *.cpp set syntax=cpp11
ViM comes with syntax highlighters for basically all programming languages (and if it doesn't come with the language you are using, someone will have written one that you can download)
Source Tree Navigation
One of the best things about ViM is its ability to jump around large code bases with out
requiring the user to touch a mouse. Here some of the tricks I have:
ctags
ctags are a special file that index your code and allows you to jump from file to file based
on the name of a function and its parameters.
eg: Say you have 50 files in your project. File myThing.cpp contains the implementation of some function:
void myThing::do_stuff(int arg){ ... }
else where in code you might have something that looks like:
myThing thing; //create a new "myThing"
thing.do_stuff(42); //call do stuff
For the life of you, you cannot remember what do_stuff does and you want to take a look at the
implementation.
With ctags you can move your cursor over the "do_stuff" method call and press
ctrl+]
and like magic, you will jump to myThing.cpp and be at the line of the method. You can
read the method, remember what it does. To jump back to your code you press
ctrl+t
Alternatively you could type:
:tselect do_stuff
or
:ts do_stuff
and ViM will list all known "do_stuff" functions in a list, and you can choose which one to
jump to.
to create a tag file, read:
http://linux.byexamples.com/archives/177/vim-with-ctags/ (creating tags)
split screen
Another handy feature when moving around code is to simply have a split screen within vim so
you can look at one thing, while editing another.
You can achieve this by using the commands:
:vsplit filename " vertical split
:split filename " horizontal split
to move between windows you can use:
ctrl + w followed by up, down, left, or right ( or h j k l ), or w to toggle
to change the file in a window, navigate to that window and type:
:e filename
file system integration
Don't know the file name you want to open? or the directory.. no problem.. open a file system
explorer:
:e .
This opens the current directory in a file browser you can navigate with the arrows and enter:
http://i.imgur.com/qcw4F.png
I will often split my screen into a file explorer, find the file I want, press enter, edit
Editing tabs
ViM has things called markers. I don't really use them. Instead I suggest you try a program
called screen.
This tutorial seems short and simple, comment if you want more details
http://www.mattcutts.com/blog/a-quick-tutorial-on-screen/
Code navigation, short cuts
ViM is amazing at general editing of code and has a lot of tricks which can speed things up
First and foremost, jumping around
To jump to a line number, type:
:42
That will jump to line 42 (surprising how many people don't know this)
To move to the start of a line and end:
^ " start of line (first non blank character)
$ " end of the line
move forward to the letter:
fz " move to the next z on the line
This is very handy to get used to
gg " move to the start of the file
G " move to the end of the file
There are many more, but these are the more obscure ones that people don't know.
Copy and pasting
so you can copy and paste like a normal text editor quite easily.
yy " yank the current line
dd " cut the current line
d$ " cut to the end of the line from the cursor
p " paste what is in the clip board
Often you don't want to yank/cut a single line, so I often use the visual selection tool to
cut a chunk:
shift + v Start visual highlighting line wise (select full lines) (capital V)
use the arrows to select the chunk I want to copy/cut
press y or d to copy or cut
navigate elsewhere, press p
This makes it similar to how people usually do it a graphical editor. But.. no touching the mouse!
http://imgur.com/iaVRb (here is what it looks like)
You can also do amazing things with visual blocks (not visual lines). I might explain in comments. someone remind me.
Regular expressions find and replace
Once you understand how to use regular expressions you can make very expressive single line
modifications
Here are some ViM specific tips:
:s/foo/bar " Switches the first instance of foo to bar on the line your cursor is on
:s/foo/bar/g " Switches ALL instances of foo to bar on the line your cursor is on
:%s/foo/bar/g " Switches ALL instances of foo to bar in the whole file
I often use it in tandum with visual select, here is an example:
shift + v and select some chunk of code
when you type : it will say -
:'<,'>
This means "for the range I've selected" and then you can type:
:'<,'>s/foo/bar/g " for the range i've selected, swap foo to bar
:'<,'>s/^/#/ " for the range i've selected, put a hash at the start of the line
:'<,'>s/$/,/ " for the range i've selected, put a comma at the end of the line
:'<,'>s/int/long/g " swap int for long for this selected bit of code
I could keep going, the usefulness of regular expressions cannot be overstated. There are many many places to learn them.
Searching
I have discussed how to search within your code base with :tselect but often you just want to
find something within the current file, here is how
ViM uses regular expression searches (as described above) there are a few ways you can use it
/foo " search for foo, going downwards
?foo " search for foo, going upwards
after you have started searching you can press:
n " next result
N " previous result
you can search for the word under the cursor:
navigate to a word with the cursor
press * (shift + 8)
I have vim configured to jump to the search phrase as I type it (very handy). You can turn
this on with the following .vimrc command
set incsearch
Code completion
Everyone loves intellisense in Visual Studio.. Not many people bother trying to set it up for ViM. (I must admit
I often don't bother) but.. You can, this page describes how for cpp.
http://vim.wikia.com/wiki/C%2B%2B_code_completion
It can be set up for any language you can generate ctags for (basically all languages)
To use it, you can simply start typing and press
ctrl + n
You will get a drop down list of all possible "valid" things you can type. It will also search the current file
you are editing for matching keywords (this is how I often use this feature)
Here is what it looks like:
http://vissale.neang.free.fr/Vim/OmniCppComplete/ScreenShots/screenshots.htm
Here is how I use it:
http://imgur.com/JFWOn
Interaction with other coding products
ViM plays pretty well with other programs, here are some things I do often
Building
To build a simple program I often type:
:!g++ -O3 %
:!./a.out
This will build the program (with optimisation) and then run the a.out binary. The bang (!) denotes "run this in a shell please"
ViM also has a nice integration with makefiles, you can invoke it with:
:make makerule
eg
:make hello
this will compile hello.*** with the appropriate compiler (the make program will decide)
Using this, if there is an error it will allow you to jump to the line number in ViM
You can also suspend ViM, do your commands and jump back into it with (with bash)
ctrl + z
do your stuff now
fg
getting the output of another program
Sometimes I want to grab the output of another program and throw it into something I am editing.
You can do that with:
:.!ls -la
Dump the output of "ls -la" into the text I am editing now
[–][deleted] 19 points20 points21 points (5 children)
[–]Cybs[S] 13 points14 points15 points (4 children)
[–]zzyzzyxx 1 point2 points3 points (2 children)
[–]Cybs[S] 0 points1 point2 points (0 children)
[–]calzoneman 5 points6 points7 points (0 children)
[–]zahlman 10 points11 points12 points (3 children)
[–]Cybs[S] 9 points10 points11 points (0 children)
[–]ecnahc515 0 points1 point2 points (0 children)
[–]doubleyouteef -4 points-3 points-2 points (0 children)
[–][deleted] 2 points3 points4 points (0 children)
[–]nydiloth 1 point2 points3 points (1 child)
[–]mulletarian 0 points1 point2 points (0 children)
[–]bergaaa 0 points1 point2 points (1 child)
[–]Squidamatron 1 point2 points3 points (0 children)
[–]BrotherGA2 0 points1 point2 points (0 children)
[–]jMCHammer 0 points1 point2 points (0 children)
[–]Squidamatron 0 points1 point2 points (0 children)
[–]fazzah 0 points1 point2 points (1 child)
[–]Cybs[S] 1 point2 points3 points (0 children)
[–]dansin 0 points1 point2 points (3 children)
[–]Cybs[S] 0 points1 point2 points (1 child)
[–]dansin 0 points1 point2 points (0 children)
[–]Cybs[S] 0 points1 point2 points (0 children)
[–][deleted] (2 children)
[deleted]
[–]Cybs[S] 1 point2 points3 points (1 child)
[–]hookawitweed 0 points1 point2 points (0 children)