Mastering Linux vi and Vim: Essential Commands & Shortcuts

Overview

Terminal-based text editing remains a core competency when administering Linux servers or managing cloud deployments. This hands-on tutorial walks through the foundational architecture of vi and vim, breaking down the operational mechanics of Normal mode, Insert mode, and Command-line mode. Through step-by-step shell exercises, you will master essential keystrokes for cursor navigation, file manipulation, pattern substitution, and clean exits.

Setting Up the Practice Environment and Initial Files

Create an isolated directory for practicing vi commands and switch into it by running mkdir -p ~/vi_practice && cd ~/vi_practice.

Create and enter vi practice directory

Verify whether the vim package is installed on your system, and automatically update repositories and install it if absent, using which vim || (apt-get update -qq && apt-get install -y vim).

Check or install vim package

Generate a mock configuration file to serve as our initial editing canvas by executing echo -e 'Server Configuration File\nVersion: 1.0.0\nEnvironment: Production' > config.txt.

Generate sample configuration file

Inspect the generated file’s size, access permissions, and timestamp metadata with ls -l config.txt.

Inspect file metadata with ls -l

Confirm that the payload was written accurately to disk by checking its content with cat config.txt.

Display file content using cat

Demystifying vi Modes and Basic Text Manipulation

Return to the designated practice workspace by executing cd ~/vi_practice.

Navigate to practice workspace

Create an initial text file for testing modal transitions and character insertion with echo -e 'Line 1: Initial Entry' > modes_demo.txt.

Initialize modes demo file

Run vim in a scriptable batch fashion to jump directly to the bottom line, append a new row in Insert mode, and save the buffer by executing vim -c 'normal! G' -c 'normal! oLine 2: Added in Insert Mode' -c 'wq' modes_demo.txt.
Under this invocation, uppercase G navigates to the end of the file within Normal mode, lowercase o opens a new row beneath while entering Insert mode to write text, and the :wq sequence writes changes to storage and quits the editor.

Append line using normal and insert mode commands in vim

Examine the updated file to verify that the appended line was written properly using cat modes_demo.txt.

Verify appended content with cat

Cursor Navigation, Cutting, and Pasting Lines

Ensure you are located in the active exercise folder by running cd ~/vi_practice.

Switch to practice directory

Populate a multiline dataset for practicing row-level rearrangement and deletion using echo -e 'Alpha\nBeta\nGamma\nDelta' > edit_demo.txt.

Create multiline file for editing practice

Execute an automated editing flow that cuts the leading row and deposits it at the end of the document via vim -c 'normal! 1G' -c 'normal! dd' -c 'normal! G' -c 'normal! p' -c 'wq' edit_demo.txt.
In this sequence, 1G targets the first row, dd cuts (deletes) that line into the register, G hops to the final row of the buffer, p pastes the cut content directly below the cursor, and :wq commits the modification.

Reorder rows using dd and p in vim

Check the resulting file to verify the updated line hierarchy using cat edit_demo.txt.
Notice that the original opening entry has been successfully relocated to the bottom of the document.

Examine rearranged lines with cat

Global Search, String Substitution, and Safe Exits

Reposition your shell session into the working directory with cd ~/vi_practice.

Switch working directory

Initialize a sample application properties file for search-and-replace testing by running echo -e 'PORT=8080\nHOST=localhost\nDEBUG=true' > app.conf.

Generate mock application configuration file

Replace every occurrence of localhost across the entire file with 127.0.0.1 and save the outcome using vim -c '%s/localhost/127.0.0.1/g' -c 'wq' app.conf.
Entering command-line mode via colon allows you to run :%s/old/new/g, where % indicates the whole buffer, s initiates string substitution, and the trailing g flag applies changes globally across all matching instances before :wq writes the file and terminates the editor.

Perform global string substitution in vim

Verify that the substitution took effect throughout the configuration file by executing cat app.conf.
The output confirms that HOST=localhost has been updated to HOST=127.0.0.1.

Validate substitution result with cat

Regularly practicing these foundational keystrokes and command combinations will help you develop the muscle memory needed to edit configurations and inspect files swiftly across any Linux terminal environment.