Text editors are essential tools in the Linux command-line environment for editing configuration files, writing scripts, and managing documents. Among the most widely used editors are:
nano
: A simple and beginner-friendly text editor.vi
: A classic text editor available on almost all UNIX-based systems.vim
: An improved version ofvi
with additional features.
This tutorial covers how to use each editor with practical examples.
1. nano
Editor
Introduction
nano
is a straightforward and easy-to-use text editor. It is commonly pre-installed on Linux systems.
Opening a File
To open a file in nano
, use:
nano filename
If the file does not exist, nano
creates a new one.
Basic Navigation and Editing
- Arrow keys: Move the cursor.
- Typing text: Works as expected, like in a simple text editor.
Common nano
Commands
Command | Description |
---|---|
CTRL + O | Save the file |
CTRL + X | Exit nano (asks to save if changes exist) |
CTRL + G | Open help menu |
CTRL + K | Cut current line |
CTRL + U | Paste cut text |
CTRL + W | Search for text |
CTRL + C | Show cursor position |
CTRL + A | Move to the beginning of the line |
CTRL + E | Move to the end of the line |
Example: Creating and Editing a File
- Open a file named
example.txt
:nano example.txt
- Type some text.
- Save the file (
CTRL + O
, thenEnter
). - Exit (
CTRL + X
).
2. vi
Editor
Introduction
vi
(Visual Editor) is a powerful text editor available on almost all UNIX and Linux systems. It operates in different modes:
- Command Mode: For executing commands.
- Insert Mode: For editing text.
- Last Line Mode: For saving, exiting, and performing other actions.
Opening a File
vi filename
If the file does not exist, vi
creates a new one.
Modes in vi
Mode | How to Enter | Description |
---|---|---|
Command Mode | Default when vi starts | Used for navigation and commands |
Insert Mode | Press i , a , o | Used for text editing |
Last Line Mode | Press : in Command Mode | Used for saving, quitting, and other operations |
Basic Navigation
Command | Action |
---|---|
h | Move left |
l | Move right |
k | Move up |
j | Move down |
0 | Move to beginning of line |
$ | Move to end of line |
Editing Text
Command | Action |
---|---|
i | Insert before cursor |
a | Append after cursor |
o | Open a new line below |
dd | Delete current line |
yy | Copy current line |
p | Paste copied text |
Saving and Exiting
Command | Action |
---|---|
:w | Save file |
:q | Quit vi |
:wq or ZZ | Save and quit |
:q! | Quit without saving |
Example: Creating and Editing a File
- Open
example.txt
:vi example.txt
- Press
i
to enter Insert Mode and type some text. - Press
ESC
to return to Command Mode. - Save and exit with
:wq
.
3. vim
Editor
Introduction
vim
(Vi IMproved) is an enhanced version of vi
with additional features like syntax highlighting, undo/redo, and plugins.
Installing vim
If vim
is not installed, install it using:
sudo apt install vim # Debian/Ubuntu
sudo yum install vim # CentOS/RHEL
sudo dnf install vim # Fedora
Opening a File
vim filename
Modes in vim
Similar to vi
, vim
has:
- Command Mode (default)
- Insert Mode (press
i
) - Last Line Mode (press
:
)
Advanced Navigation
Command | Action |
---|---|
G | Go to last line |
gg | Go to first line |
Ctrl + d | Scroll half-page down |
Ctrl + u | Scroll half-page up |
Advanced Editing
Command | Action |
---|---|
u | Undo last change |
Ctrl + r | Redo last undone change |
. | Repeat last command |
x | Delete character |
r | Replace character |
Search and Replace
Command | Action |
---|---|
/word | Search for “word” |
n | Find next occurrence |
:%s/old/new/g | Replace “old” with “new” globally |
Saving and Exiting
Command | Action |
---|---|
:w | Save file |
:q | Quit vim |
:wq | Save and quit |
:q! | Quit without saving |
Example: Creating and Editing a File
- Open
example.txt
:vim example.txt
- Press
i
to enter Insert Mode and type text. - Press
ESC
, then:wq
to save and exit.
Summary: Comparing nano
, vi
, and vim
Feature | nano | vi | vim |
---|---|---|---|
User-Friendly | ✅ Yes | ❌ No | ❌ No |
Available by Default | ✅ Yes | ✅ Yes | ❌ No (needs installation) |
Syntax Highlighting | ❌ No | ❌ No | ✅ Yes |
Undo/Redo | ❌ No | ❌ No | ✅ Yes |
Mouse Support | ✅ Yes | ❌ No | ✅ Yes |
Plugins and Customization | ❌ No | ❌ No | ✅ Yes |
Conclusion
- Use
nano
for quick and simple edits. - Use
vi
if you need a reliable editor available everywhere. - Use
vim
for advanced text editing with powerful features.