How to edit a text file in my terminal
Categories:
Mastering Terminal Text Editing: A Guide to Vi, Vim, Nano, and Emacs

Learn how to efficiently edit text files directly within your Linux terminal using powerful command-line editors like Vi/Vim, Nano, and Emacs. This guide covers basic operations, common commands, and best practices for each editor.
Editing text files is a fundamental task for anyone working in a Linux or Unix-like environment. Whether you're configuring system files, writing scripts, or just jotting down notes, knowing how to manipulate text directly from the terminal can significantly boost your productivity. This article will introduce you to the most popular command-line text editors, guiding you through their basic usage and helping you choose the right tool for your needs.
Why Use a Terminal Text Editor?
While graphical text editors are convenient, terminal-based editors offer several advantages, especially in server environments, remote SSH sessions, or when system resources are limited. They are lightweight, fast, and often pre-installed on most Linux distributions. Mastering them provides a powerful skill set for system administration, development, and general command-line proficiency.
flowchart TD A[Start Terminal Session] --> B{Need to Edit File?} B -- Yes --> C{Which Editor to Use?} C -- Simple/Beginner --> D[Nano] C -- Powerful/Advanced --> E[Vi/Vim] C -- Feature-rich/Extensible --> F[Emacs] D --> G[Edit File] E --> G F --> G G --> H[Save & Exit] H --> I[Continue Work]
Decision flow for choosing a terminal text editor
Nano: The Beginner-Friendly Editor
Nano is often recommended for beginners due to its straightforward interface and on-screen help. It's a simple, modeless editor, meaning you can type directly into the file without switching between different modes. It's perfect for quick edits and users who prefer a more intuitive experience.
nano myfile.txt
Opening a file with Nano
Once inside Nano, you'll see a list of common commands at the bottom of the screen, typically using Ctrl
key combinations. For example, Ctrl+X
exits Nano, Ctrl+O
writes (saves) the current file, and Ctrl+W
searches for text.
Ctrl+G
for a more comprehensive help menu.Vi/Vim: The Powerful and Ubiquitous Editor
Vi (and its improved version, Vim - Vi IMproved) is a highly efficient and powerful text editor that comes pre-installed on almost all Unix-like systems. It has a steep learning curve due to its modal editing approach, but once mastered, it allows for incredibly fast and precise text manipulation without ever touching the mouse. Vim operates in different modes: Normal mode (for navigation and commands), Insert mode (for typing text), and Visual mode (for selecting text).
vim myotherfile.conf
Opening a file with Vim
Upon opening a file with Vim, you are in Normal mode. To start typing, you must enter Insert mode by pressing i
(insert at cursor), a
(append after cursor), or o
(open new line below). To return to Normal mode from Insert mode, press the Esc
key. Saving and exiting are done from Normal mode using commands like :w
(write/save), :q
(quit), or :wq
(write and quit).
i
to start typing and Esc
to return to command mode.Emacs: The Extensible Editor
Emacs is another highly powerful and extensible text editor, often considered an operating system within an operating system due to its vast capabilities. It's known for its extensive customization options, built-in Lisp interpreter, and a wide array of features including file management, email client, and even games. Like Vim, Emacs has a learning curve, but it offers unparalleled flexibility for those willing to invest the time.
emacs yetanotherfile.sh
Opening a file with Emacs
Emacs uses Ctrl
and Alt
(or Meta
) key combinations for most commands. For instance, Ctrl+X Ctrl+S
saves the file, and Ctrl+X Ctrl+C
exits Emacs. The Ctrl+H
key followed by t
will open a tutorial, which is highly recommended for new users.
1. Choose Your Editor
Decide which editor best suits your needs: Nano for simplicity, Vim for power and efficiency, or Emacs for extensibility.
2. Open a File
Use the command nano <filename>
, vim <filename>
, or emacs <filename>
to open the desired text file.
3. Edit the Content
For Nano, just start typing. For Vim, press i
to enter Insert mode. For Emacs, use Ctrl
and Alt
key combinations.
4. Save Your Changes
In Nano, press Ctrl+O
. In Vim, press Esc
then :w
. In Emacs, press Ctrl+X Ctrl+S
.
5. Exit the Editor
In Nano, press Ctrl+X
. In Vim, press Esc
then :q
(or :wq
to save and quit). In Emacs, press Ctrl+X Ctrl+C
.