Talk Tech to Me: How to Use Vi and Vim Text Editors In Linux

One of the most important tools for any developer or systems administrator on UNIX and Linux systems is a text editor. Mastering a text editor allows you to quickly create and edit the source code.

Talk Tech to Me, brought to you by CompTIAOne of the most important tools for any developer or systems administrator on UNIX and Linux systems is a text editor. Mastering a text editor allows you to quickly create and edit the source code for programs, scripts and configuration files. Nearly all configuration on a UNIX/Linux system is stored as text.

What Is Vi (and Vim)?

Vi stands for visual instrument, and the vi editor has been one of the most common — if not THE most common — text editors on Linux and UNIX systems since its creation in 1969 by Bill Joy. It was a visual extension to the 1969 ex line-based text editor used back when UNIX terminals looked like a printer with a keyboard. Then, you had to edit an existing text file using obscure commands, without seeing the actual file on your screen!

A photo of an old UNIX terminal that looks like a printer with a keyboard

In 1991, Bram Moolenaar created a version of the vi editor with extended functionality and called it vim (“vi improved”). Nearly all UNIX and Linux systems today use vim, but we still refer to it as vi. The vi executable is typically a shortcut to the vim executable.

Why Should I Learn Vi? Isn’t It Difficult to Use?

For people who open vi for the first time, it may seem difficult since it doesn’t present visual cues on the screen that tell you how to use it. 

Of course, this has led to a plethora of vi-related humor on the internet:

A screenshot of a tweet that says, "I've been using Vim for about 2 years now, mostly because I can't figure out how to exit it."

 

But vi isn’t difficult to use at all. It merely has a small learning curve at the beginning.

But learning vi is worth it because it offers a number of benefits:

  • Very fast for any purpose
  • Easily addictive, with a one-day learning curve (for the basics)
  • Powerful (800+ built-in functions with plugins and customization ability)
  • Found standard on nearly all UNIX flavors and Linux distributions
  • A “vi-able” skill for any UNIX/Linux systems administrator, user or developer

How Do I Learn Vi?

The quickest and easiest way to learn how to use the vi editor is to first focus on essential vi functionality (the sur“vi”val skills, if you will) using some sample text files. As you become more familiar with the survival skills, you can expand upon that functionality later on. 

You can run the following commands to obtain sample text files on your system and edit the letter file using the vi editor:

git clone https://github.com/jasoneckert/classfiles.git
cd classfiles
vi letter

 

The cursor is automatically placed at the beginning of the file. The easiest way to navigate the file is to use the arrow keys, or page up/page down keys.

You’ll know you’ve reached the end of the file when you see a tilde (~) at the bottom.

Before you edit or add text, you must first understand that vi has two modes:

  • Command mode
  • Insert mode

When you first open the vi editor, you are placed in Command mode, where every key and key combination on your keyboard represents a function (e.g., pressing the x key will delete the character your cursor is on).

A handful of these functions take you to Insert mode, where the keys on your keyboard are actually used to type and edit text (e.g., pressing the x key will insert the letter x in your document). 

To return back to Command mode after inserting or editing text, simply press Esc.

A diagram showing the commands that apply to both Command mode and Insert mode in vi

Common Functions That Allow You to Enter Insert Mode

a

Start appending text after current character

I

Start inserting text before current character

o

Open a new line underneath the cursor to insert text

A

Start appending text after current line

I

Start inserting text at the beginning of the current line

O

Open a new line above the cursor to insert text

c

Start inserting text on the character that you are on

r

Replace one character only

 

Common Functions Used in Command Mode

yy

Yank a line of text to the buffer

y3y or 3yy

Yank 3 lines of text to the buffer

y3w 3yw

Yank 3 words of text to the buffer

p

(Lower Case P) Paste the contents of the buffer below the current line

P

(Upper Case P) Paste the contents of the buffer above the current line

dd

Delete the current line

d3d or 3dd

Delete 3 lines

d5w 5dw

Delete 5 words

x

Delete the current character

3x

Delete 3 characters (starting with the current character)

J

Join lines (join line below you to current line)

u

Undo last change

[Ctrl]+g

Current line stats

:

Takes you to the interactive : prompt (calledex mode)

:wq

Save and quit

:w lala

Save as file lala

:q

Quit (if no changes were made)

:q!

Quit and throw away any changes

:set all

To see all vi environment parameters

:set number

To set auto line numbering

:set nonumber

To unset auto line numbering

ZZ

Save and quit (same as:wq)

 

Spend some time practicing the functions in Command mode within the letter file, including the functions that take you to Insert mode. 

  1. Insert and delete as much text as you want.
  2. Then, hold down the u key in Command mode to undo all of your changes and use :q to quit the editor. 
  3. Next, type vi small town (a joke file with a lot of typos) and use these same survival skills to fix all of the typos.
  4. This time, save your changes using :wq in Command mode.

How to Go from Vi Essentials to More Advanced Functions

There’s no shortage of things to do after mastering the survival skills. Here are some more useful functions to use once you have mastered the basics of vi.

Navigation in Command Mode (Beyond the Cursor Keys and Page Up/Page Down)

h j k l

Alternatives to the cursor keys for navigation

1G

Go to line 1

23G

Go to line 23

G

Go to the last line

^

Go to beginning of the current line

$

Go to end of the current line

d$

Delete from cursor to end of the current line

d^

Delete from cursor to the beginning of the current line

 

Any set options in ex mode can be made permanent for all users by editing the /usr/share/vim/vimrc file, or for just your user account by creating a .vimrc or .exrc file in your home directory (e.g., vi ~/.vimrc).

Here are a few common options to place in this file:

set number

Turn on line numbering

set ruler

Cursor position

set showmatch

Highlight brackets

set ts=4

Tabs that are equivalent to four spaces (the default is 8, which is too much)

syntax on

Displays programming language highlights (syntax)


Useful Functions In Command Mode

/Mother

Searches forMother(n= next occurrence,N= previous occurrence)

?Mother

The same search, but in the reverse direction (earlier lines)

~

Switch case for current letter

gUU

Turn entire line to uppercase

ddp

Swap current line with the next one

zf5j

Fold the next 5 lines (zo= open/expand,zc= close,zd= delete)

 

Useful Commands In Ex Mode (The : Prompt)

:% s/the/THE/g

Search for the and replace with THE in the whole file

:1,7 s/the/THE/g

Same as previous, but only on lines one through seven

:ab LR Linux Rocks

When you type LR in INSERT MODE, replace with Linux Rocks – this can be put into your .vimrc or .exrc file too!

:r proposal1

insert the contents of the proposal1 file under the current line

:r !date

insert the output of the date command under the current line

:help p

display help for functions that start with p

:help holy-grail

displays help for all ex mode commands

:e proposal1

edit a new file (proposal1) instead of current file

:split proposal1

edit proposal1in a new split screen (horizontal)

:vsplit proposal1

edit proposal1in a new split screen (vertical) – you can use[Ctrl]+ww to move between screens, [Ctrl]+w,_ to minimize current screen or[Ctrl]+w,= to restore current screen to original size

:tabe lala

create a new tab called lala

:tabs

show tabs

:tabn

move to next tab

:tabp

move to previous tab

:set rightleft

a fun prank (especially if you put it in someone else’s .vimrc)

Want More Vi Help?

Run the vimtutor command! This opens a lengthy interactive vi tutorial within vi itself.

If this command isn’t available on your Linux system, it’s because your Linux distribution installed the minimal vim package. Simply run dnf install vim or apt install vim (depending on your Linux distribution) to get the full vim package that includes the vim tutor!

CompTIA Linux+ validates the skills employers need in jobs such as technical support specialist, network administrator and systems administrator. Download the exam objectives to see what other Linux skills are needed for these IT jobs.

Jason W. Eckert has been a UNIX (and later Linux) user/developer/sysadmin for more than 30 years, He has taught UNIX and Linux topics, including the vi text editor, in the college space for more than 20 years. 

Email us at [email protected] for inquiries related to contributed articles, link building and other web content needs.

Read More from the CompTIA Blog

Leave a Comment