Vim
Vim is a powerful command-line based text editor that has enhanced the functionalities of the old Unix Vi text editor. It is very popular and widely used text editors among system administrators and programmers. As the name suggests, VIM means “vi improved” as it is just an advanced version of the default Linux text editor. Vim supports automatic commands, digraph inputs (useful in programming), split and session screens, tabs and tagging, and does not contain a GUI. The only way to initiate it is to start it directly from the command line. The interface is user-unfriendly, while some commands are not intuitive.
The Vim editor in Linux environment can be installed by using the default package manager, as shown below:
$ sudo apt install vim-enhanced [On Debian, Ubuntu and Mint]
$ sudo dnf install vim-enhanced [On RHEL, CentOS and Fedora]
$ sudo pacman -S vim-enhanced [On Arch Linux and Manjaro]
$ sudo zypper install vim-enhanced [On OpenSuse]
Vim can be used for creating, editing, and saving documents from the command line. It operates in two different modes: - Command mode - Insert mode By default, VIM is started in command mode:
$ vim (name of the file)
or
$ vim (full path of the file)
For editing the file Vim must be in Insert mode and this can be achieved by pressing the letter “i“ on the keyboard (“I” stays for Insert). When the Insert mode is active, the mark-up ---INSERT---
is shown at the bottom of the terminal page.
After editing is finished, Vim should exit from Insert mode by pressing the escape (esc)
key. By this, Vim returns in Command mode, and the document can be saved.
Vim Editing commands:
Commands are executed in Command mode only. Please note that the Vim editor is case-sensitive. Check available options are with the command:
vim --help
More information: link
Moving inside the file
Moving inside the file is possible only in Command mode. Default keys for navigation are as follows:
- l - Move cursor right
- k - Move cursor up
- j - Move cursor down
- h - Move cursor left The arrow keys on the keyboard can be used for navigation as well.
Saving and closing the file
Saving changes and close the file can also only be done in Command mode:
- :wq - save the content and close the file
- Shift+zz - save the content and close the file
- :q - close th file without saving
- :w - save the content but keep the file open
Nano
Nano is another popular text editor also used in the UNIX operating systems. It comes packed with many additional functionalities, which make it very powerful and advanced text editor. Similar to Vim, Nano runs in command line interface only.
Nano is easy-to-use editor for both new and advanced Linux users. It enhances usability by providing customizable key bindings.
Nano can be installed in Linux environment by using the default package manager:
$ sudo apt install nano [On Debian, Ubuntu, and Mint]
$ sudo dnf install nano [On RHEL, CentOS and Fedora]
$ sudo pacman -S nano [On Arch Linux and Manjaro]
$ sudo zypper install nano [On OpenSuse]
The following command verifies successful installation and shows the version:
$ nano --version
Opening and Creating Files
Opening an existing file or creating a new one can be achieved by typing “nano” followed by the file name:
$ nano filename
This opens a new Nano window for editing the content.
There is a list of basic command shortcuts at the bottom of the window, which can be used inside Nano. All commands are prefixed with either ^
or M
character. The caret symbol (^)
represents the Ctrl
key. For example, the ^J
command means pressing Ctrl
and J
keys at the same time. The letter M represents the Alt key
.
A list of all commands can be retrieved by pressing Ctrl+g
.
Please note that file-read permissions are required to open a file.
Editing files
Unlike Vim, Nano is a modeless editor – meaning editing the text can be started immediately after opening the file.
The command Ctrl+_
moves the cursor to a specific line and character number. In this case, the menu on the bottom of the screen changes and the number(s) in the “Enter line number, column number:”
field should be entered, followed by pressing the Enter key.
Searching and replacing
Searching for a specific text can be done by pressing Ctrl+w
, typing the search term, and pressing the Enter key. The cursor will move to the first match, and pressing Alt+w
moves the cursor to the next match.
Search/Replace is triggered by pressing Ctrl+\
, so the search term and the replacement text should be entered. After this, the editor moves to the first match and asks for a permission for replacement. After hitting Y
or N
the cursor moves to the next match. Pressing A
will replace all matches.
Copying, cutting, and pasting
For selecting any text, the cursor should be moved to the beginning of the text and Alt+a
should be pressed. This will set a selection mark (selection start). Moving the cursor to the end of the text highlights the selection.
The selection can be cancelled by pressing Ctrl+6
or copied into the clipboard by pressing Alt+6
.
Ctrl+k
cuts the selected text. Cutting whole lines can be achieved by moving the cursor to the desired line and pressing Ctrl+k
. Multiple lines can be cut by pressing Ctrl+k
multiple times.
For pasting the selection, the cursor should be moved to the desired location and Ctrl+u
should be pressed.
Saving and exiting
The file can be saved by pressing Ctrl+o
. If the file does not exist, it will be created.
Exiting Nano is done by pressing Ctrl+x
. In case there are unsaved changes, the editor will ask whether they should be saved.
Please note that file-write permissions are required for saving the file. In case it is a newly created file, directory-write permissions are needed.