Skip to content

Latest commit

Β 

History

History
358 lines (278 loc) Β· 11.6 KB

File metadata and controls

358 lines (278 loc) Β· 11.6 KB

✏️ Chapter 08: Text Editors

Beginner Chapter 08


πŸ“‘ Table of Contents


Choosing a Text Editor

Editor Learning Curve Speed Best For
nano ⭐ Easy Medium Quick edits, beginners
vim ⭐⭐⭐⭐ Hard Fast Power users, servers
neovim ⭐⭐⭐⭐ Hard Fastest Modern vim experience
emacs ⭐⭐⭐⭐⭐ Very Hard Fast Everything (it's practically an OS)
VS Code ⭐⭐ Easy Medium GUI development

πŸ’‘ Recommendation: Learn nano for quick edits, and invest time in vim β€” it's available on every Linux server and will make you incredibly productive.


Nano β€” The Beginner-Friendly Editor

Nano is pre-installed on most Linux systems and shows keyboard shortcuts at the bottom.

Basic Usage

nano filename.txt                  # Open or create a file
nano +15 filename.txt              # Open at line 15
nano -l filename.txt               # Show line numbers
sudo nano /etc/hosts               # Edit system files (need root)

Nano Keyboard Shortcuts

The ^ symbol means Ctrl and M- means Alt.

Shortcut Action
Ctrl + O Save (Write Out)
Ctrl + X Exit
Ctrl + K Cut line
Ctrl + U Paste line
Ctrl + W Search
Ctrl + \\ Search & Replace
Ctrl + G Help
Ctrl + _ Go to line number
Alt + U Undo
Alt + E Redo
Ctrl + C Show current line number
Alt + A Start selecting text
Ctrl + ^ Start selecting (alternative)

Configure Nano

# Create/edit nano config
nano ~/.nanorc
# ~/.nanorc β€” Useful nano settings
set tabsize 4              # Set tab width to 4 spaces
set tabstospaces           # Convert tabs to spaces
set linenumbers            # Always show line numbers
set autoindent             # Auto-indent new lines
set mouse                  # Enable mouse support
set smooth                 # Smooth scrolling
set constantshow           # Always show cursor position
set softwrap               # Soft wrap long lines

# Syntax highlighting (usually enabled by default)
include "/usr/share/nano/*.nanorc"

Vim β€” The Power Editor

Vim (Vi IMproved) is the most popular terminal editor on servers. It has a steep learning curve but extraordinary power once mastered.

🏠 Analogy: Vim is like learning to touch-type. Slow and frustrating at first, but once you get it, you'll never go back.

Install Vim

sudo apt install vim               # Debian/Ubuntu
sudo dnf install vim-enhanced      # Fedora
sudo pacman -S vim                 # Arch

Understanding Vim Modes

Vim is a modal editor β€” keys do different things depending on which mode you're in:

                 β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
     i, a, o     β”‚   INSERT MODE    β”‚     Esc
  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Άβ”‚  Type text here  │──────────────┐
  β”‚              β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜               β”‚
  β”‚                                                 β–Ό
  β”‚              β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
  β”‚              β”‚  COMMAND MODE    β”‚         β”‚  NORMAL MODE   β”‚
  β”‚              β”‚  :w :q :wq :%s  │◀────────│  Navigate, editβ”‚
  β”‚              β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   :     β”‚  copy, delete  β”‚
  β”‚                                           β””β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
  β”‚                                               β”‚    v, V
  β”‚              β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”              β”‚
  β”‚              β”‚  VISUAL MODE     β”‚β—€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
  β”‚              β”‚  Select text     β”‚
  β”‚              β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
Mode Purpose Enter Leave
Normal Navigate, delete, copy Esc Press mode key
Insert Type text i, a, o Esc
Visual Select text v, V, Ctrl+v Esc
Command Run commands : Enter or Esc

Surviving Vim (First 5 Minutes)

1. Open:      vim myfile.txt
2. Type text: Press 'i' (enters Insert mode), type your text
3. Stop typing: Press 'Esc' (back to Normal mode)
4. Save:      Type ':w' and press Enter
5. Quit:      Type ':q' and press Enter
6. Save+Quit: Type ':wq' and press Enter
7. Quit without saving: Type ':q!' and press Enter

🚨 Stuck in Vim? Press Esc then type :q! and press Enter. This quits without saving.

Navigation (Normal Mode)

Character Movement:
h ← left    j ↓ down    k ↑ up    l β†’ right

Word Movement:
w β†’ next word start     b β†’ previous word start
e β†’ next word end       W β†’ next WORD (space-separated)

Line Movement:
0 β†’ start of line       $ β†’ end of line
^ β†’ first non-space     g_ β†’ last non-space

Screen Movement:
gg β†’ first line         G β†’ last line
5G β†’ go to line 5       Ctrl+d β†’ half page down
Ctrl+u β†’ half page up   H β†’ top of screen
M β†’ middle of screen    L β†’ bottom of screen

Search:
/pattern β†’ search forward    ?pattern β†’ search backward
n β†’ next match               N β†’ previous match
* β†’ search word under cursor

Editing (Normal Mode)

Insert Text:
i β†’ insert before cursor     a β†’ insert after cursor
I β†’ insert at line start     A β†’ insert at line end
o β†’ new line below           O β†’ new line above
s β†’ delete char + insert     S β†’ delete line + insert

Delete:
x β†’ delete character         X β†’ delete char before cursor
dd β†’ delete entire line      D β†’ delete to end of line
dw β†’ delete word             d$ β†’ delete to line end
d0 β†’ delete to line start    3dd β†’ delete 3 lines

Copy (Yank) & Paste:
yy β†’ copy line               yw β†’ copy word
y$ β†’ copy to end of line     p β†’ paste after cursor
P β†’ paste before cursor      3yy β†’ copy 3 lines

Undo & Redo:
u β†’ undo                     Ctrl+r β†’ redo

Change (delete + enter insert mode):
cw β†’ change word             cc β†’ change line
c$ β†’ change to line end      ci" β†’ change inside quotes

Command Mode Essentials

:w                          " Save
:q                          " Quit
:wq  or  :x  or  ZZ        " Save and quit
:q!                         " Quit without saving

:w newfile.txt              " Save as different file
:e otherfile.txt            " Open another file

:%s/old/new/g               " Replace all occurrences in file
:%s/old/new/gc              " Replace with confirmation
:s/old/new/g                " Replace in current line only

:set number                 " Show line numbers
:set nonumber               " Hide line numbers
:set paste                  " Paste mode (disable auto-indent)
:set nopaste                " Normal mode again

:! ls                       " Run external command
:r !date                    " Insert command output into file
:r filename                 " Insert contents of another file

:split file.txt             " Horizontal split
:vsplit file.txt            " Vertical split
Ctrl+w w                    " Switch between splits
Ctrl+w q                    " Close split

Visual Mode (Selecting Text)

v β†’ character-wise selection (like click+drag)
V β†’ line-wise selection (select entire lines)
Ctrl+v β†’ block selection (select a rectangle)

After selecting:
d β†’ delete selection        y β†’ copy selection
> β†’ indent right            < β†’ indent left
~ β†’ toggle case             U β†’ uppercase
u β†’ lowercase               : β†’ command on selection

Vim Configuration

# Create ~/.vimrc for permanent settings
vim ~/.vimrc
" ~/.vimrc β€” Essential settings
set number                  " Show line numbers
set relativenumber          " Relative line numbers
set tabstop=4               " Tab width
set shiftwidth=4            " Indent width
set expandtab               " Tabs β†’ spaces
set autoindent              " Auto-indent
set smartindent             " Smart indentation
set hlsearch                " Highlight search results
set incsearch               " Incremental search
set ignorecase              " Case-insensitive search
set smartcase               " ...unless uppercase used
set wildmenu                " Tab completion menu
set mouse=a                 " Enable mouse
set clipboard=unnamedplus   " Use system clipboard
set cursorline              " Highlight current line
syntax on                   " Syntax highlighting
set background=dark         " Dark background
colorscheme desert          " Color scheme
set showmatch               " Highlight matching brackets
set encoding=utf-8          " UTF-8 encoding
set scrolloff=8             " Keep 8 lines visible above/below cursor

Neovim β€” Modern Vim

Neovim is a refactored, modern Vim with better defaults, Lua scripting, and LSP support.

# Install
sudo apt install neovim                 # Debian/Ubuntu
sudo dnf install neovim                 # Fedora
sudo pacman -S neovim                   # Arch

# Run
nvim filename.txt

# Config location
# ~/.config/nvim/init.vim    (Vim script)
# ~/.config/nvim/init.lua    (Lua β€” recommended)

Key Differences from Vim

Feature Vim Neovim
Config file ~/.vimrc ~/.config/nvim/init.lua
Plugin system Vimscript Lua (faster)
Built-in LSP ❌ No βœ… Yes
Built-in terminal Basic Full
Async support Limited Full

Other Editors

Command-Line Editors

# Emacs
sudo apt install emacs
emacs filename.txt

# micro β€” modern terminal editor
sudo apt install micro
micro filename.txt

# Visual Studio Code (from terminal)
code filename.txt

Setting Your Default Editor

# Set default editor for the system
sudo update-alternatives --config editor

# Or set via environment variable
export EDITOR=vim
export VISUAL=vim
# Add to ~/.bashrc to make permanent
echo 'export EDITOR=vim' >> ~/.bashrc

πŸ‹οΈ Practice Exercises

  1. Nano: Create a file practice.txt with nano, add 5 lines, save and exit
  2. Vim Basics: Open vim, enter insert mode, type "Hello Vim", save and quit
  3. Vim Navigation: Open /etc/passwd in vim, navigate to line 10 using 10G
  4. Vim Editing: In vim, delete a line (dd), undo (u), copy a line (yy), paste (p)
  5. Vim Search: Open a large file in vim and search for a word using /word
  6. Vim Replace: Use :%s/old/new/g to replace text in a file
  7. Vim Config: Create a ~/.vimrc with at least 5 settings from above
  8. Editor Choice: Set your preferred editor as the system default

← Previous: Package Management Β· 🏠 Home Β· Next: Shell Scripting Fundamentals β†’