Working Code
Example 1: Editing a file with nano
nano is the most intuitive terminal editor. Keyboard shortcuts are always shown at the bottom of the screen.
nano hello.txt
The file opens and you can start typing immediately:
Hello, terminal editor!
This is line two.
Save and exit:
Ctrl+Othen Enter — save (Write Out)Ctrl+X— exit
The ^ at the bottom means Ctrl.
Example 2: Useful nano shortcuts
Open nano and practice these while editing:
| Shortcut | Action |
| -------- | --------------------- |
| Ctrl+O | Save |
| Ctrl+X | Exit |
| Ctrl+K | Cut current line |
| Ctrl+U | Paste |
| Ctrl+W | Search |
| Ctrl+G | Help |
| Ctrl+/ | Go to a specific line |
Example 3: Getting started with vim — how to exit first
Opening vim for the first time can be intimidating. The most important thing to learn first is how to quit.
vim test.txt
vim opens. You're in Normal mode.
How to exit:
:q+ Enter — quit (when no changes were made):q!+ Enter — force quit without saving:wq+ Enter — save and quit
Pressing Esc multiple times always returns you to Normal mode.
Try It Yourself
vim's Mode System
The core of vim is modes. This is what makes it different from other editors.
Normal mode -> i (insert) -> Insert mode
Insert mode -> Esc -> Normal mode
Normal mode -> v -> Visual mode
Visual mode -> Esc -> Normal mode
Normal mode -> : -> Command mode
vim always starts in Normal mode.
Basic editing workflow:
vim myfile.txt
i— enter Insert mode (-- INSERT --appears at the bottom)- Type your text
Esc— return to Normal mode:wq+ Enter — save and quit
Essential Normal Mode Commands
In Normal mode, keystrokes are commands. All cursor movement and editing happens via keyboard.
Movement
| Key | Action |
| ----- | --------------------------- |
| h | left |
| l | right |
| j | down |
| k | up |
| w | jump to next word start |
| b | jump to previous word start |
| 0 | jump to line start |
| $ | jump to line end |
| gg | jump to file start |
| G | jump to file end |
| :10 | jump to line 10 |
Editing
| Key | Action |
| -------- | --------------------------------- |
| i | Insert mode before cursor |
| a | Insert mode after cursor (append) |
| o | New line below + Insert mode |
| O | New line above + Insert mode |
| dd | Delete current line (cut) |
| yy | Copy current line (yank) |
| p | Paste |
| u | Undo |
| Ctrl+R | Redo |
| x | Delete character under cursor |
Search and Replace
| Key/Command | Action |
| --------------- | ---------------------- |
| /word | Search forward |
| ?word | Search backward |
| n | Next search result |
| N | Previous search result |
| :%s/old/new/g | Replace all |
Visual Mode: Block Selection
v -> character-wise selection
V -> line-wise selection
Ctrl+V -> rectangular block selection
After selecting:
d— deletey— copy>— indent<— unindent
Real-world Scenario: Editing git commit messages
Running git commit without -m opens vim:
git config --global core.editor vim # set vim as default editor
git commit # vim opens
Writing a commit message in vim:
i— Insert mode- Type the message (
feat: add new feature) Esc— Normal mode:wq— save and quit
If vim feels uncomfortable, switch to nano:
git config --global core.editor nano
"Why?" — Why You Need Terminal Editors
Servers don't have GUI editors like VS Code or Cursor. When you SSH into a server to edit config files or make quick changes, a terminal editor is essential.
Editor selection guide
| Scenario | Recommended Editor |
| ---------------------- | -------------------------- |
| Quick file edits | nano (easy and intuitive) |
| Frequent server work | vim (available everywhere) |
| Want powerful features | neovim (modern vim) |
| Already using VS Code | code filename (locally) |
Why learning vim is worth it:
- Pre-installed on virtually every Linux/Mac server
- Hands never leave the keyboard — fast
- Full editing without a mouse
- Highly customizable
Common Mistakes
Mistake 1: Text won't type in vim
When vim opens, you're in Normal mode. Keystrokes are interpreted as commands.
# Typing "hello" in Normal mode:
# h -> move left
# e -> move to end of word
# l -> move right
# l -> move right
# o -> new line + Insert mode
Always press i to enter Insert mode before typing text.
Mistake 2: Trying to save without pressing Esc first
# Typing :wq in Insert mode
# <- ":wq" gets inserted as text
# Correct sequence
Esc -> :wq -> Enter
Mistake 3: Opening multiple files by accident
# When you accidentally open multiple files
# :q to close the current file and move to next
vim file1.txt file2.txt
:n # next file
:N # previous file
Deep Dive
vim config file (~/.vimrc)
Customize vim's default behavior:
" ~/.vimrc
set number " show line numbers
set tabstop=2 " tab width 2 spaces
set expandtab " tabs as spaces
set autoindent " auto indent
set hlsearch " highlight search results
set incsearch " incremental search
syntax on " syntax highlighting
Create the file if it doesn't exist: vim ~/.vimrc
neovim: Modern vim
neovim is a modern fork of vim with better defaults and a richer plugin ecosystem:
brew install neovim
nvim filename
Usage is identical to vim. It's becoming increasingly popular among developers.
Using VS Code as a terminal editor
In local development, you can set VS Code as your terminal editor:
git config --global core.editor "code --wait"
VS Code opens for git commit messages and closes when you're done.
Note: this doesn't work on remote servers (SSH).
- Open
nano practice.txt, type 3 lines, save withCtrl+O, and exit withCtrl+X. - Verify the contents with
cat practice.txt. - Open
vim practice.txt, pressGto go to the end,oto add a new line, thenEsc->:wqto save. - In vim, try
ddto delete a line, thenuto undo. - Search with
/searchwordand pressnto jump to the next result.
Q1. What's the vim command to force quit without saving?
- A)
:exit - B)
:q - C)
:q! - D)
:quit!