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 โ†’

โšก