0%

0/1 Lessons

Introduction to Linux Server Operating Systems

• 30min

0 / 3 lessons complete

Linux Server Installation and Lab Setup

• 23min

0 / 6 lessons complete

Working with the Linux Command Line Interface

• 1hr 30min

0 / 12 lessons complete

User and Group Management

• 44min

0 / 7 lessons complete

Linux Storage

• 30min

0 / 6 lessons complete

Linux Administration Basics

• 53min

0 / 8 lessons complete

Linux Networking

• 47min

0 / 8 lessons complete

Course Conclusion

• 5min

0 / 1 lessons complete

How to use the Vim editor

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.

Create note

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:

image 1
The Vim welcome screen

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.

image 2
No write since last change (add ! to override)

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:

  1. 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.
  2. 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 line
  • j: Move the cursor down one line
  • h: Move the cursor left one character
  • l: 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 cursor
  • I: Insert text at the beginning of the line
  • a: Insert text after the cursor
  • A: Insert text at the end of the line
  • o: Open a new line below the current line
  • O: Open a new line above the current line

Deleting Text

In Command Mode, you can delete text using:

  • x: Delete character under the cursor
  • X: Delete character before the cursor
  • dw: Delete from the cursor to the end of the word
  • dd: Delete the entire line

Advanced Editing

Vim also offers advanced features:

  • yy: Copy the current line
  • yw: Copy the current word
  • p: Paste copied text after the cursor
  • P: 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 file
  • n: 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.

0 0 votes
Lesson Rating
Subscribe
Notify of
profile avatar
0 Comments
Inline Feedbacks
View all comments