Hi, what’s up?
Wanna good debate? I am all set!
Are you wondering why am I up to a firm debate? Then you must not be aware much about text editors, right? Well, let me tell you then.
Vim is actually a really powerful, CLI based, text editor, which most of the developers used as text buffers. It is the enhanced version of “Vi”. But the problem is, Vim is not the only one, There are various text editors available viz. Atom, Nano, Sublime, Gedit, Emacs, et,. As a result, their corresponding and constant users always remain on debateful wars ( a healthy-minded one ). That’s why it is hard to point out the Best out of them. But just for the starting, let’s have a look at The Vim.
Since Vim is on Command Line Interface, therefore I am using in this blog My Linux Terminal for this.
First, let us install the Vim. You may install it via command:-
sudo apt-get install vim
After Vim is installed, you are set to use it. Now command terminal:-
A new empty file is created with the name “newfile” (if not created before) and will open in the vim text editor. Now you are having vim opened in front of you (left-hand side empty lines are represented by strudel/curl sign “~” and bottom left corner shows the file name i.e “newfile” in this case). But currently, you are in Vim’s “Command Mode” (a mode in which you can give commands to the editor). If you want to add certain text in this empty file you need to Enter in “Insert Mode” (a mode in which you can insert text in the file ). Alright? So press:-
i – ( to enter into Ins. Mode )
look at the left bottom corner now showing “–INSERT–” (showing that you are in insert mode), But be careful Vim is case sensitive, it means “o” is different than “O”. But whether you pressed ‘i’ or ‘I’ you will enter in the insert mode. You may type text to enter now:-
this is just for testing purpose!
And this is two line text.
Now, to return to Command Mode press <escape>. You did it? If yes, then see to the bottom-right corner now showing the coordinates where your blinking cursor is. 2 28 in my case this shows 2nd line and 28th column. Get it?
To move the cursor in the editor: you may use arrow keys, no restriction in that. But if you know touch typing then moving your hands from “alpha keys” towards “arrow keys” will be time-consuming that you will realize when you work on a lengthy project, like programming codes, documentation, and all. Therefore in Vim, you may also use:- ‘h’ – to move left, ‘j’ – to move down, ‘k’ – to move up, ‘l’ – to move right. Once you are comfortable with these “h,j,k,l” you won’t find it confusing.
Now to save the file, type:
:w – to save file (every external command is started by “:” colon)
To exit the file after saving:
:q – to quit Vim
To exit the file without saving:
:q! -to quit Vim and “!” means forcefully (without saving)
To regain the file as it was while opening:
:e! -useful to restore all the changes since its opening
To save and exit simultaneously:
Let us study commands in alphabetical order. But first make sure that you are in your command mode by pressing <escape> and You already have that “two-line text with you” you may insert more by entering first in insert mode then coming back to the command mode, you may proceed further now.
Before we start: see the command “u” it is undone ( it will undo the last change in as usual reverse order ). and “.” dot command ( it will repeat the last command as many times as pressed: not applicable to all commands, with little logic you will soon, understand why? ).
Press: a — to append text after the current alphabet.
A — to append at the end of the line.
Press: b — to move backward word vice, ( with the beginning of the words ).
B — to move backward word vice too. Same as b
// these can be used with numerical prefixes. e.i 4b or 4B will take you back 4 words. ?b or ?B will take you back ‘?’ Words.
Press: c — to change the current letter. Press c then l this will cut the current letter and you will enter in insert mode to change that letter.
// other alterations.
Like, cc or C — will cut the complete line to change.
// some common suffixes:-
$ :- to impose the command till the end of the line.
0 :- to impose the command till the beginning of the line.
Repeating the letter :- to impose the command on the complete line.
e :- to impose the command till the end of the word.
w :- to impose the command till the beginning of the next word.
With this, our ‘c’ command has been available in the following formats as well.
c$ will cut the line from the cursor to the end of the line. Also, c0 will cut the line from the cursor to the beginning of the line.
Press: d? — to delete and ‘?’ can be replaced by any of those common suffixes.
D — to delete complete line
Press: e — to move forward word vice, ( with the ends of words ).
E — to move forward word vice, ( with the ends of words ).
Press: f and F – does nothing as far as I know!
Press: gg — will take you at the beginning of the file (first line).
G — will take you at the end of the file (last line).
?G — will take you on ‘?’ line number.
Press: h — to move left.
H — to reach the first letter of the first line.
Press: i — to enter in “insert” mode.
I — to enter in “insert”mode.
Press: j — to move downwards.
J — to join next line with the current line.
Press: k — to move upwards.
K — “not a vim command” but if you would press K on a word that is a valid terminal command than ‘K’ will give you the manual about that command. Similar to man <command> of the terminal.
Press: l — to move right.
L — to reach the first letter of the last line.
Press: m — does nothing!
M — to reach the first letter of the middle line.
Press: n — to repeat searching. ( we will do when we would reach searching )
N — same as above. ( we will see this as well )
Press: o — will open an empty line below the current line.
O — will open an empty line above the current line.
Press: p — to put (paste) the content just deleted or copied. If it is the line(s) then it will be pasted below the current line.
P — does same as above.
// q is used to quit, already explained above.
Press: Q — to enter in “ex mode”, now you may type “visual” to go back in normal command mode. (here <escape> won’t work as it works to come back from “insert” or “replace” mode)
Press: r? — to replace a single current character with ‘?’ // also, the prefix of numerical works. e.i ‘4rs’ will replace 4 characters ( including current ) with ‘s’.
R — to enter in “Replace” mode. (may press <esc> to go back in normal mode)
Press: s — to substitute from the current character. // numerical prefixes work.
S — to substitute the complete line.
Press: t? — to reach ‘?’ letter in the line after the current character (it will reach the first instance of ‘?’ found)
T? — to reach ‘?’ letter in the line before the current character.
Press: u — to “undo” the changes ( in as usual reverse order preference )
U — to “undo” all the current changes in the current line.
Ctrl + r — It “redo”.
Press: v — to enter in visual mode ( may go back to <esc> ), here you may select text. And operate commands like ‘d’, ‘s’, ‘c’,’r’,’y’, etc. On the selected text.
V — to select the complete line
Press: w — to move forward word vice, ( with the beginning of the words ).
W — same as above.
Press: x — to delete the current character.
X — to delete character previous to current one
Press: y? — to yearn means “copy” (suitable suffixes may be used ) and ‘p’ to paste.
e.i:- y3w will copy 3 words. Or yy will copy the line, etc.
Y — to yearn complete line.
Press: z — Does nothing!
ZZ — to save and exit.
Press: 0 — to reach starting of the line.
Press: $ — to reach the end of the file.
Press: % — in case of open and closing brackets (curly, square, round) it will reach to the other corresponding bracket. Useful in programming.
Press: ^ — to reach the first non-empty character.
Press: ( — to reach first non-empty character of the i) current line and ii) above the line.
) — to reach first non-empty character of the next line.
Press: – — to reach first non-empty character of the previous line.
_ — to reach first non-empty character of the current line.
Press: == — to bring current line’s indentation with the previous line.
Other vim operations and commands:-
1) searching –
Press: /searching_word — this will search for the given word ( forward to cursor )
Or you may do: ?searching_word — this will search for the given word (backward to cursor )
// by default searching will stop at the first instance found. Thus to search for the next instance,
Press: n — to search again for the next instance (forward to the cursor) or: Press: N — to search again for the next instance (backward to the cursor).
// by default searching is case-sensitive, to make searchings ignore case. You need to specify it by giving the command- :set ic — now all your searchings wouldn’t be case-sensitive.
To revert back the function of “ic” (ignore-case) you may type- :set noic — now all your searchings would be again case sensitive.
// but if you want to ignore case just for the current search. Then you may give the searching command like- /searching_word\c — this will ignore case for just this one search.
// to make the searched words highlight you need to give the command- :set hls — ‘hls’ stands for highlight_search.
Similarly to revert back its function you may type- :set nohls — will switch off the highlight.
2.) :set number — will show line numbers (useful in programming and movement)
:set nonumber — will switch off the line numbers.
3.) Ctrl – commands:-
If tag-label[pointer to some other location of the same file or other files, generally have blue/yellow colors] are available then to jump on the tag location, first arrive the cursor on tag-label and Press: Ctrl + ] — will take you to the tag location.
Ctrl + t — will bring you back from where you were gone.
Ctrl + o — will bring you back from where you were gone. ( also work when you move with G,H, or any other movement commands)
Ctrl + r — to rebound “Undoes”. It redo.
Etc., These were just some basic commands, there are many more commands available for/in Vim. Your curiosity will lead you through. Also, if you want to learn more about the Vim you may do as follows:
1. open vim
2. type the command :help — this will open the help manual for vim.
3. scroll down: (with ‘j’)
// In this file you will see some BASIC files that are tag linked with this file ( shown in blue color at the left side of the screen ) to move on the file give command: ctrl + ] (i have already written)
Also, there are some “GETTING STARTED” files/tags and some “EDITING EFFECTIVELY” files/tags and some “TUNING VIM” files/tags and one “MAKING VIM RUN” file/tags all these files are tag linked you may go to the one and come back.
But since these are vim’s own documentation files you can’t normally make changes to them, then how are you going to test the commands, on this file (if it is not allowed).
What you can do in this case, on whatever file you want to make changes and perform the operation as you would be learning in the process, just copy this file to some other location of your preference.
How to copy, it’s very simple to save the file on your required path by giving the command:
Reading these Documentations and manuals are really a quality learning process. But takes time, no doubt.
There is also a Basic Crash course of 30 minutes of vim in its manual. That is called Vimtutor.
You may directly open this tutor file by giving command on your terminal that is:
again First copy, this file in another location (by :w! ~/Public/Newfolder/any_newfile_name ) otherwise you won’t be able to practice commands on it. Alright?
Let’s hope for the best!
See you soon, and I, am GeekyShacklebolt, bidding you goodbye!
“It’s still a draft, adding soon, a little bit left…”