Scattered
Hmm .. I remember the first time I met Vim. That’s when I started to graduate and go to work, then I was asked to use an operating system that I had never used, which was ubuntu linux. This is the first time I need to use the Linux operating system regularly, so when I am using windows and switch to ubuntu, I am no different from a first-grader, overwhelmed. At that time, I was instructed by a brother to install related programs and software. Including Vim and I only understand that the file editor is something, and I installed it so it’s rarely used)
Later when working, I sometimes used it and saw people using Vim when editing files without the mouse, they saw “ahh … really skillful”, it also made me more curious about it. . I use nano more because it’s a lot easier, but switching to Vim is confusing since insert text. Until recently, I started to learn about deploy server, I have to work with it more, so today I am determined to learn about it. The starting point is always important, and I want to make the experience as easy to start as possible.
What is Vim
Vim – short for Vi IMproved is a copy, with some additions, of the vi
editor of Bill Joy
for Unix. It was written by Bram Moolenaar based on the source code of a port of Stevie editor to Amiga and first released in 1991. Vim is used very strongly in CLI (command-line interface). Linux uses a lot of configuration files, I will often need to edit them and vim
is a great tool to do that. Also vim alternatives are nano of the command-line editor.
Install Vim on linux
To use Vim, you must install it via the following command:
1 2 3 | sudo apt update sudo apt <span class="token operator">-</span> get install vim |
On MacOS, you can also install it, you can learn how to install it yourself
Steps to manipulate Vim basically
Step 1: Open the Terminal
Before using Vim, I need to prepare a little. Here I will create a Tutorial folder and go inside that directory.
Done, it’s time for the fun part, start using Vim.
Step 2: Create and close the Vim file without saving
As above I said it was very embarrassing to start using Vim. The most frightening part is “what if I change an existing file and mess it up?”. So I want to know: How can I open and close the file without saving my changes?
I can use the same command to create or open files with Vim as follows:
1 2 | vi <span class="token tag"><span class="token tag"><span class="token punctuation"><</span> FILE_NAME</span> <span class="token punctuation">></span></span> |
Now, this is a very important concept to remember in Vim. Vim has many modes, here are three things you need to know to do basic with Vim:
Regime | Description |
---|---|
thường | Default; for simple navigation and editing |
Insert | To insert and modify clear text |
Command Line | For activities like saving, exiting, etc. |
Vim has other modes like Visual, Select and Ex-mode, but the three modes above are good enough to get started.
Now I’m in Normal mode. If the file has data, you can move around with the arrow keys or another key combination. To make sure you’re in Normal mode, just press Esc (Escape). Next if I press the colon in Normal mode, it will switch Vim to Command Line mode and type : q! – command exits the editor without saving. You can also use the ZQ key combination, but I prefer to use the more convenient one :q!
. When I press Enter , I will exit Vim.
Step 3: Make and save edits in Vim
Reopen the file you just created in step 1. First press Esc to make sure it is in Normal mode, then press i to enter Insert mode. The letter i
Looking to the bottom left, you will see – INSERT – . This means you are in Insert mode already.
I will write a code here
1 2 3 4 5 6 7 8 9 10 11 12 13 | <span class="token doctype"><!DOCTYPE html></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span> html</span> <span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span> body</span> <span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"><</span> h1</span> <span class="token punctuation">></span></span> My first PHP page <span class="token tag"><span class="token tag"><span class="token punctuation"></</span> h1</span> <span class="token punctuation">></span></span> <span class="token php language-php"><span class="token delimiter important"><?php</span> <span class="token keyword">echo</span> <span class="token double-quoted-string string">"Hello World!"</span> <span class="token punctuation">;</span> <span class="token delimiter important">?></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span> body</span> <span class="token punctuation">></span></span> <span class="token tag"><span class="token tag"><span class="token punctuation"></</span> html</span> <span class="token punctuation">></span></span> |
To save the above code, you need to exit Insert mode by Esc then enter Command Line mode. Then type : x! or : wq and press Enter to save and exit the file.
Step 4: Basic navigation in Vim
Although I can use the arrow keys to move around a file. So what if the file has big data, it will be very difficult right. Vim has a lot of great navigation features, but the first is how to navigate to a specific stream.
Continue with the Esc key to Normal state, then type : set number and press Enter , Yeahhh … I have the number to the left of each line.
Good, good So how do I jump to a certain line? , then you would write the command : <LINE_NUMBER> , where LINE_NUMBER is the number of lines you want to go and press Enter . For example, I moved to line 8, it will be
1 2 | :8 |
And now I’m in line 8.
So now imagine a bit, the file above has about 1000 lines and I want to go to the end of that file. What should I do now?
Press Esc in Normal mode, then type : $ and press Enter . I was at the last line!
Step 5: Basic editing in Vim
Once I have basic navigation in the file, now apply it to make some basic edits in Vim. Switch to Insert mode ( i key). I can use the keyboard to erase or insert characters, but Vim has some nice things.
Move to line 8, press d twice as fast as possible ( dd – remember to turn off Vietkey: v). Boom! line 8 disappears and each subsequent line is moved up, that is, line 9 becomes line 8.
So it’s the delete command. Now press the u key, you will see that the deleted line 8 has been restored. This is the undo command.
Next, copy and paste the text, but first learn how to hightlight
the text in Vim. Press the v key and arrow keys to select and deselect text. It can also help you if you want to show others that code more clearly.
When the text you want to copy is highlighted, press y – yank mode and it will copy it to the clipboard. Next, create a new line below with the o key. Note that it will put me into Insert mode, press Esc to exit and press p – paste to paste the text just copied into that line. When finished, I will save the file with : wq .
Step 6: Basic search in Vim
When you want to edit the code on a certain line in a file, how to do it quickly? You can also move by line number if you know which line it is on? Or maybe you want to search for that code with a certain keyword.
Vim is also functional so the search is very helpful. You switch to Command Line mode, then press the colon with the following syntax:
1 2 | :/<SEARCH_KEYWORD> |
Where <SEARCH_KEYWORD> is the text string you want to find. Here I search by keyword “Hello world”.
However, that keyword may appear many times, so you can use the n – Next key to go to the next position of the keywork (remember to exit Insert mode. So you have completed the steps to use Vim It’s basic!
Use split mode in Vim
Exit hello.php
and create a goodbye.php
file. In Terminal type vi goodbye.php and press Enter to create a new file named goodbye.php . Enter any code you want and save your changes.
In Command Line mode, enter : split hello.php , and see what happens.
Ohh! The split command creates windows horizontally that divide 2 different filles. To switch between windows, hold Control on Mac or Ctrl on PC, then press ww (twice for w in a row).
Note:
If you want to display the window vertically, use the command : vsplit <FILE_NAME> instead of : split <FILE_NAME> .
You can open more than two files by repeating either of these commands.
Some basic commands
Vim has many different commands, you can learn here about a basic command of Vim!
Conclude
In this article, I have learned how to use Vim just enough to be able to serve for work and projects. But this is just the beginning of a journey to become a Vim master if anyone wants. You need to learn a lot of Vim’s other commands and master them. Hope my article helps a little bit for you
References:
https://en.wikipedia.org/wiki/Vim_(product_products)
https://www.computerhope.com/unix/vim.htm
https://coderwall.com/p/adv71w/basic-vim-commands-for-getting-started