Ultimate Beginner’s Guide to Vim
In our previous post about Vim, we’ve talked about some neat tips and tricks usable for more advanced users. In this post, we will talk about getting started with Vim, so this guide is mostly intended for absolute beginners. Usually, when a newbie opens a document with Vim the first ever user experience is nothing but frustrating, as the user has no idea how to do anything and that includes exiting the program as well. So you are getting frustrated like, “What is this?! I am going back to nano, this is not working!” – as you desperately keep pressing CTRL-C or ESC in an attempt to exit the editor but ending up closing and reopening the PuTTY terminal in the end.
If you are constantly using Linux-like systems, learning Vim is like an investment. It takes time to get started with, but the more you use it, the better and more productive you will be.
So let’s get started.
First things first, you may have purchased a VPS, but if you’ve chosen one of the minimal templates, Vim may be missing on your server. So let’s install Vim first:
How to Install Vim
It’s pretty easy and straightforward. Just execute the commands below based on the distro you are using.
For CentOS
yum install vim
For Ubuntu
apt-get update && apt-get install vim
For Arch Linux
pacman -S vim
General – The three modes of Vim
Before going any further, one must first understand the way Vim works. In a nutshell, Vim has three basic modes of operation.
Command mode – where one can pass commands to Vim in order to manipulate the document. You search for text. Copy/Paste portions of it. Find and replace text based on a pattern and much more. There is almost no limitation of what one can achieve in this mode.
Insert mode – This is where the writing of text gets done.
Visual mode – This is like a sub-mode of the command mode where one can select text.
COMMAND mode
This is the default mode and the one that you will find yourself into when opening a file with Vim. In this mode, you can not type, but you can pass commands to Vim. You can scroll through the document using the arrow keys and go up/down using the Page Up and Page Down keys. By passing commands to Vim, you have the power to quickly manipulate the file you are editing, in ways that no other text editor offers.
There are several ways that you can pass commands into Vim in this mode. For now, you should know only the most basic ones to get you started:
i
– Puts you into INSERT mode – where you can type just as in any other editor; At the same time with the ESC
key you can exit from INSERT mode;:w
– Save/Write the changes to disk;:w!
– Save but do not prompt for confirmation (! stands for force);:q
– Exit from Vim if no changes have been made;:q!
– Exit from Vim and discard the changes;
Some commands (those that are compatible can be combined together).
:wq!
– Save the changes and exit;
The last two commands
:q!
and :wq!
are the ones that you will most often use.INSERT mode
As the name suggest, this is the mode where one does all the typing.
Using
i
from command mode you can enter INSERT mode, type whatever you like and do the changes, and then exit back into COMMAND mode with ESC key.VISUAL mode
In this mode, you can select text. You can think of this mode like a sub-mode of the Command mode. You can either choose to select whole blocks of text or select just a single portion of it. While in COMMAND mode, there are three types of VISUAL mode that one can enter into.
v
– Toggles the most simple visual mode. While in command mode position your cursor anywhere in the text and press v. You will see that at the bottom left of the terminal the status will change to -- VISUAL --
. Now you can move your cursor using the arrow keys and select parts of the text.V
– is the — VISUAL LINE — mode. Use this mode when you need to select whole lines.CTRL-V
– is the -- VISUAL BLOCK --
mode. Use this mode to select blocks of text by freely moving the cursor around.
Now that we know the three basic modes we can learn some more useful commands. These work in Command mode but are also very useful while used in VISUAL mode. That is the exact reason why we are mentioning these command here.
0
– Will position your cursor to the begging of the line;$
– Will position your cursor to the end of the line;o
– Will enter INSERT mode and into a new line for writing;y
– Copy (y stands for yank);yy
– Copy a whole line;p
– Paste;P
– Paste above the cursor;d
– Cut;dd
– Cut the whole line;x
– Is simply delete. Normally it will delete the selected character or the chunk of text selected with v
. However, we found out that in visual mode there is no difference between d
and x.
u
– Undo;CTRL-R
– Redo;:set number
– Toggles line numbering. This does not affect the document, it is just for reference;:set number!
– Turn line numbering off;
While line numbering is on you can quickly jump to a line number you like:
:10
– go to line 10:5
– go to line 5
.. and so on.
Tips for practicing
All that is written above is just a quick opening the door and peeking towards the functionality of Vim.
How to practice? Open a new document
vim lorem.ipsum.txt
, then go to the Lorem Ipsumgenerator and generate some paragraphs of text. Paste those paragraphs in the document and practice the above. While working on your systems, practice editing the files with Vim. As time passes, you will only get better. As we’ve said previously: learning Vim is not just a waste of time, but it is also an investment. It is The Extension of your fingers. It will increase your productivity and functionality. Once you take advantage of its features you will find yourself slowly throwing out all of the other text editors.
Once you get accustomed with these basic commands you can check our other article for more advanced tricks for manipulating your files.
Get a VPS from us and our expert Linux admins will help you if you get stuck somewhere or if you mess something up. They are available 24×7 and will take care of your request immediately.
PS. If you liked this post please share it with your friends on the social networks or simply leave a reply below. Thanks.
Nguồn bài viết lấy từ:
+ https://www.rosehosting.com/blog/vim-tips-and-tricks/
+ https://www.rosehosting.com/blog/ultimate-beginners-guide-to-vim/