Post 5: Copy, Cut, and Paste Operations in Vi Editor


1. Why Copy/Cut/Paste is Important in Vi

In verification or RTL workflows, you frequently need to:

  • Duplicate portions of code (e.g., testbench sequences)
  • Move register blocks or module instantiations
  • Extract error logs into another file

With Vi, you can perform all this without using a mouse, making your workflow much faster — especially when working over SSH or remote servers.


2. Understanding Yank (Copy), Delete (Cut), and Put (Paste)

CommandOperationDescription
yyYank (copy) current line
nyyYank n lines
ddCut current line
nddCut n lines
pPaste text after cursor or line
PPaste text before cursor or line

💡 Tip:

  • The last deleted (d) or yanked (y) content is stored in a buffer.
  • You can reuse it until you overwrite it with a new yank or delete.

3. Copying (Yanking) Text

CommandExampleAction
yyCopy current line
3yyCopy next 3 lines
:5,10yCopy lines 5 to 10
ywCopy one word
y$Copy from cursor to end of line

Example:

To copy the next 10 lines from an RTL file:

10yy

Then paste them anywhere using p or P.


4. Cutting (Deleting) Text

Vi’s delete commands also cut text into the same buffer used by yank.

CommandExampleDescription
ddDelete (cut) current line
5ddDelete next 5 lines
dwDelete one word
d$Delete from cursor to end of line
:10,20dDelete lines 10 to 20

Example:

:50,60d

Deletes lines 50–60 — useful for removing extra debug prints from testbench logs.


5. Pasting (Putting) Text

CommandDescription
pPaste after cursor
PPaste before cursor

Example:

5yy

:100

P

Copies 5 lines, jumps to line 100, and pastes them before that line.


6. Copying Between Files

You can copy specific lines from one file and paste them into another without leaving Vi.

Example:

:5,20y a

:e other_file.v

“ap

This sequence:

  1. Yanks lines 5–20 into register a
  2. Opens another file
  3. Pastes the copied lines from register a

This is great for transferring common code (like signal declarations or parameters) between modules.


7. Move (Cut and Paste) Lines Between Locations

You can move a block of lines using delete + paste.

Example:

:100,120d

:500

P

Moves lines 100–120 to before line 500 — helpful for reorganizing RTL or constraint sections.


8. Copy Lines to an External File

You can write selected lines into another file using:

:10,30w newfile.v

This writes lines 10–30 into a new file named newfile.v.

To append instead:

:10,30w >> newfile.v

💡 Perfect for extracting meaningful log sections from large simulation output files.


9. Real-World Use Case for VLSI Engineers

Scenario:

You’re reviewing a testbench file and want to:

  1. Copy stimulus code block from lines 200–250
  2. Paste it in another test sequence file

Commands:

:200,250y

:e tb_sequence_2.sv

P

Done — block copied, pasted, and ready for modification.

No external editor needed.


Summary

Copy, Cut, and Paste operations in Vi are essential for:

  • Managing repetitive testbench code
  • Moving module declarations
  • Extracting logs efficiently

Once mastered, they replace the need for mouse-driven editing entirely — making Vi the fastest editing tool for Linux-based VLSI workflows.


Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top