A fond memory with my favorite command-line text editor: Vim

Bram Moolenaar – the creator of Vim – died a few days ago, making it feel like as appropriate a time as any to share a fun story that he enabled with his software.

Vim isn’t my main text editor, but it is my main text editor when I’m SSHd into a server and need to edit a file from the command line. Thank you, Bram!

It was a year or so into my first job out of college, and I was on a family vacation in North Carolina. I was with my dad, dragging fishing gear to a canal we wanted to try dipping a line into, and I started getting some panicked texts from my friends at work.

We’d recently launched our first Drupal site on a shiny new virtual private server (VPS) running FreeBSD with no desktop UI, and had a product or raw material shortage of some sort that needed to be up at the top of the website on an emergency basis. I hadn’t built the functionality to do that yet, but it could be added easily enough.

This is a small marketing department, not some large production with an agency managing the brand and all that. I was the only developer doing anything on this thing, so I didn’t have a problem making a quick change on the server and cleaning it up when I got home. Neither did anyone else.

This particular site being Drupal, I could simply add a new region to the theme where we wanted it (at the top of the template), then let someone else with a user account know where to put the content.

So I put down my stuff, logged in to the server via SSH using Prompt on my iPhone (which is a product that absolutely slaps, by the way, if you’re the type of nerd who wants to SSH into servers from your iPhone), added the new region and cleared the caches with Drush (which also slaps). Picked up my stuff and caught up with my dad.

Obviously, leaving a dirty file on my production server (and, uh… testing on it, for that matter) while I’m out on vacation isn’t ideal, nor is this what I’d call a typical use case for a CLI text editor, but we needed a resolution, and I could deliver quickly thanks to the connectivity provided by the phone, and a command line text editor waiting for me on the server.

A more common/practical use case might be the editing of configuration or other files on the server, e.g. editing Nginx or Apache config to spin up a new website, or tweaking firewall config.

If you host your own websites, or have ambitions of doing so, I strongly recommend familiarizing yourself with Vim, Nano, or another CLI editor. In the spirit of doing some Vim evangelism to future generations of computer dorks, I’ll cover here some of the essential parts I use, personally.

I’ve worked with quite a few developers over the years who use it as their main text editor, and if you really take the time to learn it and lean into it, it can be wildly powerful. Usually this developer is way better at regex than I am, and with far stronger opinions about how their text editor behaves. I am not this developer (at least not when it comes to CLI text editors), and don’t usually get too far away from out-of-the-box defaults unless something is really in my way. These are the bits of Vim that I use enough to memorize them.

The important parts of Vi/Vim

There are so many other guides out there for Vim and other CLI text editors, but my aim is to make a brief, practical guide of only the minimal parts I use myself as a software-first person who dabbles in devops/sysadmin.

I like Vi and/or Vim because it was what I was originally taught, and it also seems to be preinstalled on most Linux servers I’ve worked with. It’s almost certainly one of the first things cut from those leaner, smaller Linux flavors that end up powering people’s Docker apps, but if you’re deploying Docker containers and find yourself needing to SSH into a live container to edit a file, I imagine you did something pretty wrong somewhere (not that I’ve got room to talk).

Insert vs. Normal/Command/Interactive

The first thing to know about Vim is that it has two main modes: Insert and Normal (I’ve seen it called a few things, but we’ll roll with this). Normal mode is sort of like switching off your keyboard’s usual functionality, and turning it into an alternate keyboard with buttons specially tailored for editing text. More on this shortly.

One exits Normal mode and enters Insert mode by simply hitting the letter ‘i’ on your keyboard, allowing you to type/edit text like you normally would. It should say ‘INSERT’ somewhere in the bottom left of your screen. Out of the box, this is a minimal version of the text editing experience you know and love from elsewhere, even if the setting is a little more barbaric.

When you’re finished editing and want to quit (or do anything else other than edit), you hit Esc once to exit Insert mode and go back to Normal. From here, many of the keys on your keyboard have new superpowers, allowing you to copy or delete entire lines, navigate to different parts of the document, or search it.

Delete an entire line

At some point in working with config files, deleting entire lines comes in handy. This is a double-tap of the lowercase ‘d’. From Normal mode: dd

Delete a single character

Carving out a single letter or two also comes in handy rather often for me. In Normal mode, move the cursor (with the arrow keys, although a Ctrl + click may work) to the character you want to ax, the hit x.

Search

Vim is at least as capable as any other editor out there, but it’s almost never obvious how to do anything. Searching a file is as great an example of that as any. You can kick off a search from Normal mode simply by pressing / and typing your string. From there, you can hit Enter or n to move to the next result.

There’s actually a somewhat baffling amount of power just here when you factor in that you can use regular expressions and such here. Most of that is very need-to-know, and I dig into documentation if I need to do something more involved. Usually if I’m in a file with Vim and using this, it’s just a quick check for a specific thing I expect to be there, and never replacing strings or anything like that.

Saving

In this world, we think of it less as ‘saving’ and more as a ‘write to disk’. So rather than ‘save’ being the operating word, or clicking on a floppy disk, we issue a ‘write’ command. We do this from within Vi or Vim by using Esc to get back to Normal, then typing a colon, followed by a ‘w’, then Enter.

Yes, we are a far cry from clicking on a floppy disk icon.

If it doesn’t let you saved, it’s probably because you’re trying to edit a file you don’t have adequate permissions to edit. If you’re editing server config (e.g. Apache/Nginx files in your /etc directory) and haven’t assumed root permissions when editing the file (i.e. with sudo vim file-to-edit.conf) it will assume you’re not allowed to do what you’re trying to do.

Quitting

As long as Vim continues to exist, so too will jokes bout quitting Vim exist. It’s actually pretty easy. It’s like saving, but with the letter ‘q’. Hit Esc to get back to Normal, type your ‘q’ and hit Enter.

If you try to quit without saving, it’ll stop you and tell you about your dirty file. If you want to quit anyway, simply add an exclamation point at the end. :q!

Veteran Vim users often write and quit at the same, which simply combines the two: :wq

Copy/Paste

While I don’t use it quite as often, knowing how to copy and paste never hurts. But in Vim, we don’t copy and paste. We yank and paste.

From Normal mode, y will ‘yank’ the whole line, and p will paste it.

Try it!

  • From the command line, change into the directory within which you want to create a test file (e.g. cd ~/Desktop)
  • Start Vim for the creation of a new file by just typing vim my-test-file-name.txt
  • Press i on your keyboard to enter Insert mode and type a few lines just like you would in any other text editor
  • When you’re done, go back to Normal mode by pressing Esc, then try deleting a whole line by placing the cursor over it and typing dd, or a single character, with x
  • Save and quit at the same time by issuing :wq from Normal mode
  • On a desktop? Try double-clicking on your new .txt file to see it open like regular people do.

©2024 Joe Castelli