Vim text editor
In the previous section, you created a few files, but they were blank. This section describes a tool—Vim text editor—to put content into files and edit that.
Command-line editor
Vim is a command-line text editor—a single window with text input and output only. Intended as a plain text editor, it is similar to Notepad on Windows or Textedit on Mac. However, it has a lot more power compared to them. Everything in Vim is done via the keyboard, without a mouse.
There are two modes in Vim:
- Insert (or Input): you can input or enter content into a file.
- Edit: you can move around the file, perform actions, such as deleting, copying, searching, replacing, saving, and so on.
A common mistake is to start entering commands without first going back into the Edit mode or to start typing input without first switching to the Insert mode. However, if you do either of these, it is generally easy to recover.
When you run Vim, you typically issue it with a single command-line argument representing the file you would like to edit:
vim <file>
If you forget to specify a file, there is a way to open the file within Vim, but the easiest way is to quit Vim and have another go. Also, remember that when you specify a file, it can be with either an absolute or relative path.
First off, let's move into the directory you created using the File manipulation section instructions. You will create a few files, which will keep them out of the way of your usual stuff.
To open a file, run the following command:
user@bash: vim firstfile
If the file does not exist, the command creates it for you and then opens it up. Once you enter Vim, it looks something like this, depending on what system you are on:
~
~
~
~
~
"firstfile" [New File]
You start in the Edit mode, so the first thing you should do is switch to the Insert mode by pressing i. Once you are in the Insert mode, you will see it in the bottom left corner.
~
~
~
~
~
-- INSERT --
Now, type in a few lines of text and press Esc, which takes you back to the Edit mode.
Saving and exiting
There are a few ways to go about saving and exiting the file you are currently in. All these methods do essentially the same thing, so pick whichever way you prefer.
First, make sure you are in the Edit mode. If you are unsure whether you are in the 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 press Esc to be sure. If you are already in the Edit mode, pressing Esc does nothing, so you won't do any harm.
To save a file, use one of the following commands:
ZZ: save and exit; use capital letters only.:q!: discard all changes since the last save and then exit.:w: save but don't exit.:wq: again, save and exit.
Most commands within Vim are executed as soon as you press a sequence of keys. Any command beginning with a colon (:) requires hitting Enter to complete the command.
Other ways to view files
Vim allows you to edit files. If you want, you could use it to view files, but there are two other commands that are a bit more convenient for that purpose.
The first command is cat, standing for concatenate. Its main purpose is to join files together. In its most basic form, the command is best suited for viewing files.
cat <file>
If you run the cat command with a single command-line argument representing the file you just created, you will see its contents displayed on the screen, followed by a prompt.
If you accidentally run cat without giving it a command-line argument, you will notice that the cursor moves to the following line, and then nothing happens. Because you don't specify a file, cat instead reads from something called STDIN that defaults to the keyboard. For details, see the Piping and redirection section.
If you type something, hit Enter, and you see the cat command mirror your input to the screen. To get out of here, press Ctrl + C, which is the universal signal for Cancel in Linux.
In fact, whenever you get in trouble, press Ctrl + C to get yourself out of it.
user@bash: cat firstfile
here you will see
whatever content you
entered in your file
user@bash:
This command is nice when you have a small file to view. If the file is large, most of the content flies across the screen, and you only see the last page of the content. For larger files, there is a better-suited command—less.
less <file>
less allows you to move up and down within a file using the arrow keys. You can go forward a whole page using the SpaceBar or back a page by pressing b. When you are done, press q to quit.
Have a look at the file you created using both the commands.
Navigating file in Vim
Now let's go back into the file you created and enter some more content. In the Insert mode, you can use the arrow keys to move the cursor around. Enter two more paragraphs of the content, then hit Esc to go back to the Edit mode.
Below are some commands you can enter to move around the file:
- Arrow keys—move the cursor around
- j, k, h, l: move the cursor down, up, left, and right accordingly
- \^ (caret): move the cursor to the beginning of the current line
- \$: move the cursor to the end of the current line
- nG: move to the nth line, for instance, 5G moves to the 5th line
- G: move to the last line
- w: move to the beginning of the next word
- nw: move n words forward, for instance, 2w moves two words forward
- b: move to the beginning of the previous word
- nb: move n words back
- {: move one paragraph backward
- }: move one paragraph forward
Typing :set nu in the Edit mode within Vim enables line numbers.
Deleting content
Deleting works similar to movement. In fact, several delete commands allow you to incorporate a movement command to define what is going to be deleted.
Below are examples of how you can delete content within Vim:
x: delete a single character.nx: delete n characters, for instance, 5x deletes five characters.dd: delete the current line.dn: delete action followed by a movement command. Delete to where the movement command takes you, for instance,d5wmeans delete 5 words.
Undoing
To undo changes in Vim, use the following commands with the u character:
u: undo the last action; you can continue pressing u to keep undoing.U(capitalized): undo all changes to the current line.
Other Vim actions
After reading the previous sections, you know how to do basic editing in Vim. However, this is just touching the surface of what Vim can do. A basic search in a search engine of your choice for Vim <insert concept here> will find you many pages with useful information. There are many Vim cheat sheets listing available commands, such as:
- Copy and paste
- Search and replace
- Buffers
- Markers
- Ranges
- Settings
Activities for practicing
Let's play with content:
- Start by creating a file and putting content into it.
- Save the file and view it in both
catandless. - Go back into the file in Vim and enter more content.
- Move around the content using at least six different movement commands.
- Play about with several of the delete commands, especially the ones that incorporate a movement command. Remember, you can undo your changes, so you don't have to keep putting new content in.