Mastering Vim: Essential File Editing Commands for Linux

Installing Vim

To install the Vim editor on Ubuntu systems, use the apt package manager with the following command:

sudo apt install vim

Launching Vi/Vim

After installation, you can launch Vim using either the vi or vim command. Both share the same basic functionality:

vi  # If vim is installed, this command will automatically open the vim editor

vim # Directly opens the vim editor

You can also create or open files using these commands:

vi filename  # Opens file if it exists, creates it if it doesn't

vim filename  # Same functionality as vi

Vim's Three Operational Modes

  • Normal Mode: This mode allows you to navigate through text and search for content, but doesn't permit direct editing. Keyboard inputs in this mode are interpreted as commands for operations like copying and pasting. Vim starts in Normal mode by default.
  • Insert Mode: In this mode, Vim functions like a standard text editor where keyboard inputs are treated as text content.
  • Command-Line Mode: This mode supports commands for saving, exiting, replacing text, and accessing advanced Vim features.

Using Vim involves switching between these three modes. The transitions can be performed as follows:

  • From any mode, press "Esc" to enter Normal mode.
  • From Normal mode, keys like "a", "i", "o", "O", "r", "R" will switch to Insert mode.
  • From Normal mode, pressing ":" enters Command-Line mode.

Mode Transitions

Normal Mode to Insert Mode

Vim provides several shortcuts to enter Insert mode from Normal mode. Once in Insert mode, you can edit text normally using arrow keys for navigation and Enter for line breaks, similar to Windows Notepad.

a   # Begin editing after the current cursor position
A   # Begin editing at the end of the current line
i   # Begin editing at the current cursor position
I   # Begin editing at the start of the current line
o   # Open a new line below the current line and begin editing
O   # Open a new line above the current line and begin editing

Command-Line Mode Operations

From Command-Line mode, you can perform various text operations:

:   # Access settings or save operations
/   # Search downward through the file for a specific string
?   # Search upward through the file for a specific string

Essential vi/vim Commands

These commands are fundamental for efficient text editing in Vim:

n dd     # Delete n lines starting from the cursor position
n yy     # Copy n lines starting from the cursor position
p        # Paste the copied content after the cursor
u        # Undo the last operation
Ctrl + r # Redo the last undone operation
r        # Replace a single character
Shift + 6 # Move cursor to the beginning of the current line
Shift + 4 # Move cursor to the end of the current line
Shift + g # Move cursor to the last line of the document
gg       # Move cursor to the first line of the document
n Shift + g # Move cursor to the nth line of the document
d n Shift + g # Delete content from cursor to line n
y n Shift + g # Copy content from cursor to line n

Operations in Normal Mode

Press "Esc" from any mode to enter Normal mode. The following commands are commonly used in Normal mode for tasks like copying, pasting, deleting, and searching/replacing text:

:w          # Save the current file
:q          # Exit the editor
:wq         # Save and exit
:q!         # Force exit without saving
:w newfile  # Save the file with a new name
:set nu     # Display line numbers
:set nonu   # Hide line numbers
:set hlsearch # Highlight search results
:set nohlsearch # Remove search highlighting
:n,m s/old/new # Replace the first occurrence of 'old' with 'new' on each line from n to m
:n,m s/old/new/g # Replace all occurrences of 'old' with 'new' on each line from n to m
/string     # Search downward for 'string'
?string     # Search upward for 'string'

Tags: Vim Linux Text Editor Command Line file editing

Posted on Tue, 03 Nov 2026 16:20:21 +0000 by redsox8185