10 - Editing in the Terminal¶
What this session is¶
About 30 minutes. You'll learn nano (beginner-friendly terminal editor) and just enough vim to survive - because vim is the default editor on many systems and you'll eventually be dropped into it without warning.
nano: the friendly editor¶
That opens nano. You see the file's contents (empty for a new file) and a help bar at the bottom showing the key shortcuts.
Editing: just type. Arrow keys move the cursor. Backspace deletes.
The shortcuts (where ^ means "Ctrl"):
- ^O - write (save). Hit Enter to confirm filename.
- ^X - exit. Asks to save if there are unsaved changes.
- ^K - cut current line.
- ^U - paste.
- ^W - search. Type query, Enter.
- ^G - help.
That's it. nano is friendly because the help is always on-screen.
Use nano whenever you need to edit a file in the terminal and don't want to think about it.
vim: the unavoidable editor¶
vim is everywhere. Many systems set EDITOR=vim by default. git commit opens vim if you forget -m. Servers often only have vim installed. Learn just enough to survive.
Open a file:
You're now in normal mode. Pressing letters doesn't insert text - it runs commands. (This is why beginners panic.)
The four modes¶
Vim has modes. The basics:
- Normal mode (where you start) - navigate, run commands.
- Insert mode - actually type text.
- Visual mode - select text.
- Command-line mode - for commands like save, quit.
Survival commands¶
When you open vim, you're in normal mode. To type text, press i to enter insert mode. To go back to normal, press Esc.
Minimal vocabulary:
| Press | What happens |
|---|---|
i |
enter insert mode (insert before cursor) |
a |
insert mode, after cursor |
o |
new line below, enter insert mode |
Esc |
back to normal mode |
:w |
save (Enter to confirm) |
:q |
quit |
:wq |
save and quit |
:q! |
quit without saving (force) |
u |
undo |
Ctrl-r |
redo |
dd |
delete current line |
yy |
"yank" (copy) current line |
p |
paste after cursor |
/pattern |
search (then n for next, N for previous) |
gg |
go to top |
G |
go to bottom |
:N |
go to line N |
A minimal workflow¶
vim file.txt- open.- Press
i- now you're typing. - Type your content.
- Press
Esc- back to normal. - Type
:wq- save and quit.
If you're stuck and want to leave without changes: Esc, then :q!.
Why people love (or hate) vim¶
Vim's command language is composable. d3w deletes 3 words. 5dd deletes 5 lines. >} indents the next paragraph. Once you internalize it, you edit at near-thought speed. Getting there takes 2-4 weeks of daily use.
If you're going to spend time at the terminal, learn enough vim to be unfrustrated. vimtutor (a command - try it) is the official 30-minute interactive guide. Run it. Once.
Editor preference: EDITOR env var¶
Many commands (git commit, crontab -e, visudo) open whatever editor $EDITOR is set to.
Persistent: add export EDITOR=nano to ~/.bashrc or ~/.zshrc (the shell startup file - page 11 has a bit on these). New terminals will pick it up.
VS Code's code -w works too: export EDITOR='code -w'. The -w makes code wait for you to close the window before returning.
VS Code from the terminal¶
If you have VS Code installed, you can:
code file.txt # open file in VS Code
code . # open current directory
code -d a.txt b.txt # diff two files
(Setup: on macOS, install the code shell command via the VS Code Command Palette → "Shell Command: Install 'code' command in PATH".)
The hybrid workflow that many engineers use: terminal for navigation, building, running, debugging; VS Code (or another editor) for serious code editing; nano/vim for quick edits to one file.
Dotfiles: your shell's configuration¶
Your shell reads startup files from your home:
~/.bashrc- read for each interactive bash shell.~/.bash_profileor~/.profile- read for login shells.~/.zshrc- read for each zsh shell (the macOS default).
What goes in them: aliases, env vars, custom prompt, PATH additions.
A small .bashrc addition you might add:
# Custom prompt with current directory
PS1='\u@\h:\w$ '
# Useful aliases
alias ll='ls -la'
alias gs='git status'
alias gd='git diff'
# Add ~/bin to PATH for your own scripts
export PATH="$HOME/bin:$PATH"
After editing .bashrc, either open a new terminal or run source ~/.bashrc to apply changes.
People often manage these files as a git repo called "dotfiles" - version-controlled, sync across machines. A common first OSS contribution: improve someone's dotfiles repo (or your own).
Exercise¶
-
nano:
Type some content. Save with^O(Enter to confirm name), exit with^X. -
vim survival:
Pressi. Type a few lines. PressEsc. Type:wq. You just used vim. -
vimtutor (highly recommended):
30 minutes. Best vim intro ever made. Even if you'll never use vim daily, it makes you not-panic when you land in it. -
Set up your shell:
- Find your startup file (
~/.bashrcfor bash,~/.zshrcfor zsh). - Add an alias:
alias ll='ls -la'(or pick your own). - Add a PATH entry:
export PATH="$HOME/bin:$PATH"(create~/binif it doesn't exist). -
Open a new terminal. Run
ll. Confirm it's an alias forls -la. -
Bonus - fish: if you find bash awkward, try fish shell. Auto-suggestions, syntax highlighting out of the box. Different syntax for scripting though (
fishis its own shell language, not bash).
What you might wonder¶
"Should I learn vim or use VS Code?" Both. VS Code for serious editing; vim for "I'm SSH'd into a remote server and need to fix one line." Don't fight a vim-vs-emacs holy war; pick what helps.
"What about emacs?"
Powerful editor with a different model (everything's a keybinding involving Ctrl/Meta). Some people love it. Less common than vim for terminal-only work. Try M-x butterfly.
"What's the difference between ~/.bashrc and ~/.bash_profile?"
.bashrc is read for interactive non-login shells (new terminals after you're already logged in). .bash_profile is read for login shells (SSH, console login). Many setups source .bashrc from .bash_profile so the distinction doesn't matter.
"What about tmux/screen?"
Terminal multiplexers - run multiple shells in one terminal window, detach and reattach. Hugely useful for SSH sessions. Try tmux; the basic commands are tmux new -s name, Ctrl-b d to detach, tmux attach -t name to reattach.
Done¶
- Use nano comfortably.
- Survive vim:
i, type,Esc,:wq. - Know that
vimtutorexists (and ideally run it). - Set
EDITOR. - Add aliases and PATH entries to your shell config.