Code Editor of Choice: An (Incredibly) Extensive Defense of Vim for Developers
If you intend on writing any significant amount of code, you will need a text editor that grants you the greatest amount of productivity. Some people rely heavily on IDEs for this purpose, and while I personally do recognize the service they provide I try not to depend too greatly on them. This ultimately comes down to a problem of scope for me — whether it takes longer to Google for a solution or work off of memory, versus relying on autocompletion or IntelliSense to walk you through methods or properties.
Where IDEs are concerned, I’m traditionally a huge fan of what Microsoft has done. Their Visual Studio line has been peerless in my experience, and I’ve never felt this as strongly as when I had to leave it behind. Transitioning from C# into PHP required reevaluating my options, and while there is some facility for PHP in Visual Studio, I’m cheap and also didn’t want to go down the third-party-extension route.
Eclipse is designed for working with a wide variety of languages, and for PHP development the relevant ones were PHP, HTML, and JavaScript. It did fill this niche, but I found the IDE to be lethargic (it took considerable time to launch), monolithic, and prone to crashing for no explicable reason. In frustration, I turned to Netbeans and discovered a lithe environment, but I still wasn’t fully satisfied for reasons I’ve long since forgotten.
Vim to the rescue
So it was that I discovered Vim. It’s readily available (and frequently installed by default, though your mileage will vary on this) on every platform, fast to launch, handles a wide range of file types with ease, and once you learn how to use it effectively it becomes a huge time-saver. Of course, that’s if you learn to use it effectively, and its learning curve is massive.
The best way to master this one is to get your feet in it and try not to use anything else. At first you might find yourself crippled by a lack of knowledge, in which case Google is your friend.
There are some fundamentals that are essential to understand going in:
- The default state is “Normal” mode. In Normal mode, you can move around quickly, make bulk modifications, and execute commands.
- The “Insert” mode is what most people think of when considering text editors. In this mode, your keystrokes translate directly into characters on your screen. You also have some limited capacity for advanced features via macros that target Insert mode.
- “Visual”, “Visual Line”, and “Visual Block” mode are how you make selections. Entering Visual mode begins the process of selecting text, and is analogous to holding down SHIFT when moving around in standard text editors.
- Visual Block, rather than working at the character-level as normal selections do, works on a columnar basis. When you enter Visual Block, your selection begins at the character position you’re at in any given row (e.g. the 5th character from the left) — moving left or right increases the width of your selection, and up or down causes the selection to span lines. Thus, if you’re online, 3 character 5, begin a visual block, move right 5 and down 12, your selection forms a box whose top-left position is row 3 character 5 and bottom-right position at row 15 character 10.
- Visual Line, rather than working one character at a time, works a line at a time. All of your selections are whole sequential lines.
Following in the footsteps of Vi, Vim can be used without moving your hands away from their default home-row positions. That said, I use the arrow keys for navigation (something frowned upon by many purists) — with my right hand covering the arrow keys I can readily use the Delete (good for deleting characters without leaving Insert mode) and Insert (I use the Shift+Ins shortcut for working with the global clipboard).
Why Not Just Use Insert Mode?
As I indicated above, the default mode is Normal (as the name suggests). Why?
- Copy/Cut/Paste operations
- Jumping around more than one character at a time (e.g. paging, jumping to a particular line)
- Working with your buffers (“clipboards”)
- Executing commands
- Searching
What is a “command” in Vim? Examples include:
:sav
(save current document):e
(edit)- If you’re in a file in read-only mode, this switches out of it
- If you follow this with a filename, it opens it up for editing
:q
(quit):!{cmd}
(execute{cmd}
in your CLI shell (Windows command-line, Bash, etc.)):s/
(search and replace)
I preface all of those commands with :
because that’s the key that takes you into “command” mode. Other keys have various effects, which include:
a
(enter Insert mode immediately following the cursor’s position)i
(enter Insert mode at the cursor’s position)x
(cut the character at your cursor’s position into the default buffer)s
(cut the character at your cursor into the default buffer, and enter Insert mode)dd
(cut the whole line at the cursor’s position into the default buffer)yy
(copy the whole line at the cursor’s position into the default buffer)p
(paste whatever is currently in your default buffer at the cursor’s position)b
(jump to the beginning of the current or previous word)w
(jump to the beginning of the next word)e
(jump to the end of the current word)v
(enter Visual mode)gg
(go to beginning of document)G
(go to end of document)>>
(indent line right)<<
(unindent line)%
(jump between matching enclosing (parentheses, brackets, braces) characters)
These are all case-sensitive. Doing these in upper-case (by the way, don’t use CAPS LOCK unless you wish to cause yourself unnecessary problems) will alter the behavior as follows:
A
(enter Insert mode at the end of the current line)I
(enter Insert mode at the beginning of the current line)D
(cut all text at the cursor’s position to the end of the line)V
(enter Visual Line mode and simultaneously select the entire line at the cursor’s position)
… and more. Experiment a bit to see how things change.
Want out of Insert or Visual mode? Escape works, but if you stick to the paradigm of not leaving home-row then you’ll instead use CTRL+[ (left-bracket).
So, what can you do in the different Visual modes? Make a selection, and…
d
to delete the selected text. It moves into the default buffer.p
to paste over your selected text. Will paste from the default buffer, and whatever was previously selected is moved into the default buffer.>
or<
will indent or un-indent text respectively.o
alternates jumping between the beginning of your selection and the end.
With a few keys under our belt, we can start doing cool stuff with them:
- Transpose characters
- How often do you spell the as teh? Place your cursor on the 2nd character (e) and, in Normal mode, type
xp
. Thex
cuts the character under the cursor (e), moves the h left, and thenp
places the cut e immediately after the h.
- How often do you spell the as teh? Place your cursor on the 2nd character (e) and, in Normal mode, type
- Duplicate lines
- Do you like a line you just wrote? I recall in German class being forced to write the same sentence ten times, and crafting some ingenious ways to expedite the process (whether I learned anything in the process was debatable). Vim makes this very easy: Place your cursor on the line you wish to duplicate, type
yy
(copy entire line), and pressp
as many times as you need. Watch in amazement as your text is replicated.
- Do you like a line you just wrote? I recall in German class being forced to write the same sentence ten times, and crafting some ingenious ways to expedite the process (whether I learned anything in the process was debatable). Vim makes this very easy: Place your cursor on the line you wish to duplicate, type
- Select all text
- If you have GVim you may have already seen the Select All command. If not, or you aren’t sure what that’s all about, consider what it would mean to select all text (
ggVG
) in Vim:- Go to beginning (
gg
) - Enter Visual Line mode (
V
) to begin selecting lines - Jump to the bottom (
G
). All text will now be selected.
- Go to beginning (
- If you have GVim you may have already seen the Select All command. If not, or you aren’t sure what that’s all about, consider what it would mean to select all text (
- Wrap code in curly braces and indent
- As a C-style programmer (C, C++, C#, PHP, JavaScript), code in curly braces is frequent. Suppose you have a block of code and you wish to wrap it in curly braces with an if… statement above it.
- I would start by first writing the if… code, followed by the curly brace.
- Then, on the line following your soon-to-be-indented code, add the close brace.
- With your cursor on the close brace, type
vi%
— thev
enters Visual mode, and thei%
says “inside the enclosing characters”. - If done correctly, everything inside the curly braces is selected, and you can follow up with a
>
on your newly selected text.
- As a C-style programmer (C, C++, C#, PHP, JavaScript), code in curly braces is frequent. Suppose you have a block of code and you wish to wrap it in curly braces with an if… statement above it.
Repetition and Qualification
This is one of the things that helps Vim shine. Many of the normal-mode commands can be prefaced with a numeric value. Consider 10gg
(or 10G
) — what this tells Vim is that you wish to go to line 10. Want to delete three lines instead of one? Prepend 3
to the command dd
, giving you 3dd
, to delete three lines starting from the cursor’s position.
Recall the earlier example of duplicating lines? In that scenario, we repetitively typed p
to paste lines; we could instead have done this with a single command (2pp
) to paste two lines.
What if you just did something, such as paste (pp
), and you wanted to repeat that last action? You could always just keep typing pp
, or you could instead press .
(repeat last manipulation). This repeats the last action that manipulated text, such as a deletion, paste, or indentation
Some last thoughts…
This was not meant to be an all-inclusive primer in Vim, so much as to illustrate many of the things I find myself using. There are proper tutorials out there, and I’ve only even just scratched the surface of how I make good use of Vim. Let it be said that it’s difficult but well worth the investment.
Also, you can’t really look at Vim without hearing about how great Emacs is. It is the frequent source of vitriol in the developer/editor community, and the “Vim/Emacs war” has been waged for several years now. There is no clear winner, and you’re welcome to explore both, but note that they’re popular with developers for a reason: versatility. I’ve taken aside simply because I’ve had some encounters with Vim in the past and had a bit of familiarity going in.
This editor is ubiquitously available, extremely powerful, and has a plethora of file types that it can work well with. Furthermore, when other editors might choke on large files, Vim takes it all in stride. It can handle multiple documents simultaneously by using “buffers”, which can be exposed in any combination of tabs and panes even within a command-line interface. It has a powerful history feature that lets you walk back and forth through your changes.
Give it a try, you won’t be disappointed — that is, if you survive the initial encounter!