How to exit vim / save
Posted about 5 years ago 2.2kIntroduction
In the last section we created a few files but they were blank. A little boring but we have to start somewhere. In this section we'll look at a tool to put content into files and edit that content as well. Vi is a text editor that is most likely very different to any editor you have used before. It will take a while to get your head around but once you do you will realise it is actually quite powerful. It's kinda like touch typing, initially learning is awkward and you wonder why you're bothering but once you get the hang of it you will not want to go back.
Even if you don't use Vi all the time you will definitely find that work patterns you develop in learning the editor can be transferred easily to other programs and to great effect.
This section and the next few sections are actually forming the foundation for the last few sections where we will put them all together and start doing some really funky stuff. I've chosen to look at Vi first so that your mind has a bit of time to process and make sense of it in preparation for later when we'll need it.
Vi is a very powerful tool. In this section my aim is not to cover everything that Vi can do but to get you up and running with the basics. At the end of the section I'll provide some links to resources where you can learn Vi further. I highly recommend you look into a few of them.
A Command Line Editor
Vi is a command line text editor. As you would be quite aware now, the command line is quite a different environment to your GUI. It's a single window with text input and output only. Vi has been designed to work within these limitations and many would argue, is actually quite powerful as a result. Vi is intended as a plain text editor (similar to Notepad on Windows, or Textedit on Mac) as opposed to a word processing suite such as Word or Pages. It does, however have a lot more power compared to Notepad or Textedit.
As a result you have to ditch the mouse. Everything in Vi is done via the keyboard.
There are two modes in Vi. Insert (or Input) mode and Edit mode. In input mode you may input or enter content into the file. In edit mode you can move around the file, perform actions such as deleting, copying, search and replace, saving etc. A common mistake is to start entering commands without first going back into edit mode or to start typing input without first going into insert mode. If you do either of these it is generally easy to recover so don't worry too much.
When we run vi we normally issue it with a single command line argument which is the file you would like to edit.
vi <file>
If you forget to specify a file then there is a way to open a file within vi but it is easiest to just quit vi and have another go. Also remember that when we specify the file it can be with either an absolute or relative path.
Let's dive in and get started. It's going to be hard for me to demonstrate a lot of this so instead I'll list what I want you to type and you'll have to give it a go and see what happens.
First off let's move into your directory you created in the section on file manipulation. We're going to create a few files and this will keep them out of the way of your normal stuff.
Now we'll edit our first file.
vi <file>
When you run this command it opens up the file. If the file does not exist then it will create it for you then open it up. (no need to touch files before editing them) Once you enter vi it will look something like this (though depending on what system you are on it may look slightly different).
~ ~ ~ ~ ~ "firstfile" [New File]
You always start off in edit mode so the first thing we are going to do is switch to insert mode by pressing i. You can tell when you are in insert mode as the bottom left corner will tell you.
~ ~ ~ ~ ~ -- INSERT --
Now type in a few lines of text and press Esc which will take you back to edit mode.
Saving and editing
There are a few ways to go about doing this. They all do essentially the same thing so pick whichever way you prefer. For all of these, make sure you are in edit mode first.
If you are unsure if you are in edit mode or not you can look at the bottom left corner. As long as it doesn't say INSERT you are fine. Alternatively you can just press Esc to be sure. If you are already in edit mode, pressing Esc does nothing so you won't do any harm.
- ZZ (Note: capitals) - Save and exit
- :q! - discard all changes, since the last save, and exit
- :w - save file but don't exit
- :wq - again, save and exit
- Arrow keys - move the cursor around
- j, k, h, l - move the cursor down, up, left and right (similar to the arrow keys)
- ^ (caret) - move cursor to beginning of current line
- $ - move cursor to end of the current line
- nG - move to the nth line (eg 5G moves to 5th line)
- G - move to the last line
- w - move to the beginning of the next word
- nw - move forward n word (eg 2w moves two words forwards)
- b - move to the beginning of the previous word
- nb - move back n word
- { - move backward one paragraph
- } - move forward one paragraph
- x - delete a single character
- nx - delete n characters (eg 5x deletes five characters)
- dd - delete the current line
- dn - d followed by a movement command. Delete to where the movement command would have taken you. (eg d5w means delete 5 words)
- u - Undo the last action (you may keep pressing u to keep undoing)
- U (Note: capital) - Undo all changes to the current line
Most commands within vi are executed as soon as you press a sequence of keys. Any command beginning with a colon ( :
) requires you to hit <enter> to complete the command.
Save and exit the file you currently have open