vim basics (insert, command, save, quit) in Linux CLI - Time & Space Complexity
When using vim, it's helpful to know how the time to perform actions grows as your file gets bigger.
We want to see how commands like insert, save, and quit behave as the file size changes.
Analyze the time complexity of these basic vim commands.
# Open a file in vim
vim filename.txt
# Insert text (in insert mode)
i
Hello, world!
# Save and quit
:wq
This snippet shows opening a file, inserting text, then saving and quitting vim.
Look at what repeats or takes time as file size grows.
- Primary operation: Inserting characters one by one in insert mode.
- How many times: Once per character typed.
- Save operation: Writing the whole file to disk.
- How many times: Once per save command.
Typing more characters means more insert operations, but each is simple and quick.
| Input Size (characters) | Approx. Operations |
|---|---|
| 10 | 10 insert actions |
| 100 | 100 insert actions |
| 1000 | 1000 insert actions |
Saving writes the entire file, so time grows with file size.
Quitting is quick and does not depend on file size.
Time Complexity: O(n)
This means the time to insert or save grows roughly in direct proportion to the number of characters in the file.
[X] Wrong: "Saving a file in vim always takes the same time no matter how big the file is."
[OK] Correct: Saving writes the whole file to disk, so bigger files take longer to save.
Understanding how basic commands scale helps you reason about performance in real tools and scripts.
"What if vim used incremental saving instead of writing the whole file at once? How would the time complexity change?"
Practice
Solution
Step 1: Understand Vim modes
Vim has two main modes: insert mode for typing and command mode for commands.Step 2: Identify the key to switch modes
PressingEscexits insert mode and returns to command mode.Final Answer:
Esc -> Option CQuick Check:
Esc switches to command mode [OK]
- Pressing Enter instead of Esc
- Using Ctrl+C which interrupts
- Pressing Tab expecting mode change
Solution
Step 1: Recall Vim save commands
In Vim,:wwrites (saves) the file without quitting.Step 2: Differentiate from quit commands
:qquits,:wqsaves and quits,:xalso saves and quits but is less common.Final Answer:
:w -> Option DQuick Check:
:w saves without quitting [OK]
- Using :q to save
- Confusing :wq with just save
- Using :x without knowing it quits
i to insert text, type "Hello", then press Esc and enter :wq. What happens?Solution
Step 1: Insert and exit insert mode
Pressingienters insert mode, typing "Hello" adds text, pressingEscreturns to command mode.Step 2: Use :wq command
:wqsaves the file and quits Vim.Final Answer:
The text "Hello" is saved and Vim quits -> Option AQuick Check:
:wq saves and quits [OK]
- Forgetting to press Esc before :wq
- Using :q without saving
- Expecting :wq to only save
:q and Vim shows an error "No write since last change". What should you do to quit without saving?Solution
Step 1: Understand the error message
"No write since last change" means Vim won't quit because changes are unsaved.Step 2: Use force quit command
Typing:q!forces Vim to quit without saving changes.Final Answer:
:q! -> Option BQuick Check:
:q! quits without saving [OK]
- Using :q which fails
- Trying :w to save then quit
- Typing :exit which is invalid
Solution
Step 1: Insert text at start of line
PressingI(capital i) enters insert mode at the start of the current line.Step 2: Type text, exit insert mode, save and quit
Type "# TODO", pressEscto exit insert mode, then:wqto save and quit.Final Answer:
Press I, type "# TODO", Esc, then :wq -> Option AQuick Check:
I inserts at line start, :wq saves and quits [OK]
- Using i inserts before cursor, not line start
- Using o inserts new line below
- Using :q quits without saving
Ready to prove it?
Pass the test and this pattern moves unknown
