The Basics
Vim is built on a pretty understandable system of movements and “incantations”. If you understand the basics, you can usually begin to work your text quite effectively. I find that it all seems to go together quite nicely once you grasp the basics.
If you’ve never used Vim, this will seem incredibly odd. But here’s the thing: Vim is not built around text entry – it’s built on Text Editing. The idea being that we spend way more time editing/tweakin than we do entering text. Dan Benjamin in PeepCode’s “Smash Into Vim” video (which I highly recommend) likened it to “text surgery” – and I couldn’t agree more.
Modes
The first concept to grasp is that Vim uses the idea of “modes” – meaning that the editor does different things for different commands based on the mode you’re in. When you open Vim you’re in “Normal Mode” – also known as “Command Mode”. This is where you get to the tell the editor to do things.
When you hit “I” or “A” – you’re in Insert Mode, which basically turns Vim into a regular text editor. You can enter text all you like and move around with the keys or a mouse.
Vim is focused on efficiency – so working the two modes together is really important. You should only be in Insert mode when you absolutely have to – like when creating a new document for instance. Otherwise there are better ways to do things – with a lot more efficiency.
When learning this stuff it’s very, very tempting to just pick up the mouse and CTRL-C/V your way around. I fight this every day. That’s what katas are all about – training your eyes/hands/mind to open up a new path.
I’ve just recently started tipping the scale in terms of how fast I am in Vim. I noticed this when I tried to code something in Visual Studio and it was monsterously slower to get around. Same with TextMate – I was searching for files, wrestling with mouse positioning, getting lost with all the tabs…
Anyway – if you’re going to try this, you have to get started with the basics. Here’s a fun kata that you can use to get yourself used to the “Mode Dance” – entering text and hitting escape. Entering text and hitting escape… (escape puts you in Normal mode).
Getting Started
I’m using MacVim for this, but you can go get gVim if you’re on windows – it all works the same. Put on some groovy music and slow your mind down. When you first start you’re going to hate it. No you’re going to LOATHE it. But as time goes by and you get faster you will absolutely BLOW away your productivity in other editors.
If you’re wondering “why the hell do I care about Vim anyway” – for two simple reasons:
- Vim is Forever
- Vim is Everywhere
Mastering it will help you in every situation.
So let’s get rolling! Kick open a terminal (shift-right-click with Windows) in a directory of your choosing and type in “mkdir Dojo” – this will create our Dojo.
Enter the Dojo
Arms at your side, hands on hips facing forward with full chest to the shomen. Bow from the waste to greet your sensei. Stand to meet the gaze and enter.
Seiza! Mokusoh! YAME!
Hajime!
Type “mvim Kata1″ if you’re on a Mac – if you’re on windows type “gVim Kata1″. Vim should open, and you should be staring at an empty page. There’s no need for a file extension – although if it makes you feel better go ahead.
Type each of these lines, and also do what they say:
I will write a line and hit escape and o to insert a new line and hit escape I will write a line and hit escape and o to insert a new line and hit escape I will write a line and hit escape and shift-O to insert a new line above and hit escape I will write a line and hit escape and shift-O to insert a new line above and hit escape I will navigate from beginning of line to end of line using 0 and $ I will go to the top of the document by hitting gg I will go to the bottom of the document using shift-G I will delete the first line by using 0shift-D I will paste it back using p I will delete the next line using 0dd I will paste it back using p I will repeat that using . I will undo using u I will undo again using u I will select a line using 0shift-V I will remove everything using %d I will undo using u I will go down a line using j I will go up a line using k I will go left 1 using h I will go right 1 using l I will go up four lines using 4k I will go down four lines using 4j I will select 2 lines below this using 0shift-V2j I will select 4 lines below this using 0shift-V4j I will select 3 lines above this using 0shift-V3k I will save using command-S (using MacVim, control-s using gVim) I will save using :w! I will navigate by word using w I will navigate by many words using 3w I will replace poprely spelled words using 3wcw I will undo those changes using u I will delete this line using shift-D and undo with u I will delete these words using 3wd2w, saying "move 3 words, delete 2 words" I will undo using u I will replace this with bacon using 3wcw "bacon" and undo with u I will save and quit with![]()
These steps encompass movement, inserting, changing, and pasting. You also touch on Visual Mode – which will be part of the next Kata.
