Spread the love

Bill_joy

____________________________________________________________________________________________________

This CrashCourse will not make you an expert in very less time and Learning vim will be painful at first. It will take time. It will be a lot like playing a musical instrument. Don’t expect to be more efficient with vim than with another editor in less than 3 days. In fact it will certainly take weeks instead of 3 days.

                    But I am sure,  it will be your last text editor. There isn’t any better text editor that I know of. It is hard to learn, but incredible to use. I have designed this course in different four levels,  i suggest you to start slow with level 1. 

By the end of this journey, you’ll feel the power in your finger.

Beginner

  • Open Vim editor by typing vim filename (i.e. vim main.c).
  • In a standard editor, typing on the keyboard is enough to write something and see it on the screen. Not this time. Vim is in Normal mode. Let’s go to Insert mode. Type the letter i.
  • You should feel a bit better. You can type letters like in a standard editor. To get back to Normal mode just press the ESC key.
  • You now know how to switch between Insert and Normal mode. And now, here are the commands that you need in order to survive in Normal mode:

Command Detail
i Insert mode. Type ESC to return to Normal mode.
x Delete the char under the cursor
:wq Save and Quit (:w save, :q quit)
dd (and copy) the current line
p Paste
hjkl cursor move (←↓↑→). Hint: j looks like a down arrow.
:help <command> Show help about <command>.
   
  • Only above 5 commands. That is all you need to get started. Once these command start to become natural (maybe after a day or so), you should move on to level 2.
  • But first, just a little remark about Normal mode. In standard editors, to copy you have to use the Ctrl key (Ctrl-C generally). In fact, when you press Ctrl, it is as if all of your keys change meaning. Using vim in normal mode is a bit like having the editor automatically press the Ctrl key for you.
  • Commands starting with : end with <enter>. For example, when I write :q, I mean :q<enter>.

 

Feel Comfortable 

Now, You know the basic commands required to operate Vim editor . It’s time to learn a few more commands. These are my suggestions:

Basic Moves

Command Detail
:/pattern Search for pattern in current file
0 Go to the first column
$ Go to the end of line
gg Go to the start of the file
G Go to last line
 :%s/text to search for/text to replace/g Search text occurrences in file and replace all with text given

Load / Save / Quit File

Command Detail
:w Save current file
:saveas <path/to/file> Save to <path/to/file>
:x, ZZ or :wq Save and quit (:x only save if necessary)
:q! Quit without saving, also: :qa! to quit even if there are modified hidden buffers
   

Undo /Redo

Command Detail
u Undo
Ctrl+r Redo
   

Copy /Paste

Command

Detail

p

paste before, remember p is paste after current position.

yy

copy the current line, easier but equivalent to ddP

v

Start Visual selection from current cursor position (Mostly used to copy multiple line by select area press yy and then paste by pressing p)

   

Take the time to learn all of these command. Once done, you should be able to do every thing you are able to do in other editors. You may still feel a bit awkward. But follow me to the next level and you’ll see why vim is worth the extra work.

 Better, Stronger, Faster

 Put the line numbers in each line (Better)

Go to Normal mode by pressing ESC and write :put =range(1,10). It will print line number starting in each line starting from 1 to 10. you can use custom arguments in “range(x,y)”. For more information use :help put

Line number print in vim

You can also use following command, in Normal mode

:set number

Copy/Paste multiple line (Stronger)

Step 1 Go to address from where you want to start copy.

Step 2 Go to Normal mode by pressing ESC and then v (visual selection).

Step 3 Go up and down until copy area complete.

Step 4 Press yy which is copy(yank) the selected area(press ‘y’ key twice).

Step 5 Go with cursor where you want to paste copied data and press p.

Multiple line Copy Paste in vim

Auto Detecting/Completing Words (Faster)

In Insert mode, just type the start of a word, then type Ctrl+p, just see the magic…

 Auto Detect Word vim

 

Use Mouse in Vim

Use following command in Normal mode.

:set mouse=a

Feel the power in your fingers

This Level is specially designed for the software developers and this will take you to high of Vim. When you master all following command, you will be able to increase your productivity and i am sure you would never use any other editor than Vim.

Open File And Go To Specific Function or Line Number

This is very useful when one coding and accessing files through terminal, because when compiler gives error in particular file, we have to move to that location and mend it. So in this scenario this trick or feature of Vim found very useful.

  1. Go to specific line number

vim fileName +LineNumber

i.e. vim main.c +3

Go to specific line in vim

  1. Go to specific function

vim filename +/pattern

i.e. vim main.c +/main

3.   Go to specific line number if your file is already opened

Type :linenumber in Normal mode and press enter, you will be at your desired location 

For example    :42

Commenting or Editing multiple line in single stroke

Step 1 Go to Normal mode by pressing ESC key.

Step 2 Press Ctrl+v (visual block selection) from where you want to edit.

Step 3 Move cursor up and down to number of line you want to edit and press I// .

Step 4 Press ESC again and wait for second it will comment all the selected line or you can say add // in front of each selected line.

Typically:  ESC, Ctrl+v, ↑or↓,  I<PatternToAdd>, [ESC]

 Commenting multiple line in single stroke

Align code in curly braces (indent)

Step 1 Go inside the culy braces in which you want to align code properly.

Step 2 Go to Normal mode by pressing ESC then press =.

Step 3 Now press i.

Step 4 To see the magic press either { or } .

Typically:  ESC, =, i, {

 Align code in vim indent

Working with multiple file in Vim

This is very useful when we working with multiple file. For example, if you are coding in a file and at the same time you need another file for reference, so general technique is to use another terminal or open new window. But Vim have another nice option, by which you can split current working window in two sections and work on both file at the same time. 

Split window vertically or horizontally

In Normal mode, use command

:vs filename         ———Split window vertically

:sp filename         ———Split window horizontally

Spliting window in vim

Note:- use Ctrl+w to shuffle between two opened section in Normal mode.

 

To Practice the Vim, There is one online game also, check it http://vim-adventures.com/