Vim and vigour...notes from our dev learning lunch

Engineering

Read in 4 minutes

Vim and vigour...notes from our dev learning lunch

Vim: what and why?

“You’re not a real developer unless you use Vim”. A bit harsh, but is it true?

Well, it’s certainly a useful thing to know. A text editor created originally for the Unix operating system, Vi (short for visual) and its successor Vim (Vi improved) is pretty much everywhere.

There’s not an operating system out there that won’t run it. But there may well be ones that don’t support your favourite text editor – particularly if you’re using a remote machine that doesn’t have a graphical user interface (GUI).

If you’re logged into a remote server, a text editor like Atom or Notepad might not be there to help you. And if you’re thinking of using your mouse you can probably forget that, too. So, if you’re reliant on both of those, you’re pretty much stuffed.

Know Vi(m), though, and you’ll have the tool to survive in the harsh GUI-less environment, where only your wits and your keyboard are on hand to save you…

What’s the syllabus?

At Seccl, we’re lucky enough to have a subscription to Udemy, the fantastic online video learning platform.

The whole team is encouraged to use it to pick up new skills or brush up existing ones – and so far we’ve clocked up nearly 60 hours of learning since we started using it in February.

There are more than 100,000 online courses to choose from, so it’s no surprise that one of them is a ‘Vim Masterclass’. And with 15,382 students already enrolled, I guess we’re in good company.

So we spent the majority of our lunch hour following the first few classes on Rhymes’s machine.
Following along remotely to our Vim Udemy course on Rhymes's machine

First things first…how do I get out of this thing?

One of the common challenges that people face when getting stuck into Vim is, well, just that – they get stuck.

Sam, our front-end engineer, revealed that he’d “never used Vim before until about six weeks ago, when someone showed me how to use it. They left me alone for about 15 minutes and I’d wanted to exit, but I was stuck inside.”

Don’t worry, Sam, you’re not alone in the struggle. More than 2 million people have wondered how on earth to leave, since the question was first posed on Stack Overflow over 7 years ago.
Over 2 million people have asked how to quit Vim on Stack Overflow...

And so we focused this time on getting to grips with the absolute basics: understanding Vim’s different modes, learning how to create and save a new file, navigating within it – and, crucially, getting out.

Normal vs insert mode

Vim has a few different modes to be aware of. Normal, or ‘command’ mode, is the default mode that you’re in when you first enter vim.

It’s from here that you can issue commands. Just hit : and you’ll enter ‘command line’ mode – you can now type whatever command it is that you want to issue.

Then there’s ‘Insert’ mode. This allows you to insert and edit text within a file. To get here just hit i – it’ll show --INSERT-- at the bottom of your terminal to confirm that you’ve entered it.

Insert mode is like the default mode for most text editors – it’s where you can actually insert and edit text. The reason Vim doesn’t default to this mode is because there’s no GUI. So Normal mode is, in a way, a replacement for the mouse and menu bar – your way of navigating around and between files. This handy blog explains it pretty well.

To return to normal mode at any time, just hit Esc.

And to exit Vim? It’s :q. Q for quit. To be fair to Vim, it does say it on the first screen…

The essential Vim commands...

Creating and navigating a file

Next we learned how to create a file, or navigate to an existing one. It’s as simple as vim [FILENAME].

Creating a file automatically places you within it so you can insert your content. To save and quit it’s esc (to enter normal/command mode) followed by :wq.

If you try and quit :q without saving your changes, it won’t let you. But if you want to quit and discard your changes, you can use :q!

Then we learned all the ways of navigating around a file, using only the keyboard:

Ctrl f – forward (page down)

Ctrl b – back (page up)

w – forward one word (includes punctuation)

W – forward one word (ignores punctuation)

b – back one word (includes punctuation)

B – back one word (ignores punctuation)

^ – beginning of a line

$ – end of a line

gg – beginning of file

G – end of file

:[line number] – takes you to that line

h – move to the left

l - move to the right

k - move up

j - move down
Learning the Vim navigation commands

Putting it into practice

By then, weary of classroom learning (in retrospect, learning and lunch are deeply incompatible), and daunted by the fact that we’ve barely scratched the surface, we decided to apply our new and very limited knowledge in a game.

Vim Adventures lets you practice your navigation commands in a fun way that won’t put much pressure on your computer’s graphics card… Throw in the group competition element and you’re onto a winner.

Applying our wafer thin knowledge in the 'Vim Adventures' game...

In the words of our friendly and knowledgeable Udemy teacher, “even if you’re not trying to make Vim fun, it can be a joy to use.” It’s early days on that one – but we’ll see!