Learn Text Editors in Linux
☰ In this chapter, you will learn
- vi Text editors
- vim Text editors
- Nano Text editors
When managing a VPS server, you'll often need to edit text files for tasks like configuring settings or inserting values.
In this tutorial, we'll show you how to use the default vi editor for editing text files, and we'll also cover installing other third-party editors like nano and gedit.
vi Text Editor
vi is a command-line text editor known for its power and versatility in editing text.
vi has two modes: command mode and insert mode.
Command mode Esc
is the default mode where you can perform actions on the file. You can switch to insert mode by pressing the i
button.
Insert mode i
lets you modify text in opened files. You can insert, update, or delete text. To return to command mode, press the Esc
button.
Hello world! How are you. ~ ~ ~ ~ ~ ~ ~ -- INSERT -- 2,12 All
Note
The vi editor is case sensitive. When in command mode, be mindful of the casing.
To make a new file or open an existing one, just type this command:
Syntax:
vi path_to_file/filename.txt
Example:
vi newfile.txt
If newfile.txt exists, it will open. If not, a new file with that name will be made.
To edit the file, press the i
button to enter Insert mode.
To save and exit, press Esc
button to jump into Command mode. Then type :wq
and press Enter. It will save the file and quit.
Some useful vi command to remember.
# | Command | Action |
---|---|---|
1 | :wq |
Save and quit |
2 | ZZ |
Save and quit |
3 | :q! |
Quit without saving. |
4 | :w |
Save |
5 | :q |
Quit |
6 | :w new_name |
Save as new_name |
7 | u |
Undo |
8 | Ctrl + r |
Redo |
9 | yy |
Copy the current line |
10 | p |
Paste |
More commands:
Command Mode Commands:
# | Command | Action |
---|---|---|
1 | i |
Enter insert mode (insert text before the cursor). |
2 | a |
Enter insert mode (insert text after the cursor). |
3 | o |
Open a new line below the current line and enter insert mode. |
4 | O |
Open a new line above the current line and enter insert mode. |
5 | dd |
Delete the current line. |
6 | yy |
Yank (copy) the current line. |
7 | p |
Paste the yanked or deleted text after the cursor. |
8 | u |
Undo the last change. |
9 | Ctrl + r |
Redo the last undone change. |
10 | :q |
Quit vi (exit). If there are unsaved changes, it won't exit. |
11 | :q! |
Quit vi without saving changes. |
12 | :w |
Write (save) the file. |
13 | :wq or ZZ |
Write (save) the file and quit vi. |
14 | :x or :wq |
Write (save) the file and quit vi. |
Navigation Commands:
# | Command | Action |
---|---|---|
1 | h |
Move the cursor left. |
2 | j |
Move the cursor down. |
3 | k |
Move the cursor up. |
4 | l |
Move the cursor right. |
5 | w |
Move the cursor to the beginning of the next word. |
6 | b |
Move the cursor to the beginning of the previous word. |
7 | ^ |
Move the cursor to the beginning of the current line. |
8 | $ |
Move the cursor to the end of the current line. |
9 | G |
Move the cursor to the end of the file. |
10 | 1G or gg |
Move the cursor to the beginning of the file. |
11 | :n |
Move the cursor to line number "n". |
vim Text Editor
vim stands for vi improved, meaning it's an enhanced version of the vi editor.
You can use vim just like vi, and all vi commands work perfectly here.
It also includes a tutorial, accessible by typing vimtutor
in the terminal.
======================================================= = Welcome to the VIM Tutor - Version 1.7 = ======================================================= Vim is a very powerful editor that has many commands, too many to explain in a tutor such as this. This tutor is designed to describe enough of the commands that you will be able to easily use Vim as an all-purpose editor. The approximate time required to complete the tutor is 25-30 minutes, depending upon how much time is spent with experimentation. ATTENTION: The commands in the lessons will modify the text. Make a copy of this file to practice on (if you started "vimtutor" this is already a copy). It is important to remember that this tutor is set up to teach by use. That means that you need to execute the commands to learn them properly. If you only read the text, you will forget the commands! Now, make sure that your Caps-Lock key is NOT depressed and press the j key enough times to move the cursor so that lesson 1.1 completely fills the screen. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Lesson 1.1: MOVING THE CURSOR ** To move the cursor, press the h,j,k,l keys as indicated. ** ^ k Hint: The h key is at the left and moves left.
nano Text Editor
nano is a simple and straightforward text editor with a user-friendly interface, making it popular among novice users. Unlike vi or vim, it lacks a command-line interface and instead offers simple options for editing.
GNU nano 4.8 newfile.txt
Hello world!
How are you.
^G Get Help
^O Write Out
^W Where Is
^K Cut Text
^X Exit
^R Read File
^\ Replace
^U Paste Text
To verify whether nano is installed, you can run the following command in the terminal:
nano --version
If nano is installed, this command will display the version information. If nano is not installed, you may receive an error message indicating that the command is not found.
Installation
In case of Debian/Ubuntu
sudo apt update
1sudo apt install nano
In case of CentOS/Fedora
sudo yum update
1sudo yum install nano
Create a New file or open existing file:
Syntaxnano path/filename.txt
Example
nano newfile.txt
nano commands:
# | Command | Action |
---|---|---|
1 | Ctrl + O |
Saving a File. |
2 | Ctrl + X |
Exit nano. Prompt to save the file. |
3 | Ctrl + A |
Move to the beginning of the current line. |
4 | Ctrl + E |
Move to the end of the current line. |
5 | Ctrl + K |
Cut the current line. |
6 | Ctrl + U |
Paste the cut text. |
7 | Ctrl + Shift + 6 |
Copy the current line. |
8 | Ctrl + Shift + V |
Paste the copied text. |
9 | Ctrl + W |
Search for text within the file. After searching, use Alt + W to find the next occurrence. |
Summary
In this chapter, you've discovered how to utilize the vi, vim, and Nano editors on the Linux operating system. We've outlined several useful commands within this chapter, and it's highly recommended to practice them thoroughly. Doing so will enable you to confidently create and edit files at your convenience in the future.