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 2)

Basic vi Usage (Part 2) - Images: Japanese

Hey there, everyone!

Continuing from last time — the absolute basics of editing, saving, and quitting in vi are now in good shape. This time, let's go over some features the author recommends knowing early on.

Start by opening vi with a new file using vi <new-filename>.

Type "This is a CentOS!" on the first line. Feel free to write something else — "I love programming!" or anything you like. Just keep it in English for now, since the default CentOS setup doesn't support Japanese input without additional configuration (we'll cover that later).

So, with this on screen:

This is a CentOS!
~
~
~
~

Press 'yy' — that's the y key twice in a row.

Make sure you're in command mode (press Escape or Control + [ first if you're not sure). Pressing yy in insert mode will just type "yy" as regular text.

After pressing yy, press 'p' once. The result:

This is a CentOS!
This is a CentOS!
~
~
~
~

The line was duplicated. yy copies the current line, and p pastes it. Simple and useful.

Let's look at this in more detail.

yy copies one line. To copy multiple lines at once, type the number of lines you want first, then press yy.

For example, with this text:

This is a CentOS!
This is a CentOS! 1
This is a CentOS! 2
This is a CentOS! 3
This is a CentOS! 4
This is a CentOS! 5

~
~
~
~

Place the cursor on the "This is a CentOS! 1" line and type 5yy. Then move the cursor to the blank line at the end and press p:

This is a CentOS!
This is a CentOS! 1
This is a CentOS! 2
This is a CentOS! 3
This is a CentOS! 4
This is a CentOS! 5

This is a CentOS! 1
This is a CentOS! 2
This is a CentOS! 3
This is a CentOS! 4
This is a CentOS! 5
~
~
~
~

Five lines copied and pasted in one shot.

yy operates at the line level. To copy by individual characters, use 'yl'. The usage is the same — one character if you just press yl, or prefix it with a number to copy multiple characters.

For example, place the cursor on the "C" in "CentOS! 2" in this text:

This is a CentOS!
This is a CentOS! 1
This is a CentOS! 2
This is a CentOS! 3
This is a CentOS! 4
This is a CentOS! 5
~
~
~
~

Type 9yl to copy 9 characters from the cursor. Then move to the end of that same line and press p:

This is a CentOS!
This is a CentOS! 1
This is a CentOS! 2CentOS! 2
This is a CentOS! 3
This is a CentOS! 4
This is a CentOS! 5
~
~
~
~

9 characters starting from "C" — "CentOS! 2" — were appended after the cursor position.

The paste buffer persists until you copy something new, so pressing p again will paste a second copy:

This is a CentOS!
This is a CentOS! 1
This is a CentOS! 2CentOS! 2CentOS! 2
This is a CentOS! 3
This is a CentOS! 4
This is a CentOS! 5
~
~
~
~

This behavior is consistent with copy-paste behavior on Windows and macOS.

Now — the p key comes in two flavors: lowercase p and uppercase P. The difference is where the paste is inserted.

Here's an example. Start with this text and place the cursor on the "This is a CentOS! 5" line, then press yy to copy it.

This is a CentOS!
This is a CentOS! 1
This is a CentOS! 2CentOS! 2CentOS! 2
This is a CentOS! 3
This is a CentOS! 4
This is a CentOS! 5
~
~
~
~

Now move the cursor to the "This is a CentOS! 3" line and press p:

This is a CentOS!
This is a CentOS! 1
This is a CentOS! 2CentOS! 2CentOS! 2
This is a CentOS! 3
This is a CentOS! 5
This is a CentOS! 4
This is a CentOS! 5
~
~
~
~

The copied line was inserted after line 3. Now go back to the "This is a CentOS! 3" line and press P (uppercase):

This is a CentOS!
This is a CentOS! 1
This is a CentOS! 2CentOS! 2CentOS! 2
This is a CentOS! 5
This is a CentOS! 3
This is a CentOS! 5
This is a CentOS! 4
This is a CentOS! 5
~
~
~
~

This time it was inserted before line 3. Lowercase p pastes after the cursor position; uppercase P pastes before it.

The same rule applies to character-level paste with yl. Starting with:

This is a CentOS!
This is a CentOS! 1
This is a CentOS! 2CentOS! 2CentOS! 2
This is a CentOS! 5
This is a CentOS! 3
This is a CentOS! 5
This is a CentOS! 4
This is a CentOS! 5
~
~
~
~

Place the cursor on a "C" somewhere and type 7yl to copy "CentOS!". Then place the cursor on the "1" in line 2 and press p:

This is a CentOS!
This is a CentOS! 1CentOS!
This is a CentOS! 2CentOS! 2CentOS! 2
This is a CentOS! 5
This is a CentOS! 3
This is a CentOS! 5
This is a CentOS! 4
This is a CentOS! 5
~
~
~
~

"CentOS!" was inserted after the "1". Now try with P:

This is a CentOS!
This is a CentOS! CentOS!1
This is a CentOS! 2CentOS! 2CentOS! 2
This is a CentOS! 5
This is a CentOS! 3
This is a CentOS! 5
This is a CentOS! 4
This is a CentOS! 5
~
~
~
~

Now "CentOS!" appears before the "1". The p vs P distinction applies consistently to both line-level and character-level operations.

Now, if copying and pasting exist, you might expect cutting to exist too — and you'd be right.

To cut lines, use 'dd'. To cut individual characters, use 'dl'. The prefix number syntax works the same way as with yy and yl. Prefix a count to cut multiple lines or characters at once.

Example: place the cursor on the "C" in "CentOS!1" here:

This is a CentOS!
This is a CentOS! CentOS!1
This is a CentOS! 2CentOS! 2CentOS! 2
This is a CentOS! 5
This is a CentOS! 3
This is a CentOS! 5
This is a CentOS! 4
This is a CentOS! 5
~
~
~
~

Type 7dl:

This is a CentOS!
This is a CentOS! 1
This is a CentOS! 2CentOS! 2CentOS! 2
This is a CentOS! 5
This is a CentOS! 3
This is a CentOS! 5
This is a CentOS! 4
This is a CentOS! 5
~
~
~
~

7 characters from "C" were cut, leaving only "1". Since it was a cut and not a delete, pressing p pastes it back:

This is a CentOS!
This is a CentOS! CentOS!1
This is a CentOS! 2CentOS! 2CentOS! 2
This is a CentOS! 5
This is a CentOS! 3
This is a CentOS! 5
This is a CentOS! 4
This is a CentOS! 5
~
~
~
~

Now try dd on the "This is a CentOS! 5" line above "This is a CentOS! 3":

This is a CentOS!
This is a CentOS! CentOS!1
This is a CentOS! 2CentOS! 2CentOS! 2
This is a CentOS! 3
This is a CentOS! 5
This is a CentOS! 4
This is a CentOS! 5
~
~
~
~

The line was removed. That's dd and dl in action.

A useful trick: you don't have to paste what you cut. If you just want to delete lines quickly, you can use dd repeatedly without ever pasting. For example, to delete everything after "This is a CentOS!", position the cursor on the next line and hammer dd:

This is a CentOS!
~
~
~
~

Much faster than going into insert mode and deleting character by character. Using dd and dl smartly makes editing significantly faster.

For deleting single characters, there's also 'x'. Press x once to delete the character under the cursor. Prefix a number to delete multiple characters — 5x deletes 5 characters, 7x deletes 7, and so on.

A note on terminology: in vi and Vim, the standard terms are different from what most people are used to. Copy is called yank, paste is called put, and cut is called delete. Official documentation and reference books use these terms, so it's worth knowing them. That said, recent resources often use "copy," "paste," and "cut" for clarity — so either set of terms will probably work in practice.

Also, calling it "delete" instead of "cut" is slightly imprecise: in vi and Vim, there's no separate delete operation — everything goes through the cut mechanism. So "cutting without pasting" is effectively how deletion works here.

The yank buffer in vi and Vim is not the system clipboard. Content yanked in vi cannot be pasted into another application directly.

The clipboard on Windows and macOS is a shared memory area accessible by all applications. Vi's yank buffer is separate from that — it only exists within the current vi session.

If you want to use yanked content in another application, search for something like "vi yank clipboard" and you'll find solutions. There are ways to bridge the two.

3yy can also be written as y3y, and 3dd as d3d. Both forms work — which one you use is entirely a matter of preference.

Here's something the author finds useful: after pressing y or d, you can follow it with any cursor movement command, and it applies the yank or delete to the range covered by that movement.

For example, with this text:

This is a CentOS!
This is a CentOS! 1
This is a CentOS! 2
This is a CentOS! 3
This is a CentOS! 4
This is a CentOS! 5
~
~
~

Place the cursor at the beginning of "This is a CentOS!" and type 3j — the cursor moves down 3 lines to "This is a CentOS! 3".

Now instead, with the cursor back at the beginning of line 1, type d3j:

This is a CentOS! 4
This is a CentOS! 5
~
~
~

The same number of lines that 3j would have moved across were deleted.

gg or 1G moves the cursor to the top of the file. You can combine that with d too. With the cursor somewhere in the middle:

This is a CentOS!
This is a CentOS! 1
This is a CentOS! 2
This is a CentOS! 3
This is a CentOS! 4
This is a CentOS! 5
~
~
~

Place the cursor on "This is a CentOS! 4" and type dgg:

This is a CentOS! 5
~
~
~

Everything from the cursor up to the top of the file is gone.

This is a powerful pattern that a lot of people don't know about. The author has seen experienced developers at client sites say "wait, you can do that?!" more than once. Cursor movement commands and text editing commands compose with each other — and once you internalize that, vi becomes dramatically more efficient. For beginners especially, learning cursor movement doubles as learning text editing.

Last up: 'u' and 'Control + r'. u undoes the last operation, and Control + r redoes it — the same as Control + z and Control + Shift + z on Windows or macOS.

For example, place the cursor on "This is a CentOS!" and press dd:

This is a CentOS! 1
This is a CentOS! 2
This is a CentOS! 3
This is a CentOS! 4
This is a CentOS! 5
~
~
~

Press u to undo:

This is a CentOS!
This is a CentOS! 1
This is a CentOS! 2
This is a CentOS! 3
This is a CentOS! 4
This is a CentOS! 5
~
~
~

Then press Control + r to redo:

This is a CentOS! 1
This is a CentOS! 2
This is a CentOS! 3
This is a CentOS! 4
This is a CentOS! 5
~
~
~

Works as expected.

That's the vi fundamentals covered. With what's been introduced in these articles, you should be able to handle most basic editing tasks. In the next article, we'll look at remote operation. 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 .