Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

vi Editor — Modes, Save, Run a Shell Script

Lesson 7 of 31 in the free Operating System & Linux Programming notes on Siksha Sarovar, written by Rohit Jangra.

Why vi?

vi (and its modern variant vim) is installed on essentially every Unix-like system. When you SSH into a stripped-down server with no GUI, vi is often your only editor. It is modal — keys behave differently in different modes — which feels alien at first but is extremely fast once internalized.

The Three Modes

  1. Command (Normal) mode — the default when vi opens. Keys are commands: dd deletes a line, yy copies, p pastes, /pat searches.
  2. Insert mode — entered with i, I, a, A, o or O. Now keys insert text. Press Esc to leave.
  3. Ex / Last-line mode — entered by typing : in command mode. Used for file operations: :w save, :q quit, :wq save & quit, :q! discard, :set number show line numbers.

Essential Command-mode Keys

KeyAction
h j k lleft, down, up, right
w / bnext / previous word
0 / $start / end of line
gg / Gfirst / last line
xdelete character
dddelete line
yyyank (copy) line
ppaste after cursor
uundo
Ctrl-rredo
/textsearch forward
nnext match

Writing a Shell Script in vi

  1. vi hello.sh — opens a new file in command mode.
  2. Press i to enter insert mode and type:

#!/bin/bash echo "Hello, $USER!" date

  1. Press Esc to return to command mode.
  2. Type :wq and press Enter to save and quit.
  3. Make it executable and run it:

chmod +x hello.sh ./hello.sh

Common Beginner Pitfalls

  • Typing into command mode and seeing nothing happen — you forgot i.
  • Stuck in vi — press Esc then :q! to quit without saving.
  • Garbled file — use u to undo. Vim allows many levels of undo.

Summary

  • vi has three modes: Command, Insert, and Ex.
  • Always return to command mode with Esc before issuing a save/quit command.
  • A shell script written in vi is just a text file with #!/bin/bash and execute permission.