DEV Community

Kitco's Today I Learned
Kitco's Today I Learned

Posted on

1

How to Use Vi Editor - Beginner's Guide

Day 11 - 22th Jan 2025

Quick Summary: Create file, edit and save the file

  1. vi <filename.txt > - if the file doesn't exist, this makes file with file name.

  2. i- enter insert mode.

  3. Write your contents in the file.

  4. esc

  5. :wq

  6. enter!


Most Important Concepts for Using vi

1. Modes in vi

  • Command Mode: Default mode when you open vi. You can navigate and execute commands here.
  • Insert Mode: Used for editing text. Enter this mode by pressing i.
  • Escape Mode: Press Esc to return to Command mode.

2. Basic Navigation

  • h: Move left
  • l: Move right
  • k: Move up
  • j: Move down
  • Ctrl + d: Scroll down
  • Ctrl + u: Scroll up

3. Editing

  • i: Insert before the cursor.
  • a: Append after the cursor.
  • o: Open a new line below the cursor.
  • dd: Delete the current line.
  • x: Delete the character under the cursor.
  • u: Undo the last action.
  • Ctrl + r: Redo the last undone action.

4. Saving and Exiting

  • :w: Save changes.
  • :q: Quit.
  • :wq: Save and quit.
  • :q!: Quit without saving.

5. Search

  • /text: Search forward for "text".
  • ?text: Search backward for "text".
  • n: Repeat the search in the same direction.
  • N: Repeat the search in the opposite direction.

6. Copy, Paste, and Cut

  • yy: Copy (yank) the current line.
  • p: Paste the copied text after the cursor.
  • dd: Cut the current line.

7. Replace Text

  • :s/old/new/: Replace the first occurrence of "old" with "new" on the current line.
  • :s/old/new/g: Replace all occurrences of "old" with "new" on the current line.
  • :%s/old/new/g: Replace all occurrences in the entire file.

Quick Tips

  • Always press Esc to ensure you're in Command mode before executing commands.
  • Practice basic navigation and saving before diving into advanced features.
  • To exit quickly without saving (if you're stuck), press Esc and type :q!.

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post