Monday, November 9, 2020

How to get started with Vim

Got Vim installed and excited to your first steps? Read to understand what you need to know next
Photo by Alex Knight on Unsplash

So, now that you installed Vim on your Mac, Windows or Linux workstation, let's review what's next and how to get started with Vim.

Getting started with Vim

The first thing you should understand about Vim is its modes. Vim is not a traditional editor in which you simply type your stuff by using the keys of your keyboard and move you cursor using the mouse and the arrow keys. Vim is way, waaaaaay more than that. And it's exactly because of that that we ๐Ÿงก it.

But why get to the theory before we can use Vim? Because it's important to understand it so you can exit it safely ๐Ÿ˜†.

Vim Modes

We'll keep this straightforward and for simplification, we want you to understand the two modes you will first meet in Vim:

  1. Normal mode: the default mode. This is where you usually enter your commands or move your cursor
  2. Insert mode: the mode you type stuff. Equivalent to most text editors.
With the above said, which is the mode Vim starts by default? As you probably expect, it's the normal mode. We'll see how to switch modes next.
There are more modes in Vim but let's keep it simple for now. We'll address that in a future post.

Starting Vim

So go ahead and start Vim by typing vim on your terminal:
vim

This is what you should see:

Why use Vim in the terminal? Because Vim is a terminal-based text editor. It's one of the reasons Vim is widely popular and ubiquitous.

Scary? Maybe. So let's quickly analyze the above, shall we? There are interesting things there that tell us a lot already about Vim:

  1. VIM means Vi IMproved. Vi was a text editor originally written by Bill Joy from which Vim is inspired from
  2. Our version: 8.1.2269
  3. Bram Moolenaar, Vim's author
  4. Some commands, including how to quit Vim (yay!) ๐Ÿ˜Š

Typing Text

If you started a text editor, you probably want to type some text. As previously said, this is our first second contact with modes. You know already that we're in normal mode and you want to go to insert mode so you can write your text. The simple way to switch to insert mode is by typing i. A -- INSERT -- message will show up on the status bar indicating that you're now in insert mode. Go ahead and type your stuff. We'll type "Hello Vim4Us!":

Tip: there are many ways to get into insert mode but let's stick with i for now.

From here we may want to save our file or exit discarding the changes. Let's see how to do both.

Saving our file and quitting Vim

The first thing we should do in order to save our file is leaving insert mode and going back to normal mode. Going to normal mode is as simple as pressing <Esc>. Once you press it, you'll realize that the -- INSERT -- label disappears from the status.

To save our file as hello.txt, now type in normal mode:

:w hello.txt

And exit with:

:q

Exiting Vim discarding the changes

If however you want to discard your changes and thought that using the same :q command would work, you're wrong. This is what you would get:

But why? Because the :q command tells Vim to quit. But Vim on the other hand is helping us and alerts us that we have unsaved changes. To confirm that we want to exit and lose changes we then have to use the ! operator. The ! operator tells Vim to override the command and execute it regardless of the effect it would have.

So to quit Vim ignoring changes, type in normal mode:

:q

Next Steps

Now that you understand the first two basic modes in Vim and knows how to change modes and run some commands in Vim, we suggest that you start your first Vim tutorial. But we'll cover that in a future post.

Conclusion

On this post we reviewed the basics of getting started with Vim. You now understand that Vim works with modes and know how to change modes and run some commands in it, including saving a file and quitting. What's next? Keep tuned.
Any comment about this page? Please contact us on Twitter

Featured Article

Vim - Ex Mode

When learning Vim, it's important to understand its modes, including the  Ex Mode  where you can continually type your commands u...

Popular Posts