Copy or Cut and Paste in vi on linux tutorial

vi text editor is a command line text editor. Most computer users these days have become accustomed to the graphical user interfaces available and may be uncomfortable with vi. However, when setting up a server it would do us well to get comfortable with it.

As a system admin one of the most common uses of vi is to paste configuration settings into files. The source of your text for copy pasting can come from three places:

  1. The same file you are editing
  2. A file on your server
  3. A file outside your server (e.g. on the internet)

Setup

Let’s first set up some files. We will make a couple of test files in the home directory of the user as follows:

[ahmed@amayem ~]$ echo Hello > helloWorld.txt 
[ahmed@amayem ~]$ echo World >> helloWorld.txt 
[ahmed@amayem ~]$ cat helloWorld.txt 
Hello
World
[ahmed@amayem ~]$ echo I am well > answer.txt
[ahmed@amayem ~]$ echo Thank you. >> answer.txt
[ahmed@amayem ~]$ cat answer.txt 
I am well
Thank you.

The first line piped the word, “Hello”” into helloworld.txt, overwriting the file, if it existed, and making it if it didn’t. The second line appended the word, “World” to the file. We used cat in the end to confirm our files.

Cut/Copy and Paste from the same file

Choose whichever file you made earlier to use in this section. I wil work with answer.txt. Open it with vi:

[ahmed@amayem ~]$ vi answer.txt

Copy and paste one line

With the cursor at your desired line press yy. Press p wherever you like and the line will be pasted below the line you are on. The y stands for “yank”.

Cut and paste one line

With the cursor at your desired line press dd. Press p wherever you like and the line will be pasted below the line you are on. The d stands for “delete”.

Copy and paste multiple lines

With the cursor at your desired line press nyy, where n is the number of lines down you want to copy. So if you want to copy 2 lines, press 2yy. To paste press p and the number of lines copied will be pasted below the line you are on now.

Cut and paste multiple lines

With the cursor at your desired line press ndd, where n is the number of lines down you want to copy. So if you want to copy 2 lines, press 2dd. To paste press p and the number of lines copied will be pasted below the line you are on now.

Copy and paste one word in a line

Move the cursor to your desired word. It can be any part of the word and press yiw, which stands for yank inner word. Press p and your word will be pasted after the cursor.

Cut and paste one word in a line

Move the cursor to your desired word. It can be any part of the word and press diw, which stands for delete inner word. Press p and your word will be pasted after the cursor.

Copy and paste part of a word or line

Mover the cursor to your desired word. Press v, which will enter you into visual mode. Move your cursor around to highlight the words or parts of words you want to copy then press y. Press p and your selection will be pasted after the cursor.

Cut and paste part of a word or line

Mover the cursor to your desired word. Press v, which will enter you into visual mode. Move your cursor around to highlight the words or parts of words you want to cut then press d. Press p and your selection will be pasted after the cursor.

Some other commands

I think we get the pattern. Here are some other commands:

x   deletes/cuts the single character under the cursor
Nx  deletes/cuts N characters starting with the character under the cursor
dw  deletes/cuts the rest of the word the cursor is on starting with the character under the cursor
dNW deletes/cuts the N words starting with the character under the cursor
D   Delete the remainder of the line starting with the character under the cursor.

If you swicth d or x with y you get the same effect but with a copy instead of a cut. Note that Y copys the whole line not the remainder.

Cut/Copy and Paste from a file on the same server

There are many ways to do this. Here are two ways.

Clumsy way

Append the file you want to copy from to the file you want to copy to:

[ahmed@amayem ~]$ cat helloWorld.txt >> answer.txt

Make sure you put TWO arrows to append. If you put one you will overwrite the file. Then cut/copy and paste the parts you want in the same file as above. Afterwards delete the appended part by pressing dG with you cursor at the end of the original “copied to” file’s contents, which will delete the rest of the text down.

Clean way

Open the file you want to copy from. Copy the parts you want to as before. Open the other file from within the editor by issuing this command: e: /path/to/file. For example

e: /home/ahmed/answer.txt

Now when you press p it will paste.

Cut/Copy and Paste from somwhere outside the server

This will depend on you OS and the program you are sshing with. Copy the text you want, for example copy the following text:

This is copied text from the internet.

In your vi instance enter into insert mode using i and move your cursor to where you want to paste, then paste how you normally would on your computer, for example ctrl+v or cmd+v. Done!

References

  1. Basic vi commands
  2. bheeshmar‘s answer at stack overflow’s question
  3. Adam Batkin‘s answer at stack overflow’s question
  4. Tyler Eaves‘s and Ronnie Howwel‘s answer at stack overflow’s question.

Ahmed Amayem has written 90 articles

A Web Application Developer Entrepreneur.