Skip to content

Latest commit

 

History

History
148 lines (106 loc) · 5.32 KB

vim.md

File metadata and controls

148 lines (106 loc) · 5.32 KB

vi and vim Editor Overview

vi is a classic text editor in Unix-based systems, while vim is an enhanced version of vi, offering additional functionality like syntax highlighting, multiple undo levels, and plugins. Both editors operate in modes, which can be confusing for beginners but offer great flexibility once understood.


1. Normal Mode (Default Mode)

When you open a file in vi or vim, you are in Normal Mode. In this mode, you can navigate, delete, and manipulate text without actually editing the content.

Common Commands in Normal Mode

  • Navigation:

    • h: Move the cursor left
    • j: Move the cursor down
    • k: Move the cursor up
    • l: Move the cursor right
    • w: Jump to the start of the next word
    • b: Jump back to the start of the previous word
    • 0: Move to the beginning of the current line
    • $: Move to the end of the current line
    • gg: Go to the beginning of the file
    • G: Go to the end of the file
  • Editing and Deletion:

    • x: Delete the character under the cursor
    • dw: Delete the word starting from the cursor
    • dd: Delete the entire line
    • d$: Delete from the cursor to the end of the line
    • u: Undo the last change
    • Ctrl + r: Redo the last undone action
    • p: Paste the copied or deleted text after the cursor
    • yy: Yank (copy) the entire line
    • y: Yank selected text or portion
  • Search:

    • /pattern: Search for pattern in the file
    • n: Move to the next occurrence of the search result
    • N: Move to the previous occurrence of the search result

Normal Mode is the mode for quick edits and navigation. If you’re in any other mode, press Esc to return to Normal Mode.


2. Insert Mode

This mode allows you to insert or modify text. It’s similar to the text editing modes in modern editors like VS Code or Sublime.

Switching to Insert Mode

  • i: Insert before the cursor
  • a: Insert after the cursor
  • I: Insert at the beginning of the current line
  • A: Insert at the end of the current line
  • o: Open a new line below and enter Insert Mode
  • O: Open a new line above and enter Insert Mode

Exiting Insert Mode

  • Press Esc to return to Normal Mode.

3. Command Mode (Ex Mode)

Command Mode is used to perform tasks like saving the file, quitting vim, or performing complex search-and-replace operations. You enter Command Mode by typing : from Normal Mode.

Common Commands in Command Mode:

  • Saving and Quitting:

    • :w: Save the file
    • :q: Quit vim
    • :wq or :x: Save and quit
    • :q!: Quit without saving
  • File Operations:

    • :e filename: Open another file
    • :r filename: Read another file into the current file
  • Search and Replace:

    • :%s/old/new/g: Replace all occurrences of old with new in the file
    • :%s/old/new/gc: Replace all occurrences but ask for confirmation before each change
  • Line Numbers:

    • :set number: Show line numbers
    • :set nonumber: Hide line numbers

You can execute many more complex commands using Command Mode. To return to Normal Mode from Command Mode, press Esc.


4. Visual Mode

In Visual Mode, you can select text for copying, cutting, or other actions.

Types of Visual Mode:

  • v: Character-wise selection
  • V: Line-wise selection
  • Ctrl + v: Block selection (visual block mode)

Common Visual Mode Commands:

  • y: Yank (copy) selected text
  • d: Delete the selected text
  • >: Indent the selected text
  • <: Un-indent the selected text
  • p: Paste the copied text after the selection

To exit Visual Mode, press Esc.


5. Advanced vim Features

Multiple Undo Levels

Unlike vi, which only supports one undo, vim allows multiple undo actions:

  • u: Undo the last change
  • Ctrl + r: Redo an undone action

Split Windows

You can split your screen to work on multiple files or parts of a file simultaneously:

  • :split filename: Split the screen horizontally
  • :vsplit filename: Split the screen vertically
  • Ctrl + w, w: Switch between windows

Macros

You can record and replay macros in vim:

  • qa: Start recording into register a
  • Perform actions.
  • q: Stop recording
  • @a: Play back the macro in register a

Configuration (.vimrc)

To make vim behave the way you like, you can customize it through a configuration file, .vimrc. For example:

set number        # Show line numbers
set tabstop=4     # Set tab width to 4 spaces
set expandtab     # Use spaces instead of tabs
set mouse=a       # Enable mouse usage in all modes
syntax on         # Enable syntax highlighting

Conclusion

Learning vim can feel overwhelming at first because of its multiple modes and extensive command set. However, once you master the basic navigation and editing commands in Normal Mode, and you get comfortable switching to Insert, Command, and Visual Modes, your productivity in vim will increase significantly. Keep practicing these commands and consider customizing vim with .vimrc settings to suit your workflow.