Geekdosage
  • Home
  • Computer hardware
    • GPU
      • AMD
      • Nvidia
    • Processor
  • Linux
    • CentOS
    • Ubuntu
  • Docker
  • Computer Software
    • Operating System
      • Windows
      • MacOS
      • Android
  • Gaming
  • Computer Science
    • Network Engineering
    • Comparisons
  • About Us

Geekdosage

  • Home
  • Computer hardware
    • GPU
      • AMD
      • Nvidia
    • Processor
  • Linux
    • CentOS
    • Ubuntu
  • Docker
  • Computer Software
    • Operating System
      • Windows
      • MacOS
      • Android
  • Gaming
  • Computer Science
    • Network Engineering
    • Comparisons
  • About Us
CentOSLinuxUbuntu

How to Copy, Paste and Delete in Vim / Vi

by GeekDosage February 7, 2021
written by GeekDosage February 7, 2021
copy paste and delete in vim

Change content in a file using copying, pasting, and deleting is a regular task for Linux users. You can use Vim and Vi command-line tools for this kind of file manipulation tasks. In this tutorial, You will learn how to use the most common methods of using the vim/vi tool for copy, paste, and delete tasks.

Most Linux distributions offer the Vi tool out of the box. Moreover, you can install vim by running the following command.

UBUNTU

$ sudo apt-get install vim -y

CentOS/RHEL

$ sudo yum install vim -y

You can open a file using vim as follows.

$ vim file-name

By default, vim opens the file in normal mode. If you want to add content to a file using vim, then you have to press i to switch into INSERT mode. Press ESC to return to normal mode.

Switch to insert mode in Vim/ Vi

Vim/Vi – Copying (Yanking)

Copying in Vim/Vi tools calls yanking. We can use y for copying in different ways in the normal mode. Let’s see how.

y – Copy the current line.
yy – Copy the current line, including the newline character.
5yy – Copy five lines, starting from the line where the cursor is positioned.
y^ – Copy everything from the cursor to the start of the line.
y$ – Copy content begins from the cursor point to the end of the line.
yw – Copy to the start of the next word.

Vim/Vi – Copying in Visual mode

Visual mode allows the user to select text in multiple lines and change the file content.

v – Press to enter visual mode in Vim/Vi.

V (shift +v) – Press to enter visual line mode where the user can select text as entire lines.

Ctrl + v – Press to enter visual block mode. Which means the user can select content as a block.

You can use y key to copy what you have selected in visual mode.

Switch to visual mode in Vim/Vi editor

Vim/Vi – Pasting

After Copying the text in the file you can press p key for pasting. There are few options available for you.

But, if you were in visual mode, then you have to press ESC key to return to the normal mode before pasting.

Move the cursor to the desired location where you want to place it. Then press p to paste the copied content after the cursor. Press P(Shift + p) to paste the copied content before the cursor located line.

Vim/Vi – Deleting

You can use d key to delete the content of a file in normal mode.

dd – Delete the cursor locating line.

5dd – Delete five lines, starting from the cursor locating line,

d$ – Delete everything from the cursor location to the end of the line.

Vim/Vi – Search

press / after opening the file with vim/vi editor in normal mode. Now you can type any word or number you want to search within the file content. press n key for the jump to the next matching location.

Vim/Vi – Search and Replace

You can search for a text/number and replace it with the following command. This should be done while using normal mode.

:s/current-existing-word/replacing-word/gc

In the above command, s means substitute, g means globally substitution, and c for confirming each match before replacing it. Use I to case sensitive matches.

:s/current-existing-word/replacing-word/gIc

If you wish to provide a range for search and replace command, do as below command. This will search for matching locations only in between 20 to 30 lines.

:20,30s/current-existing-word/replacing-word/gc

Conclusion

In this tutorial, we have explained how to copy, paste, and delete in the vim/vi command-line tools. You can refer to Vim/Vi man pages for more details. Please discuss your doubts and suggestions in the comment section below.

Read More:

How to Copy Files Between Linux Servers Using rsync with examples
How to Kill A Process In Linux? Simple Guide
How to remove/delete user in Linux
How to rename a Directory in Linux?

GeekDosage
copy paste in vimcopy paste vimdelete in vimdelete line in vimedit files using vimsearch and replace in vimsearch and replace vimsearch in vimvi
0 comment
0
FacebookTwitterPinterest
previous post
How to Copy Files Between Linux Servers Using rsync with examples
next post
SCP Linux – Securely Copy Files in Linux Using SCP with examples

Related Posts

SCP Linux – Securely Copy Files in Linux...

February 22, 2021

How to Copy Files Between Linux Servers Using...

February 4, 2021

passwd: Module is unknown issue in Ubuntu

January 26, 2021

How to Kill A Process In Linux? Simple...

January 24, 2021

2 Simplest Methods to Take/Capture Desktop Screenshot in...

January 17, 2021

How to remove/delete user in Linux

January 11, 2021

How to rename a Directory in Linux?

January 5, 2021

How to Install VirtualBox in Ubuntu 20.04

January 3, 2021

How to Create a Private Docker Registry in...

December 23, 2020

How to Build a Docker Image from Dockerfile

December 14, 2020

Leave a Comment Cancel Reply

Save my name, email, and website in this browser for the next time I comment.

In This Article

  • 1 Vim/Vi – Copying (Yanking)
    • 1.1 Vim/Vi – Copying in Visual mode
  • 2 Vim/Vi – Pasting
  • 3 Vim/Vi – Deleting
  • 4 Vim/Vi – Search
  • 5 Vim/Vi – Search and Replace
  • 6 Conclusion
  • 7 Read More:

Keep in touch

Facebook Twitter Google + Instagram Pinterest Email

Popular Posts

  • How to change the forgotten root password in CentOS 8

  • How to Install Docker on CentOS 7

  • How to Build a Docker Image from Dockerfile

  • Best GPU stress test software to test your GPU

About Geekdosage

Geekdosage is a technology based website which mainly focuses on network engineering, computer science, automation, computer hardware and software and internet technologies. Our purpose is to create a platform where anyone can access the subject areas without barriers.

Recent Posts

  • SCP Linux – Securely Copy Files in Linux Using SCP with examples

    February 22, 2021
  • How to Copy, Paste and Delete in Vim / Vi

    February 7, 2021

Find us

Facebook Twitter Instagram Pinterest Email
  • About Us
  • Contact Us
  • Disclaimer
  • Terms and Conditions
  • Cookie Privacy policy
  • Privacy Policy
  • DCMA

@2021 - All Right Reserved | Designed and Developed by GeekDosage