Complete Tutorial on File Editors: nano, vi, and vim

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 of vi 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

CommandDescription
CTRL + OSave the file
CTRL + XExit nano (asks to save if changes exist)
CTRL + GOpen help menu
CTRL + KCut current line
CTRL + UPaste cut text
CTRL + WSearch for text
CTRL + CShow cursor position
CTRL + AMove to the beginning of the line
CTRL + EMove to the end of the line

Example: Creating and Editing a File

  1. Open a file named example.txt: nano example.txt
  2. Type some text.
  3. Save the file (CTRL + O, then Enter).
  4. 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

ModeHow to EnterDescription
Command ModeDefault when vi startsUsed for navigation and commands
Insert ModePress i, a, oUsed for text editing
Last Line ModePress : in Command ModeUsed for saving, quitting, and other operations

Basic Navigation

CommandAction
hMove left
lMove right
kMove up
jMove down
0Move to beginning of line
$Move to end of line

Editing Text

CommandAction
iInsert before cursor
aAppend after cursor
oOpen a new line below
ddDelete current line
yyCopy current line
pPaste copied text

Saving and Exiting

CommandAction
:wSave file
:qQuit vi
:wq or ZZSave and quit
:q!Quit without saving

Example: Creating and Editing a File

  1. Open example.txt: vi example.txt
  2. Press i to enter Insert Mode and type some text.
  3. Press ESC to return to Command Mode.
  4. 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

CommandAction
GGo to last line
ggGo to first line
Ctrl + dScroll half-page down
Ctrl + uScroll half-page up

Advanced Editing

CommandAction
uUndo last change
Ctrl + rRedo last undone change
.Repeat last command
xDelete character
rReplace character

Search and Replace

CommandAction
/wordSearch for “word”
nFind next occurrence
:%s/old/new/gReplace “old” with “new” globally

Saving and Exiting

CommandAction
:wSave file
:qQuit vim
:wqSave and quit
:q!Quit without saving

Example: Creating and Editing a File

  1. Open example.txt: vim example.txt
  2. Press i to enter Insert Mode and type text.
  3. Press ESC, then :wq to save and exit.

Summary: Comparing nano, vi, and vim

Featurenanovivim
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.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *