How to Exit Vim? | Your First Lesson In Vim

This is the second article in the series titled, "Your First Lesson In Vim". These articles are written with a goal of helping out new Vim users by teaching the awesomeness of the Vim editor and there by extending the Vim community. Vim though quite powerful, has a bad rep for being hard to learn and hard to get started with. So, even when someone is interested in learning about Vim, that infamous learning curve seem to be scaring them off. This series is going to put an end to all of that.

Vim Logo

In the last article Introduction & Installation we have seen why Vim is the best and coolest editor ever. Hopefully after watching Damian Conway's YouTube video given at the end of that article, you would agree. In this article we will experience Vim for the first time. We will learn about the various modes of operations in Vim. And, most importantly as the title of the article suggests, we will learn How to Exit Vim.

First thing's first, You have to open Vim. Duh!. You can do that either by directly searching for Vim in your Search box or by typing vim in to your terminal. If you want to start off with gvim then open that instead. Your Gvim or Mac Vim would look something like this.

Gvim startup picture

If you have already tried to type something, you would observe that there is something shady going on here. For example, if you type hello world you might observe that only world is displayed and hello is no where to be seen. Try it out for yourself. This happens because of the infamous Vim modes. One of the first things you have to realize while using Vim is that its not like your typical run of the mill text editor. Vim works a bit differently and Modes is one of the key things that makes vim different. So, let's take a look at them.

Broadly speaking Vim has Four major modes of operation. That number keeps changing depending on who you're talking to because there are a few more modes that can technically be called sub-modes but some people insist on treating them as Seperate modes. But to keep things simple here 4 is the magic number for you and 4 is the answer to Life, Universe and Everything. Not 42, 4!

The modes are :

Normal Mode

Normal mode is the default mode you will be in when you open Vim. Normal mode is used for altering, deleting and formatting text. You won't be inserting any new text into the document in this mode. Normal Mode is the mode you will be spending most of your time in. You can get to Normal mode by pressing ESC from any other mode. One of the main things you will be doing in this mode is moving around your document.

To move around the file in the window you might usually be using arrow keys. In Vim they will work the way you expect them to, but instead vim advises to use h .. j .. k .. l for moving the cursor. The reason why this came to be and the advantages of this will be apparent in the next article but for now let's see how this works.

h moves the cursor left, l moves it right

j moves the cursor down, k moves it up

This following picture would make the idea clear.

vim navigation

If you are thinking to stick with the arrow keys instead of h j k l, it is fine. There are a lot of people who use vim this way. But trust me when I say using h j k l speeds up you work flow a lot. Once you get used to this you wouldn't want to use arrow keys anymore. But anyway we will discuss more about the this in the next article.

Working in Normal mode, you will see how everything you do get's easy in Vim. For a quick sneak peak of some commands.

Command What it does?
dd Copy (yank) the current line
p paste the copied text below the current line
u Undo your previous change
gg Go to the beginning of the file
G Go to the end of the file

From now on you don't have to awkwardly select the full line with your mouse to delete it. All you have to do is press dd and that sucker goes away.

Didn't mean to delete it? No problem. Just hit u and it undoes the delete. No more holding down Ctrl and z. Alsou for undo is so simple to remember. What does Z even mean in Ctrl + z? And how did that become synonymous to Undo?

Similarly no more Ctrl + c and Ctrl + v to copy and paste. yy and p got you covered.

So you see, Vim sticks to its philosophy of making you productive. Imagine all the keystrokes you save per day, per year. So, switching to Vim doesn't just improve your productivity, it take care of your health too. With every Key you saved keeps you a key away from getting Carpel Tunnels and RSI. So, Use Vim - Stay healthy. :]

You might be happy with using Ctrl+c to copy and Ctrl+v to paste in your plain old editor. Its absolutely fine but Vim offers a simple and easy alternative and honestly the choice is pretty clear.

Anyways, that is about Normal mode for now. We will discuss more later when required. Let's look at Insert mode.

Insert Mode

As the name suggests Insert mode is where you will inserting text and that is all you will be doing in here. You enter Insert mode by pressing i in Normal mode . And in almost all Vim distributions you should see a noticeable change in the cursor right away. It would have changed from a block type cursor () to an I-beam (|). That's your indication that you're in Insert mode. In another tutorial we will see how you make that even more apparent. Whatever you type in Insert mode would be displayed on the file literally. If you type a b c, it types in those characters in to the file as you would expected. Contrast this from pressing dd in Normal mode which doesn't print them on the file but instead does something to the file (In this case, a delete operation).

Unlike other editors you wont be spending much time here and Infact I'd advise you to get out of Insert mode once you are done typing. To go out of Insert mode you just have to press Esc and you will be back in Normal Mode.

Now, Let's get to the fun part of insert mode. Remember before when I said you go from Normal mode to Insert mode by pressing i, well, It turns out it is just one of the ways to get in to Insert mode. There are five more ways in which you can enter Insert mode and you can choose the best one based on what you need. Sounds complicated? Let's list them down first.

Command What it does?
i Enters Insert mode with the cursor placed before the current character
a Enters Insert mode with the cursor placed after the current character (Remember a - after)
o Enters Insert mode by opening a new line below the current line (Remember o - open)
I Enters Insert mode by placing the cursor at the beginning of the line (Remember big I - bigger version of i)
A Enters Insert mode by placing the cursor at the end of the line (Remember big A - bigger version of a)
O Enters Insert mode by opening a new line above the current line (Remember O - bigger version of o)

As explained each one has a specific purpose.

If you want to quickly create a new line above the current line and start typing - You press O

If you want to insert a new line below the current line - You press o

To add something quickly at the end of a line - You press A

To add something at the beginning of a line - You press I

Could that be any more simpler? Surprisingly none of the other popular text editors do this. I can promise you that you won't be able to move so quickly in any other editor. This is Vim's power.

Let's look at another easy mode that will help you visualize things better, Enter Visual Mode.

Visual Mode

If you have carefully looked at things till now you might have started to feel that Vim favours Keyboard commands using a mouse. If you thought so, you would be absolutely correct. So, in the spirit of No Mouse, Visual mode tries to emulate Visual selections of your text similar to the way a mouse selects on Screen but instead with completely with the keyboard.

To enter in to Visual mode, just press v and move your cursor with either h j k l or the arrow keys and you will see that the text is getting highlighted indicating that it has been selected. Now, what can you do on this selected text? You can press d and delete it completely or you can press y and copy it. Notice that these are d and y and not dd and yy like in Normal mode. With v, Visual selection happens character by character. But if you want to select the full line, press V instead and you have the whole line highlighted and you can delete, copy or run any other command on the highlighted text.

This is how it looks like when you've something selected in Visual mode.

Vim visual mode selection

And to exit out of Visual mode or to cancel the selection, just press ESC.

Command What it does?
v Visual selection by character
V Visual selection by line

We are finally down to the last mode, which is the Command mode (You don't take Command, Son)

Command Mode

Vim command mode is very powerful and one of the reasons why Vim is so versatile. Command mode is where you type Vim's commands, Vim configurations, Plugin settings, Open new files, close existing files and also access Vim's builtin help documentation. You enter to Command mode by typing : and then you type in the command you want. After you press : you will see the cursor at the bottom of the screen (called the last line appropriately) and you type the command.

vim command mode example

To open a file, you type in :e file_name (:e is short for :edit)and hit Enter. If the file exists Vim will open it for you and if doesn't exist Vim will open blank file for you and the file will be created when you save it.

To save or rather to write the file to disk, you do :w and hit Enter for it to be saved. If the file doesn't yet have a name, You type :w file_name and it will save the contents of the window with that file name.

And Now for the most important question in all of Vim's History and the given title of this article, How to exit Vim! If you are using a Graphical version of Vim, then closing Vim is the same as closing the window and poof, its gone. But If you're using a terminal (works in gvim and macvim too) then you quit Vim by typing :q (short for :quit) and that closes the current window. If you have unsaved changes in your buffer Vim will give an error saying No Write Since Last Change. If you don't mind discarding unsaved changes, you append a ! and so the command becomes :q!. That is all there is about how you exit Vim. The bang(!) at the end is similar to -f or --force option in a lot of linux commands. It forces Vim to quit even when there are unsaved changes.

From now on if you ever saw a meme like this, you know what they are talking about.

vim how to exit meme

Another important Vim command is :help. It contains the full help manual for Vim and so should be one of your most used commands in the initial days of learning.

And similar to other modes, you exit command mode by pressing the Esc key and you will be back in the Normal mode.

This image illustrates how you switch from one mode to another

Vim_modes

Okay. That's a lot of information for one article. Let's do a quick review.

Story Recap

There are four modes of operations in Vim.

  • Normal Mode : moving around the document, deleting, copying, formatting are some of the common things you do in this mode
  • Insert Mode: Inserts text in to the document. Go into Insert mode by typing any one of a A i I o O in Normal mode. Come out with Esc
  • Visual Mode : For visually selecting the text. Enter with v or V and exit with Esc
  • Command Mode : To execute commands. :w to save, :help for documentation and :q to quit.

Well, That is all for this article folks. Will see you again in the next one. Until then, Keep practicing and Happy Vimming!

← Prev Next →

Attributions:

Vim Logo - Vim Replacement Icon http://wolfrosch.com/works/goodies/vim (CC BY-NC-ND 3.0)

Vim Comic image - https://comic.browserling.com/vim.png

social