Bird
Raised Fist0
Linux CLIscripting~10 mins

vim basics (insert, command, save, quit) in Linux CLI - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - vim basics (insert, command, save, quit)
Open vim
↓
Normal mode
↓
Press i
→Insert mode
↓
Type text
↓
Press ESC
→Back to Normal mode
↓
Type :w to save
↓
Type :q to quit
↓
Exit vim
This flow shows how to open vim, switch between normal and insert modes, save changes, and quit.
Execution Sample
Linux CLI
vim example.txt
i
Hello, Vim!
<Esc>
:w
:q
Open vim, enter insert mode, type text, exit insert mode, save file, then quit vim.
Execution Table
StepActionModeEffectOutput/Result
1Open vim with 'vim example.txt'Normalvim opens in normal modeScreen shows empty buffer or file content
2Press 'i'InsertSwitch to insert modeCursor allows typing text
3Type 'Hello, Vim!'InsertText inserted into bufferBuffer now contains 'Hello, Vim!'
4Press <Esc>NormalReturn to normal modeCursor stops inserting text
5Type ':w' and press EnterNormalSave fileFile 'example.txt' saved
6Type ':q' and press EnterNormalQuit vimvim closes, back to shell
💡 vim closes after ':q' command
Variable Tracker
VariableStartAfter Step 3After Step 5Final
ModeNormalInsertNormalExited
Buffer Content"""Hello, Vim!""Hello, Vim!""Hello, Vim!" saved to file
Key Moments - 3 Insights
Why can't I type text immediately after opening vim?
vim starts in normal mode (see Step 1). You must press 'i' to enter insert mode before typing text (Step 2).
What does pressing <Esc> do?
Pressing <Esc> switches vim from insert mode back to normal mode (Step 4), stopping text insertion.
How do I save changes without quitting vim?
Use ':w' command in normal mode (Step 5) to save without exiting.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what mode is vim in after Step 2?
ANormal mode
BVisual mode
CInsert mode
DCommand mode
💡 Hint
Check the 'Mode' column at Step 2 in the execution table.
At which step is the file saved?
AStep 4
BStep 5
CStep 3
DStep 6
💡 Hint
Look for the ':w' command in the Action column.
If you forget to press <Esc> before ':w', what happens?
Avim inserts ':w' as text
Bvim quits without saving
Cvim saves the file anyway
Dvim shows an error
💡 Hint
Recall that commands like ':w' only work in normal mode (see Step 4 and 5).
Concept Snapshot
vim basics:
- Open file: vim filename
- Insert mode: press 'i' to type
- Normal mode: press <Esc> to stop typing
- Save file: type ':w' in normal mode
- Quit vim: type ':q' in normal mode
Remember: Insert mode for typing, Normal mode for commands.
Full Transcript
This lesson shows how to use vim basics. First, open vim with a file name. vim starts in normal mode where you cannot type text directly. Press 'i' to enter insert mode and type your text. When done, press the Escape key to return to normal mode. To save your changes, type ':w' and press Enter. To quit vim, type ':q' and press Enter. If you forget to press Escape before commands, vim will treat them as text. This flow helps you edit files safely and efficiently.

Practice

(1/5)
1. What key do you press to switch from insert mode to command mode in Vim?
easy
A. Enter
B. Ctrl+C
C. Esc
D. Tab

Solution

  1. Step 1: Understand Vim modes

    Vim has two main modes: insert mode for typing and command mode for commands.
  2. Step 2: Identify the key to switch modes

    Pressing Esc exits insert mode and returns to command mode.
  3. Final Answer:

    Esc -> Option C
  4. Quick Check:

    Esc switches to command mode [OK]
Hint: Press Esc to leave insert mode quickly [OK]
Common Mistakes:
  • Pressing Enter instead of Esc
  • Using Ctrl+C which interrupts
  • Pressing Tab expecting mode change
2. Which of the following is the correct command to save changes in Vim without quitting?
easy
A. :x
B. :q
C. :wq
D. :w

Solution

  1. Step 1: Recall Vim save commands

    In Vim, :w writes (saves) the file without quitting.
  2. Step 2: Differentiate from quit commands

    :q quits, :wq saves and quits, :x also saves and quits but is less common.
  3. Final Answer:

    :w -> Option D
  4. Quick Check:

    :w saves without quitting [OK]
Hint: Use :w to save, :q to quit [OK]
Common Mistakes:
  • Using :q to save
  • Confusing :wq with just save
  • Using :x without knowing it quits
3. You open a file in Vim, press i to insert text, type "Hello", then press Esc and enter :wq. What happens?
medium
A. The text "Hello" is saved and Vim quits
B. The text "Hello" is discarded and Vim quits
C. Vim saves but stays open
D. Vim quits without saving

Solution

  1. Step 1: Insert and exit insert mode

    Pressing i enters insert mode, typing "Hello" adds text, pressing Esc returns to command mode.
  2. Step 2: Use :wq command

    :wq saves the file and quits Vim.
  3. Final Answer:

    The text "Hello" is saved and Vim quits -> Option A
  4. Quick Check:

    :wq saves and quits [OK]
Hint: Use :wq to save and exit Vim [OK]
Common Mistakes:
  • Forgetting to press Esc before :wq
  • Using :q without saving
  • Expecting :wq to only save
4. You typed text in Vim but accidentally typed :q and Vim shows an error "No write since last change". What should you do to quit without saving?
medium
A. :wq
B. :q!
C. :w
D. :exit

Solution

  1. Step 1: Understand the error message

    "No write since last change" means Vim won't quit because changes are unsaved.
  2. Step 2: Use force quit command

    Typing :q! forces Vim to quit without saving changes.
  3. Final Answer:

    :q! -> Option B
  4. Quick Check:

    :q! quits without saving [OK]
Hint: Use :q! to quit without saving changes [OK]
Common Mistakes:
  • Using :q which fails
  • Trying :w to save then quit
  • Typing :exit which is invalid
5. You want to quickly add the line "# TODO" at the start of a file in Vim, save, and quit. Which sequence of keys and commands is correct?
hard
A. Press I, type "# TODO", press Esc, then :wq
B. Press o, type "# TODO", press Esc, then :q
C. Press a, type "# TODO", press Esc, then :w
D. Press i, type "# TODO", press Esc, then :wq

Solution

  1. Step 1: Insert text at start of line

    Pressing I (capital i) enters insert mode at the start of the current line.
  2. Step 2: Type text, exit insert mode, save and quit

    Type "# TODO", press Esc to exit insert mode, then :wq to save and quit.
  3. Final Answer:

    Press I, type "# TODO", Esc, then :wq -> Option A
  4. Quick Check:

    I inserts at line start, :wq saves and quits [OK]
Hint: Use I to insert at line start, then :wq to save and quit [OK]
Common Mistakes:
  • Using i inserts before cursor, not line start
  • Using o inserts new line below
  • Using :q quits without saving
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard