courses

Editing Files

You will edit files on machines with no GUI

All term you will edit configuration files directly on the course VM and on remote servers over SSH — an sshd_config, a firewall rule, a ~/.vimrc — where there is no graphical editor and you cannot just copy the file to your laptop. Being fluent in at least one terminal editor is a non-negotiable Linux survival skill, and it pairs with Shell and Other Basics (running them) and Technical Writing (the Markdown you’ll write lab notebooks in).

Three editors cover the vast majority of terminal editing on Linux: Vim (or its predecessor Vi) is modal and keyboard-driven; Nano is modeless and beginner-friendly; Emacs is a modeless, extensible editor with a Lisp runtime at its core. If you learn nothing else, learn enough vi to open, edit, save, and quit — because vi is on every Unix-like system you will ever touch.

Vim

Vim (Vi IMproved) is a highly configurable, modal text editor. “Modal” means keystrokes do different things depending on the current mode. Vim is present on virtually every Unix-like system — often as vi, which may be a symlink to vim or a minimal vi implementation.

Starting Vim

vim filename          # open or create a file
vim +42 filename      # open at line 42
vim +/pattern file    # open at first match of pattern
vi filename           # POSIX-standard alias (may be minimal vi)

Modes

Mode How to enter Purpose
Normal Esc (always returns here) Navigation and commands
Insert i, a, o, I, A, O Typing text
Visual v (char), V (line), Ctrl-v (block) Select text
Command-line : Save, quit, search/replace, settings
Replace R Overtype existing characters

Essential normal-mode commands

Navigation:

h j k l         left / down / up / right
w b e           next word start / prev word / word end
0 ^             beginning of line / first non-whitespace
$               end of line
gg G            top / bottom of file
42G or :42      go to line 42
Ctrl-f Ctrl-b   page down / page up
%               jump to matching bracket/paren/brace

Editing:

x               delete character under cursor
dd              delete (cut) current line
yy              yank (copy) current line
p P             paste after / before cursor
u               undo
Ctrl-r          redo
.               repeat last change
r<char>         replace character under cursor
cw              change word (enters insert mode)
cc              change entire line

Search:

/pattern        search forward
?pattern        search backward
n N             next / previous match
*               search for word under cursor

Command-line mode (:) essentials

:w              save
:q              quit (fails if unsaved changes)
:wq or :x       save and quit
:q!             quit without saving
:e filename     open another file
:set nu         show line numbers
:set nonu       hide line numbers
:set hlsearch   highlight search matches
:%s/old/new/g   replace all occurrences in file
:%s/old/new/gc  replace with confirmation
:r filename     insert file contents below cursor
:!cmd           run a shell command

Worked example — edit a config file safely

# Open the file
vim /etc/hosts

# In normal mode, navigate to the line you want to edit
# Press i to enter insert mode, make changes
# Press Esc to return to normal mode
# Type :wq to save and quit

# To search and replace all occurrences of 'localhost' with '127.0.0.1':
# (in normal mode, type:)
# :%s/localhost/127.0.0.1/gc

Worked example — edit multiple files

vim file1.txt file2.txt   # open both files
# :n   → next file
# :N   → previous file
# :args  → list open files

Vim configuration

User configuration lives in ~/.vimrc:

set number          " show line numbers
set hlsearch        " highlight search results
set tabstop=4       " tab width 4 spaces
set expandtab       " use spaces instead of tabs
set autoindent      " auto-indent new lines
syntax on           " syntax highlighting

Getting help inside Vim

:help           open main help
:help :w        help for the :w command
:help normal    help for normal mode
vimtutor        interactive tutorial (run from shell)

Nano

Nano is a simple, modeless terminal editor. All commands use Ctrl or Meta (Alt) key combinations, which are displayed at the bottom of the screen. It is ideal for quick edits and users unfamiliar with modal editors.

Starting Nano

nano filename           # open or create a file
nano +42 filename       # open at line 42
nano -l filename        # show line numbers
nano -w filename        # disable line wrapping
nano -B filename        # create a backup before editing

Essential key bindings

Nano displays its shortcuts at the bottom of the screen. ^ means Ctrl, M- means Meta (usually Alt).

File operations:

Key Action
Ctrl-O Save (Write Out)
Ctrl-X Exit (prompts to save if modified)
Ctrl-R Read (insert) another file

Navigation:

Key Action
Ctrl-A / Ctrl-E Beginning / end of line
Ctrl-Y / Ctrl-V Page up / page down
Ctrl-_ Go to specific line and column
Ctrl-W Search (Where is)
Ctrl-\ Search and replace

Editing:

Key Action
Ctrl-K Cut current line (into cut buffer)
Ctrl-U Paste (Uncut) from cut buffer
Alt-6 Copy current line
Ctrl-^ Start/extend text selection
Ctrl-Z Undo
Alt-E Redo

Worked example — quickly edit a file

# Open file
nano /etc/hosts

# Navigate with arrow keys
# Type to insert text
# Ctrl-W to search for 'localhost'
# Ctrl-\ to replace text
# Ctrl-O to save, Enter to confirm filename
# Ctrl-X to exit

Nano configuration

User configuration in ~/.nanorc:

set linenumbers
set tabsize 4
set autoindent
set softwrap
include "/usr/share/nano/*.nanorc"   # syntax highlighting

Emacs

Emacs is a modeless, extensible editor built around an Emacs Lisp interpreter. Unlike Vim, there are no modes to switch between — keys always insert text unless combined with Ctrl or Meta (Alt/Esc). Its extensibility makes it an IDE, mail client, file manager, and shell inside a single program, though for server-side editing you use only the core editor.

Install on Debian/Ubuntu/Kali:

sudo apt install emacs-nox   # terminal-only build (no GUI dependency)

Key notation

Notation Meaning
C-x Hold Ctrl, press x
M-x Hold Meta (Alt), press x; or press Esc then x
C-x C-f Two-key chord: Ctrl+x then Ctrl+f
C-g Cancel any in-progress command (universal escape)

Commands that need more input are entered in the minibuffer — the single-line prompt at the bottom of the screen.

Starting Emacs

emacs filename          # open in GUI if available, else terminal
emacs -nw filename      # force terminal (no window) mode
emacs -nw +42 filename  # open at line 42

File operations

Key Action
C-x C-f Open file (find-file; creates if absent)
C-x C-s Save current buffer
C-x C-w Save as (write to new filename)
C-x C-c Quit Emacs (prompts to save unsaved buffers)
C-x i Insert another file at point
Key Action
C-f / C-b Forward / backward one character
M-f / M-b Forward / backward one word
C-n / C-p Next / previous line
C-a / C-e Beginning / end of line
M-< / M-> Beginning / end of buffer
C-v / M-v Page down / page up
C-l Recenter screen on current line
M-g g Go to line number

Editing

Key Action
C-d Delete character forward
M-d Delete word forward
C-k Kill (cut) from point to end of line
C-Space Set mark (start of selection region)
C-w Kill (cut) region between mark and point
M-w Copy region (without killing)
C-y Yank (paste) most recently killed text
M-y Cycle through kill ring after C-y
C-/ Undo (repeat to keep undoing)
C-x h Select entire buffer

Search and replace

Key Action
C-s Incremental search forward (type to narrow)
C-r Incremental search backward
M-% Query replace: type old, Return, new, Return; y/n/! to confirm
C-M-s Incremental regexp search
C-M-% Query replace with regexp

During incremental search, press C-s again to jump to the next match; Enter or C-g to exit search.

Buffers and windows

Emacs keeps each open file in a buffer. Windows are panes within a single Emacs frame; there can be many windows showing different buffers simultaneously.

Key Action
C-x b Switch to another buffer (Tab to complete)
C-x k Kill (close) a buffer
C-x C-b List all open buffers
C-x 2 Split window horizontally (two stacked panes)
C-x 3 Split window vertically (two side-by-side panes)
C-x o Move point to the other window
C-x 0 Close current window (buffer stays open)
C-x 1 Close all windows except current

Getting help

Emacs has extensive built-in documentation reachable without leaving the editor.

Key Action
C-h k Describe what a key binding does
C-h f Describe a function by name
C-h v Describe a variable
C-h t Open the built-in tutorial
C-h r Open the Emacs manual
M-x apropos Search for commands matching a keyword

Worked example — edit a config file

emacs -nw /etc/hosts

# C-s to search for a hostname
# C-a to go to line start, then navigate to the field to edit
# Type new text (no mode switching needed)
# M-% to query-replace: old-hostname Return new-hostname Return y
# C-x C-s to save
# C-x C-c to quit

Emacs configuration

Configuration lives in ~/.emacs, ~/.emacs.d/init.el, or (modern) ~/.config/emacs/init.el:

;; Show line and column numbers in the mode line
(setq column-number-mode t)
(global-display-line-numbers-mode t)

;; Spaces instead of tabs, 4-wide
(setq-default indent-tabs-mode nil)
(setq-default tab-width 4)

;; Highlight matching parentheses
(show-paren-mode t)

;; Disable the startup splash screen
(setq inhibit-startup-message t)

Choosing between Vim, Nano, and Emacs

Scenario Recommended
Quick config file edit Nano
Remote server, unknown editor vi (universally available)
Teaching / beginner use Nano
Modal editing, fast navigation, scripting Vim
Long-term productivity, built-in extensibility Emacs
Lisp/Elisp development or customization Emacs

Key takeaways

References


Related course pages: Navigation Basics · Shell and Other Basics · Working with Files · Technical Writing

🛠️ Maintenance note: these editors and their core key bindings are decades-stable. The only likely drift is the Emacs init-file location (~/.emacs vs ~/.config/emacs/init.el) and default packages on the course VM — verify against the installed version each term.