Until recently, I had done all of my coding in IDEs and GUI text editors. From Notepad++ to Visual Studio and Xcode, I felt like my text-editing toolset was more than adequate for the work I needed to do. I knew about Vim and Emacs, but they both seemed like esoteric, rocket science editors that only became relevant when a Git merge forced me into the file of a commit message.
Beginning with my first project here at Atomic, however, Vim has become an indispensable tool for developing on remote machines and quickly editing miscellaneous files. In fact, it’s slowly but surely becoming my editor of choice. This is a guide for those who are transitioning to Vim from GUI editors and need to know the bare minimum before steadily becoming an expert. Hopefully, after reading this article, you will be able to use Vim almost as effectively as Sublime or VS Code.
Before We Begin
- Keep in mind that Vim has different modes. The relevant ones here are Normal mode, which lets you move around the file and enter commands, and Insert mode, for actually adding text and backspacing.
- As you’re learning Vim, pay close attention to the mnemonics associated with different commands. They aren’t always obvious (for example,
`y`typically means “yank,” which is equates to “copy”), but they will reduce the learning curve significantly.
- Don’t forget that this is a guide to using Vim like a GUI editor. That’s not quite the same thing as using Vim as effectively as possible. Over time, try to break your usual habits in favor of more Vim-specific ones. For example, you can usually prefix a command with a number to execute it as a batch, instead of simply repeating the command over and over.
- Throughout this article, I’ll be referencing keys associated for macOS, like
`opt`(⌥). Translating to Windows-specific keys should be fairly trivial.
Ideally, all of the action in Vim takes place on the keyboard. Using your mouse or trackpad to move the cursor is strongly discouraged, and for good reason: Vim provides a plethora of commands for navigating a file efficiently.
In a GUI editor, most navigation occurs via (1) the arrow keys, (2) simple shortcuts like
`cmd ⇦` and
`opt ⇨`, and (3) scrolling/clicking with the mouse or trackpad. It’s easy to achieve the same movements in Vim:
- The arrow keys are always available for use, whether in Normal or Insert mode.
- While in Normal mode, use
`b`to navigate to the beginning of the word to the left of the cursor, or
`e`to navigate to the end of the word to the right of the cursor.
`0`to move to the beginning of the line, and
`$`to move to the end of the line.
- Since you really should avoid the mouse/trackpad, scrolling through a file can be accomplished pretty effectively with
`cmd d`, which move the cursor half a page up and down the file respectively. For full-page jumps, use
`cmd b`and `cmd f`.
- To jump up or down by a certain number (#) of lines, use
`#k`. Or, jump directly to any line with
`#G`(one of my favorite shortcuts).
Unlike GUI editors, you can’t simply start typing in Vim to insert text. First, you have to get into Insert mode. There are several convenient ways to do that:
`i`is the simplest – it puts you into Insert mode without moving the cursor.
`o`first opens a new line beneath the current one, then starts Insert mode.
`o`, but the new line is opened above the current line.
`A`moves the cursor to the end of the line before starting Insert mode.
It may take a while to get used to working with Vim’s different modes. To make it easier, get back to Normal mode (by using
esc) any time you are not actively inserting text. Otherwise, you’ll inevitably start trying to enter commands before realizing you’ve simply inserted them into the file.
Here are some common ways to delete text in a GUI editor, along with the corresponding commands in Vim. Keep in mind that there are always multiple ways to accomplish the same thing, and that these are just what I consider the most straightforward options.
- Simple backspacing:
`x`will delete the character under the cursor when you’re in Normal mode.
- Deleting backwards, one word at a time (like
`opt dlt`): Use
`db`. Instead of typing two characters each time when deleting multiple words in a row, use
`.`to repeat the deletion command.
- Deleting to the beginning of the line (like
`cmd dlt`): Use
`d0`. In general, typing
`d`and then some movement command will delete everything between the present location and the movement.
- Deleting a line: In GUI editors, I usually move to the end of the line (
`cmd ⇨`) and then delete backwards (
`cmd dlt`). In Vim, it’s as simple as
- Highlighting a bunch of text and then backspacing: Vim also has a Visual mode, which is analogous to highlighting in GUI editors. Use
`v`to get into Visual mode, then use any Movement commands (or the mouse if you love bad habits!) to highlight what you need to. Then just enter
`d`to delete it all.
Finding and Replacing
Vim has a number of commands for finding and replacing strings. Here are the most essential ones:
- Instead of
`cmd f`to find a string, type
`/some` stringthen hit
enterto go to the next occurrence (if any) of “some string,” and then
`n`to scroll through subsequent occurrences.
- To search upwards through the file, begin with
- To replace all occurrences of a string with another, use this form:
`:%s/original/replacement/g`. Of course, there are all kinds of options for replacing more effectively.
Unfortunately, it’s a bit trickier to rename a method, variable, etc. across multiple files. To do that, I would recommend using the Bash command
sed, and having good unit tests in place to quickly confirm that you didn’t break anything. This is also a good opportunity to appreciate the power of IDEs.
Working Across Files
Two nice features of GUIs are seeing/tabbing between open files and navigating through directories. While you may miss out on the visual layouts of these features, here are the closest approximations in Vim (plus some other common commands):
- To list all open buffers (files), use
- To go to an open buffer, use
`:b filename`. You’ll find autocomplete via
tabto be useful here.
- Visualizing a directory is not so easy. My strategy in this scenario is to put Vim into the background with
`ctrl z`, and then search around the directory within the terminal before foregrounding Vim with
- Open up a new buffer with
- To save the current buffer,
`:w`. To close,
`:q`. And to save and close in one fell swoop,
See this nice Stack Overflow answer for some other techniques.
Once you can use Vim as easily as Sublime, don’t stop! There’s a whole world of commands, utilities, and customizations to learn. Here are a few resources to keep the momentum going.
- Practical Vim by Drew Neil (Book)
- Vim Genius (Web Exercises)
- Everyday Vim – A Basic Vim Commands Cheat Sheet
- Running Tests from MacVim in iTerm2 Version 3
- Code Like a Craftsman with a Vim Clutch
Good luck and happy editing!
Are you taking the piss?
In what world is using vim easier than emacs or nano? In every single section, you’ve been forced to highlight how things are more difficult, hard to get used to, or ‘esoteric’. Surely you don’t need extra challenges if you’re ‘developing on a remote server’ (whatever that means, you don’t actually provide a genuine use-case).
The only reasons for using this ancient tech are because you’ve been doing so before GUIs were commonplace, or you’re a hipster who wants to shown off how l33t you are.
Just use Sublime Text over an SSH connection like normal people, and get over yourself.
I like vim bindings for editors. It’s a nice middle ground, I get the familiarity and power of an ide and the speed and keyboard focus of vim. Also makes it easy to use proper vim when the need arises.
Both visual Studio code and visual Studio have good vim keybindings.
Comments are closed.