Professor Norm Matloff
Dept. of Computer Science
Some good editors (all free, all multi-platform):
We will use vim examples here.
Suppose we are currently at a call to Insert in Lnk.c (black cursor, 4th line of code):
We'd like to check whether we're using the function argument correctly. So, we want to move to the definition of the function.
First of all, we want to "mark our place," so we can easily move back to this line. We type
This tells vim to "remember" this spot under the name a.
Now, to move to the definition of Insert(), we could simply use vim's search capability, typing /Insert. But this may just bring us to other calls to Insert(). A better way is to type
The result is that the cursor does indeed move to the definition of Insert():
Now, to get back to our saved place, we type
We can save places (and other items) under the names a, b, c, etc., which are called registers.
The gD command works for variables too. On the same line as before, suppose the cursor is on the variable NumY:
If we now type gD, the cursor moves to the definition of NumY:
Extremely useful --use them often!
In our example of the usage of the gD command above,, say we want to simultaneously look at both the call to, and definition of, Insert(). We could go to the definition first:
Now, let's split this into two subwindows. Type
(or in gvim click Windows then Split), with this result:
You can move the cursor around independently in each of the subwindows.
So, to see both the definition of and call to Insert(), let's have the lower subwindow go to the call to Insert():
This way we can easily check whether we have specified a function's parameters properly, etc. There are many other settings in which having two subwindows will help clarify our work.
The ctrl-w ctrl-w operation toggles, i.e. hitting it a second time will return you to the first subwindow (or will make you cycle through subwindows, if you have more than 2).
If I want to go back to using just one window, we type :q in the subwindow to be deleted.
In vim, type u for undo, ctrl-r for re-do.
Often one wants to delete an entire set of lines or other region. Though one could do this character-by-character or line-by-line, it is faster and less distracting to apply a single delete operation to an entire region..
Moreover, by using a delete operation on an entire region, this sets up ways to copy or move the region.
Suppose for instance that we wish to move the function PrintResults() to a position just before the function Insert(). In vim we could first enter visual mode.
In gvim, one simply creates the block by dragging the mouse from the first to the last character of the region, upon which the region becomes highlighted.
In vim, we move the cursor to the first character of the region, then hit v, then move the cursor to the last character of the region. For our example here:
This will define a region, which will now be highlighted:
Now hit d to delete the region. Note that vim (or gvim) has not discarded this region, but instead has placed it in a buffer.
Move to the desired transfer point, just before Insert() (black cursor, 3rd line in the screen):
Now hit p ("paste"), and the buffer contents will be placed at the cursor, thus transfering those lines of code.
If we wanted to merely copy those lines of code, not move them, we could have deleted the block and then used p twice: First at the point of deletion, to restore the block, and then at the place at which we want to copy the block.
In gvim, the GUI version of vim, it is even easier to define the region, simply by dragging the mouse.
The v operation is oriented to sets of lines. By contrast, ctrl-v , which invokes visual-block mode, allows you to define rectangular blocks of characters.
Say for instance that we wish to de-indent the body of the function ProcessData() by 3 spaces. First, position the cursor at the beginning of the body:
Now hit ctrl-v, and then move the cursor 3 spaces to the right, and down 3 lines. This defines a highlighted region:
Now hit d to delete the region, and voila! -- the lines have been de-indented