Editing Text Files

Chapter 8: Editing Text Files

Mastering the Vim Text Editor

  • Command to open Vim:
    user@host:~$ vim

Chapter Objectives

  • Create, view, and edit text files using the Vim editor.
  • Understand Vim's modal editing system.
  • Master essential navigation and editing commands.
  • Configure Vim for efficient workflow.

Why Learn Vim?

  • Text Storage in Linux: Configuration and data are stored in text-based files.
  • Standard Tool: Vim is the standard tool for editing these text files.
  • Universal Availability:
    • Installed by default on virtually all Linux/Unix systems.
  • Remote Access:
    • Works over SSH and in terminal-only environments.
  • POSIX Standard:
    • The vi command is specified in POSIX, ensuring portability.
  • Cross-Platform:
    • Skills learned in Vim transfer to macOS and other Unix-like systems.
  • Key Reason:
    • "The key reason to learn Vim is that it is almost always installed by default on a server."

Installing Vim

  • Types of Vim Installations:
    • vim-minimal:
    • Lightweight installation that includes core features only.
    • Uses the vi command:
      vi filename
    • vim-enhanced:
    • Full feature set, including an online help system and tutorial (vimtutor).
    • Uses the command:
      vim filename
    • Alias Behavior:
    • With vim-enhanced, a shell alias makes vi automatically run vim for regular users.
    • Use \vi to override the alias if needed.

Vim Operating Modes

  • Insert Mode:
    • Used for editing text.
    • Indicated by -- INSERT -- at the bottom of the screen:
  • Command Mode:
    • Default mode when Vim opens.
    • Used for navigation, cut/paste, and text modification:
    • Essential Commands:
      • h, j, k, l: Move cursor (←↓↑→)
      • u: Undo the most recent edit
      • x: Delete character under cursor
      • dd: Delete entire line
      • yy: Yank (copy) entire line
      • p: Put (paste) after cursor
      • Navigation Keys:
      • w / b: Next / previous word
      • 0 / $: Start / end of line
      • gg / G: Start / end of file
      • Ctrl+f: Page down
      • Ctrl+b: Page up
    • Tip: Press Esc multiple times to ensure you are in command mode.
  • Visual Mode:
    • Used for selecting and manipulating text in different ways:
    • Modes of Visual Selection:
      • v: Character Mode (select characters within text)
      • V: Line Mode (select entire lines)
      • Ctrl+V: Block Mode (select rectangular blocks)
  • Extended Command Mode:
    • Entered by pressing : in command mode for extended commands at the bottom of the screen:
    • :w: Write (save) file, remains in command mode.
    • :wq: Write and quit (save and exit Vim).
    • :q: Quit (exit if no changes).
    • :q!: Force quit (discard all changes).
    • More Useful Commands:
      • :set number: Show line numbers
      • :set nonumber: Hide line numbers
      • :%s/old/new/g: Find & replace.

Basic Vim Workflow

  1. Open file using:
    • vim filename
  2. Navigate to the desired position using keys (h, j, k, l or arrow keys).
  3. Enter insert mode by pressing:
    • i
  4. Make your edits by typing your content.
  5. Return to command mode by pressing:
    • Esc
  6. Save and exit using command:
    • :wq
  • Pro Tip: Run vimtutor in the terminal to practice these commands interactively!

Yank and Put (Copy & Paste)

  • Vim uses y (yank) for copy and p (put) for paste instead of Ctrl+C / Ctrl+V.
    • Commands:
    • yy: Yank (copy) entire line.
    • y$: Yank to end of line.
    • yw: Yank word.
    • p: Put (paste) after cursor.
    • P: Put (paste) before cursor.
    • dd: Delete line (also copies it).
  • Visual Mode Copy/Paste Workflow:
    1. Press v to enter visual mode.
    2. Use arrows to select text.
    3. Press y to yank selection.
    4. Move cursor to new location.
    5. Press p to put the text.
    • Note: Deleted text (using d, dd, x) is automatically copied and can be pasted using p.

Vim Configuration

  • Configuration Files:
    • /etc/vimrc: System-wide settings for all users.
    • ~/.vimrc: User-specific settings.
    • Example of ~/.vimrc settings:
      • " Set the tab to 2 spaces for YAML files
        autocmd FileType yaml setlocal ts=2
      • " Show line numbers
        set number
  • Useful Options:
    • set number: Show line numbers.
    • set tabstop=4: Set tab width to 4 spaces.
    • syntax on: Enable syntax highlighting.
    • set autoindent: Enable auto-indentation.

Chapter 8 Summary

  • Opening file in Vim:
    Command: vim filename
  • Insert Mode Operations:
    • i / Esc: Enter / exit insert mode.
  • File Operations:
    • :wq / :q!: Save + quit / quit without saving.
  • Editing Operations:
    • u / x / dd: Undo / delete character / delete line.
    • y / p: Yank (copy) / put (paste).
  • Visual Mode Operations:
    • v / V / ^V: Visual mode selections.
  • Practice:
    Use vimtutor to master these essential Vim commands!