Day two with Vim was all about discovery. I went against my own advice and used Vim at work. Not 100% Vim, but a Vim wrapper for Eclipse. I figured I knew enough to navigate a file so I should be fine.
Well, navigation is only a small part of the functionality that I actually needed, and didn't think about that it until I installed that plugin. I quickly realized that coding, at least my coding, requires a lot of text selecting, word deletions, and line deletions.
In addition to those requirements, I also lacked a way to navigate a file quickly. Moving line-by-line, and character-by-character is extremely slow.
I needed to learn the shortcuts to quicker navigation so I pointed my browser to the Vim cheatsheet that I posted in my introduction article and scanned the graphic for ways to get around the text editor fast.
The first thing I wanted to do was to move down the page fast. There are several ways to do this. The first way is to use the shortcut combo
#jmeans - move a specific number of lines down. Example:
12jwill move the cursor twelve lines towards the bottom of the file.
This was pretty interesting and just so happens to work with a whole lot of other key combos. You can use
#l to move the cursor to the left a certain amount of characters, or
#x to delete a specific number of characters as well. The
# will even work for deleting whole lines with
dd (one of the new commands I learned today).
#j is still a slow way to move unless you know exactly where you want to be. After scanning the cheatsheet again I found the
} shortcuts. These two commands will move to the next paragraph above and below the cursor respectively. And yes, the
# prefix will work here as well.
I then found, by forgetting to hold the
shift key that the
] keys will take you to the beginning and end of a file immediately. That turned out to be extremely useful when I needed to add a new method to my classes.
So those keys would take me up and down, but what about left and right? There are a whole slew of cool commands here. I'm going to list theses here so you can try them out, rather than just reading a paragraph on each one.
- 'H' and 'L' - will move the cursor to the top and bottom of the screen respectively
E- move the cursor to the end of the word
B- move the cursor to the beginning of the word
W- move to the beginning of the next word
t<c>- moves the cursor to the left of the parameter character. Example:
tRwould move the cursor behind of the next capital
R. Note: I found this to be unreliable. In a sentence that had multiple i's and the word is, the cursor refused to move to the space before the i.
T<c>- moves the cursor behind a previous parameter character.
0- moves the cursor to the beginning of the line
$- moves the cursor to the end of the line
Most of the commands above will work with other commands, like
d. For example, if I wanted to delete everything to the end of a line I could type
d$ and it would work. I could have also used
D for that, but it's nice to have more than one way to skin a cat!
Copying and Pasting is one of those developer sins that should be avoided if possible, but sometimes it just can't be avoided. In those situations it's nice to know what keys will come to my rescue so I don't have to interrupt the keyboard zen that I just discovered.
y will copy anything that I've highlighted, while
d will cut it. Wait,
d...? Isn't, that delete? Yeah, it sure is. For some reason this works the same way cut does in a normal text editor.
Oh, and for a side note, it might be nice to know how to select lines for copying, or deletion. In order to select lines I had to enter
Visual mode with the
v key. Once in Visual mode I navigate just like I would in Normal mode, except in visual mode I will see the lines become highlighted starting from the line where I entered Visual mode.
After I've selected and copied (or cut) the words I needed I just navigate to where I want them and type either
P for past after or paste before the cursor respectively.
I learned an awful lot today mainly due to necessity after I installed the Vim plugin at work. There are a ton of commands for navigation alone, and without knowing them it can be a pain getting around a large text file. One of the most surprising things I learned though, was that you can combine commands to make one super command like deleting three words or sentences at a time. The developers that came up with Vi and Vim really put a lot of thought in to their design.