How to use the Vim editor
Full-Access Members Only
Sorry, this lesson is only available to Server Academy Full-Access members. Become a Full-Access member now and get instant access to this and many more premium courses. Click the button below and get instant access now.
Instructions
Q&A (0)
Notes (0)
Resources (0)
Saving Progress...
Resources
There are no resources for this lesson.
Notes can be saved and accessed anywhere in the course. They also double as bookmarks so you can quickly review important lesson material.
In this lesson, you will learn how to use the Vim editor, an improved version of the vi editor, commonly used in Unix and Linux systems. Vim stands for Vi IMproved and offers a robust set of features that make it a powerful tool for text editing and programming.
Vim or vi?
Sometimes, you can type vi, but you'll actually start the Vim. You can verify that by taking a look at your vi command as shown in the example below, which shows that my vi command actually starts vim:
$ which vi
/usr/bin/vi
$ ls -la /usr/bin/vi
lrwxrwxrwx 1 root root 20 May 16 02:08 /usr/bin/vi -> /etc/alternatives/vi
$ ls -la /etc/alternatives/vi
lrwxrwxrwx 1 root root 18 May 16 02:09 /etc/alternatives/vi -> /usr/bin/vim.basic
Starting Vim
To begin with, depending on the Linux distro you're using, you can start the Vi / Vim editor with the vi
or vim
commands:
Exiting Vim
As shown on the welcome screen, to exit the Vim editor, you need to first be in command mode. Press the esc
key to ensure you are in command mode. Next, type :q
and press enter
.
If you receive a message highlighted in red like No write since last change (add ! to override), then you need to type :q! to exit without saving changes you may have made.
Creating a new file with Vim
Now that you understand that, you'll want to open a file in Vim. Use the following command to either create a new file or open an existing one:
vi filename
Alternatively, if you want to open a file in read-only mode, you can use:
vi -R filename
Or:
view filename
Modes in Vim
Vim has two primary modes:
- Command Mode: This mode allows you to perform administrative tasks such as saving, executing commands, moving the cursor, etc. To ensure you're in Command Mode, press
Esc
twice. - Insert Mode: This mode allows you to insert or append text into the file. To enter this mode from Command Mode, press
i
.
Navigating Within a File
To move around within a file, use the following keys in Command Mode:
k
: Move the cursor up one linej
: Move the cursor down one lineh
: Move the cursor left one characterl
: Move the cursor right one character
Basic Editing
To insert and edit text, switch to Insert Mode (i
) and type your text. To switch back to Command Mode, press Esc
.
i
: Insert text before the cursorI
: Insert text at the beginning of the linea
: Insert text after the cursorA
: Insert text at the end of the lineo
: Open a new line below the current lineO
: Open a new line above the current line
Deleting Text
In Command Mode, you can delete text using:
x
: Delete character under the cursorX
: Delete character before the cursordw
: Delete from the cursor to the end of the worddd
: Delete the entire line
Advanced Editing
Vim also offers advanced features:
yy
: Copy the current lineyw
: Copy the current wordp
: Paste copied text after the cursorP
: Paste copied text before the cursor
Saving and Quitting
To save your file, you can type :w
in Command Mode. To quit, use :q
. If you want to save and quit simultaneously, you can use :wq
or simply ZZ
.
Running Commands in Vim
You can run shell commands from within Vim. To do so, type :! command
. For example, to list files in the current directory, type :! ls.
Searching within the File
/
: Search forwards (downwards) in the file?
: Search backwards (upwards) in the filen
: Repeat the previous search
Server Academy Members Only
Sorry, this lesson is only available to Server Academy Full Access members. Become a Full-Access Member now and you’ll get instant access to all of our courses.