1. Introduction
In this tutorial, we’ll look at a few of the editors we can use to modify or create a new file in Linux.
There are many command-line editors available in Linux distributions out of which most widely used editors are Vim, Nano, and Emacs. So let’s go over them one by one.
2. Vim
The most commonly used command-line text editor is the Vim editor. Most Linux distributions come with Vim pre-installed. In some cases where Vim is not pre-installed, we can use the following commands to install it:
sudo apt-get install vim # For Debian based systems / Ubuntu
sudo yum install vim # For CentOS / Fedora
To modify files in Linux with Vim editor, we can use the following command:
vi testFile
This will open an existing file (if there is one) or it will create a new file if there is no existing file with this name. Once we hit
Here, ~ (tilde) shows that this is an empty line. Now, let’s see how Vim works.
We can use the Vim editor in various different modes: command and insert are the most frequently used ones. Command mode lets us run administrative tasks, such as saving a file or exiting, whereas insert mode lets us insert actual text into the file.
Vim will start with command mode by default. We can go to insert mode by typing “i” and can move back to command mode by pressing “Esc”.
Let’s keep in mind that the Vim editor commands are case-sensitive. We need to be extra careful with the capitalization of a few characters in the command.
Let’s now go over a few basic commands on how to work with Vim.
2.1. File Navigation Commands
We can use keyboard arrow keys to move around the text as we would do in GUI editors. Along with those, Vim also provides special keyboard keys to move around the file:
Command
Description
k
Move the cursor to the line above the current line
j
Move the cursor to the line below the current line
h
Move cursor left by one character position
l
Move cursor right by one character position
2.2. File Editing Commands
Once we know how to navigate, we’ll want to know what commands will edit the file’s contents:
Command
Description
i
Add the content from the current cursor location
I
Add the content from the current cursor location at the beginning of the current line
o
Add the content as a new line just after the current line
x
Delete the character at the current cursor location
D
Delete text from current position till the end of the current line
dd
Delete the whole current line
2.3. Cut, Copy and Paste Within a File
Finally, we can do some copy/paste operations, too:
Command
Description
yy
Copy the current line
v
Select using arrow or Vim cursor movement keys for a copy
y
Copy the marked text
d
Cut the marked text
p
Paste the copied text after the cursor location
P
Paste the copied text before the cursor location
2.4. File Administration Tasks Commands
Once we are done editing the file, we need to remember to save our work:
Command
Description
:w
Save the content of the file
:q
Quit. If there are unsaved changes, Vim editor will give prompt to save changes
:wq or ZZ
Save the file and then quit
:w fileName
Save the file with a new name “fileName”. This is like “Save As” functionality in GUI text editors
3. Nano
Nano is probably the easiest to work with from our list as its interface is comparable to GUI-based text editors. Most Linux distributions come with Nano pre-installed. In some cases where Nano is not pre-installed, we can use the following commands based on their package management tool:
sudo apt-get install nano # For Debian based systems / Ubuntu
sudo yum install nano # For CentOS / Fedora
To modify files in Linux with a Nano editor, we need to use the following command:
nano testFile
This is how our screen will look if we don’t have any existing file with this name. If we open an existing file, the content of that file will be shown on the screen:
At the top is the Nano version number and name of the file. At the bottom is a shortcut bar that has the most frequently used commands. Here the ^ symbol means the <Ctrl> key so ^Y means <Ctrl+Y>.
Let’s keep in mind a few things when working with Nano:
- Nano doesn’t come with modes, so we can just start typing in the editor directly
- Nano commands aren’t case-sensitive, and as a result, Ctrl+o is the same as Ctrl+O and so on
Let’s go over a few basic commands on how to work with the Nano editor.
3.1. File Navigation Commands
We can use arrow keys to traverse through the file. Nano editor also provides special keyboard shortcuts for it:
Command
Description
Ctrl+P
Move the cursor to the line above the current line
Ctrl+N
Move the cursor to the line below the current line
Ctrl+B
Move cursor left by one character position
Ctrl+F
Move cursor right by one character position
Ctrl+Space
Move forward one word on the current line
3.2. Cut, Copy and Paste Within a File
We can also perform copy/paste operations:
Command
Description
Ctrl+6
Set the marker and traverse through the file which you want to cut/copy
Alt+6
Copy the marked text
Ctrl+k
To cut the marked text
Ctrl+u
To paste the marked text
3.3. File Administration Tasks Commands
After all the editing, we’ll want to save or discard our changes:
Command
Description
Ctrl+O
Save the file content. Nano will prompt for a filename
Ctrl+X
Quite
4. Emacs
Emacs is one of the oldest and most versatile text editors available in Linux and UNIX based systems. emacs has features and strengths like Vim. At the same time, it has easy-to-remember commands resembling Nano. To install emacs, we can use the following commands based on our Linux distribution:
sudo apt-get install emacs # For Debian based systems / Ubuntu
sudo yum install emacs # For CentOS / Fedora
To modify files in Linux with emacs editor, we can do:
emacs tempFile
The top section is the editing section which is also known as the main buffer. At the bottom is the status bar which shows details such as the name of the file and the current cursor location.
The last line is known as a mini buffer that emacs uses to interact with the user. Most of the commands in emacs start with either with the
Keep in mind that Emacs doesn’t come with modes. We can just start typing in the editor directly.
Let’s go over a few basic commands on how to work with Emacs.
4.1. File Navigation Commands
We can use keyboard arrow keys to navigate through the file or use shortcuts:
Command
Description
C-p
Move the cursor to the line above the current line
C-n
Move the cursor to the line below the current line
C-b
Move cursor left by one character position
C-f
Move cursor right by one character position
M-f
Move forward one word in the current line
M-b
Move backward one word in the current line
C-a
Move to the start of the line
C-e
Move to the end of the line
4.2. Cut, Copy and Paste Within a File
We can do some copy/paste operations as well:
Command
Description
C-Space
Set the marker and traverse through the file which you want to cut/copy
E-w
Copy the marked Text
C-w
To cut the marked text
C-y
To paste the marked text
4.3. File Administration Tasks Commands
And finally, let’s save our work:
Command
Description
C-x C-s
To save the content to the file
C-x C-c
To exit out of the emacs editor without saving changes that we made
5. Conclusion
In this article, we looked at Vim, Nano, and Emacs. Specifically, we saw their basic commands.