Vim, Tan, Laundry: Navigating the World’s Best Editor

05. June 2017 0
Vim, Tan, Laundry: Navigating the World’s Best Editor

The question that has bedeviled scholars and scientists alike for millennia: how in the world does one quit Vim?

View post on imgur.com

One quits Vim by typing  :q and then hitting enter. Now that you know how to quit…

why should you opt to stay inside of Vim?

Why use it in the face of other, more modern text editors, like Sublime, Atom, or VS Code? I personally elect to use Vim because it is a modal text editor, and this allows for compose-able commands. But what does this mean?

Let’s start by examining what modal means. In this context, the word “modal” means that in the course of editing a piece of code, Vim users will switch between one of multiple different modes for different functionality. Has your terminal ever shunted you into Vim, and you try to type but haven’t seen the words appear on screen? Worse, you can’t exit, and are forced to close the browser tab for want of understanding how to leave this 40 year old text editor? This is because you are not in the mode that allows you to type characters directly into the screen. You are in the default mode, one that is most useful for moving around the document (as well as copy and pasting). This is called “normal” mode. If you want to type out something, you would enter another mode, named “insert mode” to actually insert characters in the screen.

We will primarily focus on these two modes. Note that once you enter a mode OTHER than Normal mode, you can exit back to it by pressing  <Esc>. Some people remap the caps lock key to be an escape key. The workflow should be to enter insert mode through one of the commands below, enter your text, and then hit the  <Esc>  key to enter back into Normal mode.

If you do something wrong while following this tutorial, or would like to experiment with something and then want to undo it, then know that the undo command in Vim is u while in normal mode. If you’re in insert mode, hit the  <Esc> key to re-enter normal mode and then press u.

To start, type  vim test.txt  into your terminal and hit  <Enter> . You should immediately be entered into Vim with a blank file named “test.txt”. So let’s explore. First, we enter insert mode, which will let us start typing. Listed below are some common commands to enter insert mode. For now, you don’t need to worry about all of these, as it’ll be more fun to experiment once you’re move familiar moving around Vim.

i to enter insert mode and not move the cursor.

a to move forward one space and enter insert mode.

I (an uppercase i, not a lowercase L) to jump to the beginning of a line and enter insert mode.

A to jump to the end of a line and enter insert mode

s to delete the character the cursor is on and enter insert mode.

C to delete to the end of the line and hit insert mode.

cc to delete everything in the line, leaving a blank line, and enter insert mode.

These are all useful but for now, since this is a blank document, just hit i, or if you have already entered text, hit <Esc> and hit the letters ggdG, which should clear your document so you can proceed, then hit i (don’t worry, you’ll understand what all those commands mean by the end of the post).

This will bring you into insert mode. Once inside insert mode, Vim works like any normal text editor: You can type characters into it and see them entered into the file (on in Vim parlance, the “buffer”) on screen. But this isn’t what we’ll be spending most of our time in. Type the following sentences, hitting the enter key at the end of each line to insert line breaks like you would in MS Word…

Vim is the best!

Even though it's slightly annoying.

But it's still worth learning.

…and then hit  <Esc> to exit back to normal mode.

Now that you’re in normal mode, try typing  h. You’ll notice that h is not inserted into the file, as the case would in any other text editor. Instead, you’ve moved your cursor back one character. That’s because in normal mode, all of the keys have different functions, with the h,j,k, and l keys primarily focused on moving around.

h will move the cursor one character to the left

j will move the cursor up one line

k will move the cursor down one line

l will move the cursor one character to the right

These are by far the most important (and used) commands to hit in normal mode, the equivalent of the arrow keys in other text editors.

If Vim’s movement were only restricted to these four keys, and you could do nothing else with them, I would have abandoned the editor a long time ago. Fortunately for us, there are many more ways to move around a Vim buffer. The following are only a couple of the many commands (you can see even more in the cheat sheets listed below). After all, as programmers, we do not spend most of our time writing code, we spend most of our time moving around code, copying it, deleting it, and changing it.

w will move the cursor forward one word.

e will move the cursor to the end of the current word.

b will move the cursor back one word.

0 will move to the beginning of the current line

$ will move to the end of the current line

gg will move to the top of the document

G will move to the bottom of the document

Let’s put to use some of your newfound knowledge. Type gg to move to the top of the document and 0 to move to the beginning of the line. Then, hit i to enter insert mode and enter the words “I have discovered that ” so that your document now looks like that following.

I have discovered that Vim is the best!

Even though it's slightly annoying.

But it's still worth learning.

Great! You’re getting the hang of things. But three lines isn’t very hard to move around in. Let’s make a document with dozens of lines, something that, were we using Sublime or Atom, would be far more cumbersome to navigate. Stay tuned for Part 2, where we will accomplish this by getting into the one of the most common problem areas in Vim for beginners: copying and pasting, or in Vim lingo, yanking and putting.


Leave a Reply

Your email address will not be published. Required fields are marked *