Chapter 8 Text Editing
Chapter 8 Text Editing
VIM
The name Vim is an acronym for Vi Improved. This editor is an
enhanced version of the Vi text.
Vim's common modes
The Vim editor has three modes that determine how the editor
functions: Normal (or Command), Insert, and GUI.
Normal mode
Normal mode allows you to give commands to the editor. Functions such as the following can be
found here:
:w to write/save.
:q to quit.
:w <filename.txt> to name a new file.
:q! to quit without saving the changes to the file
Press the Esc key to start the Normal mode and enter :(desired command) [Enter] to perform your
intended task. For example, if I was working in a new file and wanted to name it 'file.txt', I would
use the following:
INSERT MODE
If you have made it this far, you probably know what the Insert mode
does. However, for those who don't, if you press the I key
(lowercase i) once you will see the "INSERT" prompt at the
bottom of the screen, indicating that you can now edit or add text.
To exit this mode and return to Normal mode, press the Esc key once.
VI EDITOR
Text editing skills using the command line are one of the most
crucial skills in Linux.
In this section, you will learn how to use two popular text
editors in Linux: Vim and Nano.
It will save you time and make you more productive. Vim and
nano are safe choices as they are present on most Linux
distributions.
Introduction to
Vim
Vim is a popular text editing tool for the command line.
it is powerful, customizable, and fast. Here are some reasons why you should
consider learning Vim.
Some Linux utilities, for example editing cron jobs, work in the same
editing format as Vim.
Vim is suitable for all – beginners and advanced users. Vim supports
complex string searches, highlighting searches, and much more.
Through plugins, Vim provides extended capabilities to developers
and system admins that includes code completion, syntax
highlighting, file management, version control, and more.
Vim has two variations: Vim (vim) and Vim tiny (vi). Vim tiny is a
smaller version of Vim that lacks some features of Vim.
Mastering movement and command modes
How to start using vim
vim your-file.txt
your-file.txt can either be a new file or an existing file that you want to
edit.
In the early days of the CLI, the keyboards didn't have arrow keys. Hence,
navigation was done using the set of available keys, hjkl being one of them.
Being keyboard-centric, using hjkl keys can greatly speed up text editing
tasks.
Note: Although arrow keys would work totally fine, you can still experiment
with hjkl keys to navigate. Some people find this this way of navigation
efficient.
💡Tip: To remember the hjkl sequence, use this: hang back, jump
down, kick up, leap forward.
The three Vim modes
You need to know the 3 operating modes of Vim and how to switch between
them. Keystrokes behave differently in each command mode. The three
modes are as follows:
Command mode.
Edit mode.
Visual mode.
Command Mode. When you start Vim, you land in the command mode by
default. This mode allows you to access other modes.
⚠ To switch to other modes, you need to be present in the command mode
first
Edit Mode
Let's break it down into simple steps. Remember, use the below
combinations when in command mode.
Shift + V → Select multiple lines.
Ctrl + V → Block mode
V → Character mode
The visual mode comes in handy when you need to copy and paste
or edit lines in bulk.
Extended command mode.
h: Move left
j: Move down
k: Move up
l: Move right
0: Move to the beginning of the line
$: Move to the end of the line
gg: Move to the beginning of the file
G: Move to the end of the file
Ctrl+d: Move half-page down
Ctrl+u: Move half-page up
Editing