Vim text editor is one of the oldest and best text editor for Linux. It is a greatly improved version of a good old Vi Editor. This text editor is packed with many great features like command line history, syntax highlighting, multi-level undo, spelling checker, block operations, and filename etc. In this article, we will learn how to install Vim on CentOS.
Key Features of Vim Text Editor
- It is a cross-platform editor which is available for Linux, Windows and Mac operating systems.
- It can be accessed from both command line interface (CLI) and Graphical User Interface (GUI).
- It is a high-level text editor suitable for editing Markup Languages.
How to Install Vim on CentOS
You need to run the command below to install Vim on CentOS:
sudo yum install vim
Once you hit enter, it will prompt with [y/d/N]
. You need to press y to continue the installation. It will install vim and all the dependencies (vim-enhanced & vim-common).
A Beginner’s Guide to Vim
Opening Vim
You can use vim
command with sudo
privilege to open an existing file. For example:
sudo vim name_of_the_document
Let’s take an another example and create an another document with file name “index.html” and add some HTML
code in the file. To do that, run:
sudo vim newdocument.html
Now, let’s add some html
codes in it:
<h1>Main Heading Goes Here</h1> <h2>Sub-Heading Goes Here</h2> <p>Paragraph Goes Here</P> <a href="#">Link</a> <hr> <h3>Another Sub-Heading Goes Here</h2> <p>Paragraph Goes Here</P> <a href="#">Link</a> <hr>
To save changes press colon :
and then wq!
to save and quite the changes.
Modal Editing
Most of the text editors have only one mode while Vim editor also have an another mode called “Modal Editing” mode. This is key difference between Vim and any other text editors.
In “Modal Editing”, some special functions get enabled to help you perform copying text by holding multiple modifier keys and then hitting a regular key.
Vim text editor uses these distinct modes to separate these functions from the normal input task.
Let’s check these modes one-by-one:
Normal Mode
It is basically used for editing operations like copy-pasting, deleting, moving and editing text.
By default, Vim starts in “normal” mode. If you are in some other mode, you can return back to the normal mode by pressing the escape button ESC or <C-[>
.
In normal mode, key presses do not work. But there are certain actions that you can perform to move the cursor.
Move the cursor
h
– move one character leftj
– move one row downk
– move one row upl
– move one character right
As many vim commands, row movement can be prefixed by a number to move s several lines at a time:
To move several lines, rows and characters, you can prefix a number.
4j
– This will move 4 rows down10l
– This will move 10 characters right
Basic word movements:
w
– move to the beginning of next wordb
– move to the beginning of the previous worde
– move to end of wordW
– move to the beginning of the next word after a white spaceB
– move to beginning of the previous word before a white spaceE
– move to end of the word before a white space
moving to the Beginning & End of the line:
0
– move to the beginning of the line$
– move to the end of the linegg
– move to the top of the document.G
– move to the bottom of the document. You can also prefix a number to go to that line number.w
– move to the next word. Prefix a number to move that many words.b
– move back one word. Prefix a number to move back that many words.e
– move to the end of the word. Prefic a number to move that many words.
Insert Mode
Insert mode is used for inserting new text in the document. It enables multiple editing options:
i
– Enters into the insert mode at the current cursor position.a
– Enters into the insert mode after the current position.o
– Inserts a new line below the current line. Also enters insert mode on the new line.I
– Enters into the insert mode at the beginning of the current line.A
– Enters insert mode at the end of the current line.O
– inserts a new line above the current line. Also enters insert mode on the new line.
To leave insert mode, press Esc
or <C-[>
Visual Mode
This third mode used by Vin Editor is known as the Visual Mode which is used for visual selection such as copying, pasting, deleting, replacing, and so on.
v
– To enter regular visual mode. This will mark a selection point and selections can be made by moving the cursor right, left, up, and down.V
– To enter visual line mode. It makes text selection by line. Which means that entire lines can be selected, from the first to the last character, by moving the cursor.-
[ctrl]-v
– To enter visual block mode. It selects the whole block. Moving the cursor up or down can select multiple lines of text.
Press ESC
or <C-[>
, to leave visual mode and return to normal mode.
Command Mode
Command Mode is used for running vim commands. To enter in Command Mode, you simply need to hit the colon key.
:
– Enters command mode.
Command mode has variety of powerful features which can make your task a lot easier. For example, to do a global search and replace, you just need to type:
:%s/foo/bar/g
This particular command will replace all “foo” with “bar”.
:
Enters command mode%
Across all liness
Substitute/foo
is regex to find things to replace/bar/
is regex to replace things with/g
means global. If not mentioned, it would only execute once per line
For Vim related help or documentation, you can also run:
:h
or
:help
Editing
For editing purpose, Vim Editor must be in Normal mode to issue command.
Here are some of the actions that you can perform:
Deleting text
x
– to delete the character under the cursor.d
– to delete text that {motion} moves over. For example, “dl” command will deletes a character to the right.dd
– to delete a line.D
– to delete from the current position to the end of the line.
Changing Text
r
– It replaces the character under the cursor with some other character you input. You just need to substitute the character after issuing this command.c
– It changes the text in the direction that follows. For example, if you press “cw”, vim editor will take you to the insert mode where you can input your replacement text.C
– It change the text to the end of the line.
Copying and Pasting
y
– to copy in the direction that follows.yy
– to copy the entire line.Y
– to copy until the end of the line.p
– to paste the last line copied or deleted below the current line.P
– to paste the last line copied or deleted above the current line.
Miscellaneous Editing
u
– Undo<ctrl>-r
– RedoJ
– Join the current and below line.
Documents Management
To manage the document you must be inside the command line mode of the Vim editor. As you already know to enter in command line mode. You need to press colon “:” to enter and perform these command below:
:q
– to quit vim.:q!
– to forcefully quit vim and discard any unsaved changes.:w
– to save changes. You can also add a space and then a filename if you would like to save the file to a different location.:e
– to dit the file that follows.:bn
– to edit the next file that vim has open.:bp
– to edit the previous file that vim has open.
For more Vim Command and shortcuts, you can visit Vim Documentation.
You may also like:
I hope you will find this guide on how to install Vim on CentOS 7 useful.
Do share your views in the comment section below.