Skip to main content

Working with Text Editors

1. Using nano Editor

nano is a simple, user-friendly command-line text editor available on most Linux distributions. It's ideal for editing configuration files or writing small scripts.

Command Description Example
nano filename.txt Open or create a file for editing nano config.json
Ctrl + O Write (save) the file
Ctrl + X Exit nano
Ctrl + K Cut current line
Ctrl + U Paste cut lines
Ctrl + W Search for text in the file
Ctrl + / Toggle line numbers

Example: Editing a Configuration File

$ nano /etc/nginx/nginx.conf
Note: nano is not installed by default on all systems, but it's typically available via package managers like apt or yum.

2. Using vim Editor

Command Description Example
:q Quit vim (only if no changes) :q
:q! Force quit without saving :q!
:w Write/save current file :w
:wq Write and quit :wq
:x Write and quit (same as :wq) :x
i Enter insert mode (before cursor) i
a Enter insert mode (after cursor) a
o Open new line below and enter insert mode o
O Open new line above and enter insert mode O
Esc Exit insert mode and return to normal mode Esc
dd Delete current line dd
yy Yank (copy) current line yy
p Paste after cursor p
u Undo last action u
Ctrl + r Redo last undone action Ctrl + r
/pattern Search for pattern in file /hello
n Go to next search result n
N Go to previous search result N
:%s/old/new/g Replace all occurrences of "old" with "new" :%s/old/new/g
:set number Toggle line numbers on :set number
:set nonumber Toggle line numbers off :set nonumber

Example: Basic vim workflow

vim example.txt
i
This is a test file.
Press Esc to exit insert mode.
:wq

Example: Search and replace in vim

vim file.txt
:/old
n
:%s/old/new/g
:wq
Note: Vim has three main modes: Normal, Insert, and Command-line. Most commands are executed in Normal mode.

3. Understanding vim Modes

Mode Description Key Features Common Commands
Normal Mode The default mode for navigating and executing commands. Fast navigation, command execution, no text input h, j, k, l, d, y
Insert Mode Used for inserting text into the document. Text input, editing capabilities i, a, o, O
Visual Mode Allows selection of text for operations like copy or delete. Text selection, block operations v, V, Ctrl+v
Command-line Mode Used for executing commands like saving, quitting, searching. Command execution, search, file operations :, /, ?

Example: Switching Between Modes

vim filename.txt
# Press 'i' to enter Insert Mode
# Press 'Esc' to return to Normal Mode
# Type ':q' and press Enter to quit

4. Navigating in vim

Command Description Notes
h Move cursor left Default binding for left arrow
j Move cursor down Default binding for down arrow
k Move cursor up Default binding for up arrow
l Move cursor right Default binding for right arrow
0 Move cursor to beginning of line Zero, not O
$ Move cursor to end of line Shift + 4
gg Move cursor to first line of file Go to beginning of file
G Move cursor to last line of file Go to end of file
Ctrl + b Move up one screen Page up
Ctrl + f Move down one screen Page down
Ctrl + u Move up half a screen Half page up
Ctrl + d Move down half a screen Half page down
w Move forward one word Start of next word
b Move backward one word Start of previous word
e Move to end of current word End of current word
Ctrl + o Jump to previous cursor position Go back in jump history
Ctrl + i Jump to next cursor position Go forward in jump history

Example: Moving to line 10

10G

Example: Moving to column 5

5l

Example: Moving to next word

w

5. Editing in vim

Command Description Example
:q Quit vim :q
:w Save current file :w
:wq Save and quit :wq
:q! Quit without saving :q!
:wq! Force save and quit :wq!
i Enter insert mode i
Esc Exit insert mode Esc
dd Delete current line dd
yy Copy current line yy
p Paste after cursor p
u Undo last action u
Ctrl+r Redo last undone action Ctrl+r
/pattern Search for pattern /hello
n Go to next search result n
N Go to previous search result N
:%s/old/new/g Replace all occurrences of "old" with "new" :%s/foo/bar/g
:set number Show line numbers :set number
:set nonumber Hide line numbers :set nonumber
:set hlsearch Highlight search results :set hlsearch
:set nohlsearch Stop highlighting search results :set nohlsearch
:set wrap Enable line wrapping :set wrap
:set nowrap Disable line wrapping :set nowrap
:set tabstop=4 Set tab width to 4 spaces :set tabstop=4
:set expandtab Use spaces instead of tabs :set expandtab
:set shiftwidth=4 Set indentation width to 4 spaces :set shiftwidth=4
:set softtabstop=4 Set soft tab width to 4 spaces :set softtabstop=4
:set fileformat=unix Set line endings to Unix style :set fileformat=unix
:set fileformat=dos Set line endings to DOS style :set fileformat=dos
:set fileformat=mac Set line endings to Mac style :set fileformat=mac

Example: Basic vim workflow

vim example.txt
i
This is a new line.
Esc
:wq

Example: Search and replace in vim

vim file.txt
:set hlsearch
/old_text
:n
:%s/old_text/new_text/g
:wq

Example: Setting up vim for Python development

vim ~/.vimrc
set number
set expandtab
set shiftwidth=4
set softtabstop=4
set tabstop=4
Note: Vim has three main modes: Normal, Insert, and Command-line. In Normal mode, you can navigate and execute commands. Press i to enter Insert mode for typing text, and press Esc to return to Normal mode.
Warning: Be careful when using :wq! or :q! as they force actions without saving or confirmation. Always ensure your changes are saved before quitting.

6. Searching and Replacing in vim

Command Description Example
/pattern Search forward for a pattern /hello
?pattern Search backward for a pattern ?world
n Move to next match n
N Move to previous match N
:%s/old/new/g Replace all occurrences of 'old' with 'new' :%s/foo/bar/g
:%s/old/new/gc Replace with confirmation for each match :%s/foo/bar/gc

Example: Replace all occurrences of a word

:%s/old_word/new_word/g

Example: Replace with confirmation

:%s/old_word/new_word/gc
Note: The g flag replaces all occurrences on a line, while c prompts for confirmation before each replacement.

7. Using vim Visual Mode

Example: Selecting text in visual mode

vim file.txt
Command Description Usage
v Start visual mode by character Press v to select characters in visual mode
V Start visual mode by line Press V to select entire lines
Ctrl+v Start visual mode by block Press Ctrl+v to select a rectangular block of text
o Switch to opposite end of selection After selecting text, press o to move cursor to the other end of selection
d Delete selected text After selecting, press d to delete the selection
y Yank (copy) selected text After selecting, press y to copy the selection
c Change selected text After selecting, press c to change the selection
Note: Visual mode in vim allows for precise text selection and manipulation. It supports character-level, line-level, and block-level selections.

Example: Using visual mode to change text

vim file.txt

Workflow: Using visual mode to change text

  1. Open file in vim: vim file.txt
  2. Move cursor to start of desired text
  3. Press v to enter visual mode
  4. Move cursor to end of desired text
  5. Press c to change the selected text
  6. Type new content and press Esc

8. Configuring vim

Option Values Notes
syntax on on Enables syntax highlighting
number on Shows line numbers
expandtab on Uses spaces instead of tabs
tabstop 4 Number of spaces for a tab character
shiftwidth 4 Number of spaces for indentation

Example: Basic vimrc configuration

set syntax=on
set number
set expandtab
set tabstop=4
set shiftwidth=4
Note: The vim configuration file is typically located at ~/.vimrc or ~/.config/nvim/init.vim for Neovim.

9. Using Multiple Buffers in vim

Command Description Usage
:e filename Open a new file in the current buffer :e /etc/passwd
:sp filename Split the window horizontally and open a file :sp /etc/hosts
:vsp filename Split the window vertically and open a file :vsp config.json
:bn Switch to the next buffer :bn
:bp Switch to the previous buffer :bp
:bd Delete the current buffer :bd
:ls List all open buffers :ls

Example: Managing Multiple Buffers

:sp /etc/hosts
:vsp /var/log/syslog
:bn
:bd
Note: Use :sp and :vsp to split windows for viewing multiple files simultaneously.

10. Using emacs Editor

Feature Description Usage
Basic Navigation Move cursor using arrow keys or h/j/k/l
h - left
j - down
k - up
l - right
File Operations Open, save, and close files
C-x C-f - Open file
C-x C-s - Save file
C-x C-c - Close Emacs
Editing Commands Basic editing functions
C-k - Kill line
C-y - Yank (paste)
M-w - Copy region

Example: Basic Emacs Workflow

emacs filename.txt
Note: Emacs is highly customizable with configuration files like .emacs or init.el.
Warning: Emacs keybindings can be unintuitive for new users; use C-h t to start the tutorial.

Was this article helpful?