Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

  1. Home
  2. UNIX(Linux)Beginner - Basic vi Usage (Part 1)

Basic vi Usage (Part 1) - Images: Japanese

Hey there, everyone! Let's keep going with vi and cover the basics of how to use it.

Fair warning upfront: this article covers only the bare minimum you need to get started with vi. The editor has an enormous feature set — just the commands the author uses on a daily basis could fill a thin book — so we're going to focus on the essentials and leave it there for now.

Just an aside — a dedicated "Vim (vi) beginner's series" may or may not happen someday. Possibly. Maybe.

One thing worth knowing before you dive in: the fastest way to get comfortable with vi-family editors is to build muscle memory, not to study. Think of it less like learning a programming concept and more like learning a sport or a musical instrument. Ask any experienced vi user and most will tell you the same thing — it's closer to a physical skill than an intellectual one. The feeling is a bit like knowing a game so well that your hands just move on their own. Keep that in mind as you practice.

Alright, go ahead and log in to CentOS as usual. The current directory doesn't matter much, but logging in as root will put you in /root by default, so let's work from there.

To launch vi, use the following command:

[root@localhost ~]# vi test.txt

Simple enough — vi <filename>. If the file doesn't exist, vi will create a new one. If it already exists, vi will open it for editing.

After pressing Enter, your screen will look something like this:


~
~
~
~
"test.txt" [New File]

The screen fills with ~ characters, and the filename appears at the bottom. This is vi running. Note that you are no longer at the shell prompt — you're inside the editor now, so shell commands like ls and cd won't work here.

Vi has a distinctive concept called 'modes'. There are three of them: 'command mode' (also called normal mode), 'insert mode' (edit mode), and 'ex mode'. When you first open vi, you're always in command mode.

In command mode, you issue commands to the editor — you cannot type text directly.

"So how do I type text?" Try pressing the 'i' key.

After pressing i, the bottom of the screen changes to:


~
~
~
~
-- INSERT --

The -- INSERT -- indicator means you're now in insert mode. In this mode, you can type text freely. Go ahead and type the following across multiple lines — we'll need the multi-line content for practice later:

Miku Hatsune

This is a CentOS!
~
~
~
~
~
~

When you're done typing, press the Escape key (or Control + [) to return to command mode. Mac users, note that this is the Control key, not command — mixing those up is a very common mistake.

After pressing Escape, the screen looks like:

Miku Hatsune

This is a CentOS!
~
~
~

The -- INSERT -- indicator is gone. You're back in command mode.

The key takeaway: vi has modes, and to edit text you need to be in insert mode.

Now let's look at cursor movement. While in command mode, the arrow keys will move your cursor — try pressing them.

Vi also provides an alternative set of cursor movement keys: h, j, k, l — that's the four keys right next to each other on a standard keyboard. Give them a try. h moves left, j moves down, k moves up, and l moves right.

Once you're used to them, these keys are far more comfortable than reaching for the arrow keys. Editor efficiency has a real impact on how fast you can work, so it's worth getting into the habit early.

One historical note: in older versions of vi, the arrow keys don't work for cursor movement at all. That's because keyboards of that era often didn't have dedicated arrow keys — including the keyboard that vi's creator, William Nelson Joy, used. You're unlikely to encounter old versions of vi in practice these days, but it's useful context to have.

You can combine a number with the cursor movement keys to move multiple positions at once.

For example, 5j moves the cursor down five lines, and 3l moves it three characters to the right.

This becomes very useful once you're comfortable with vi, so keep it in the back of your mind.

We used i to enter insert mode, but there are several other keys that also switch to insert mode. Let's go through them.

First up: 'a' and 'A'. Pressing a moves the cursor one position to the right and then enters insert mode. Pressing A jumps to the end of the line and enters insert mode. Then there's uppercase 'I' (capital i), which jumps to the beginning of the line before entering insert mode. Both A and I are highly practical in everyday use.

Next: 'o' and 'O'. Lowercase o inserts a blank line below the current line and enters insert mode. Uppercase O inserts a blank line above the current line. Both are useful in different situations.

Knowing these insert mode entry points will cover most situations you'll encounter. There are others, but these are the ones worth learning first.

One more thing: when you're in command mode, your keyboard input mode needs to be in half-width alphanumeric (ASCII) mode. If your keyboard is set to Japanese input mode, switch it to half-width before issuing commands.

Now let's talk about saving and quitting. To save or quit, you need to switch to ex mode. To enter ex mode from command mode, press :. The screen will look like this:

Miku Hatsune

This is a CentOS!
~
~
~
:

The : at the bottom signals that you're in ex mode. Now type 'w' and press Enter. You'll see something like "test.txt" 3L, 32C written. The file is saved.

Press : again, then type 'q' and press Enter. You'll return to the shell prompt. Vi is now closed.

Now reopen the file with vi test.txt and add some text — for example, a new line reading "This is not a pen!":

Miku Hatsune

This is a CentOS!
This is not a pen!
~
~

Now switch back to command mode and try typing :q to quit. You'll see this instead:

Miku Hatsune

This is a CentOS!
This is not a pen!
~
~
~
~
E37: No write since last change (add ! to override)

Vi refuses to quit because there are unsaved changes. This is by design — vi won't let you lose work accidentally.

If you want to quit without saving, use :q!. The exclamation mark forces the quit and discards changes.

Miku Hatsune

This is a CentOS!
This is not a pen!
~
~
~
~
:q!
[root@localhost ~]#

Back at the prompt. Reopen the file and you'll see:

Miku Hatsune

This is a CentOS!
~
~
~
:

"This is not a pen!" is gone — the change was discarded, as expected.

Finally, let's introduce ':wq' — the command that saves the file and exits vi in one step. It's essentially :w followed by :q combined. Extremely handy.

Let's try it: add "This is not a pen!" again,

Miku Hatsune

This is a CentOS!
This is not a pen!
~
~
~
~

then type :wq:

Miku Hatsune

This is a CentOS!
This is not a pen!
~
~
~
:wq

Press Enter, and you're back at the prompt. Reopen the file:

Miku Hatsune

This is a CentOS!
This is not a pen!
~
~
~
~

"This is not a pen!" is there. Saved successfully.

:wq is probably the vi command you'll use most often, so make sure it sticks.

A quick aside on the mode system: once you internalize it, it becomes a genuinely powerful way to edit text. That said, it's also the reason why vi is completely unusable without some prior knowledge — the same mechanism that makes it efficient is what makes it so impenetrable at first. Such is vi.

That's a good stopping point for now. We'll continue with vi in the next article. See you there!

This article was written by Sakurama.

Author's beloved small mammal

桜舞 春人 Sakurama Haruto

A Tokyo-based programmer who has been creating various content since the ISDN era, with a bit of concern about his hair. A true long sleeper who generally feels unwell without at least 10 hours of sleep. His dream is to live a life where he can sleep as much as he wants. Loves games, sports, and music. Please share some hair with him.

If you find any errors or copyright issues, please .