VIM – Beginners Guide

Introduction

VIM is a text editor for writing code, where you navigate around the screen with the Keyboard instead of a Mouse.
It is based on original UNIX text editor named VI which came about in 1976 by Bill Joy. It was later improved by B…


This content originally appeared on DEV Community and was authored by Vishwa Mittar

Introduction

VIM is a text editor for writing code, where you navigate around the screen with the Keyboard instead of a Mouse.
It is based on original UNIX text editor named VI which came about in 1976 by Bill Joy. It was later improved by Bram Mooleenar in 1991 with bunch of improvements and named VIM.

Why VIM?

Why would you use VIM when we have modern IDEs? The answer is, when you write code, your fingers should be bound to keyboard all the time. Every time you touch mouse your work rate is interrupted.
Learning to code with VIM is same as learning something new from scratch, it will be painful at first but that pain will lead you to more precise and productive code editing in the future.

Installation

Linux/MacOS: VIM comes preinstalled.
Windows: Download VIM, Download the installer and install it.

How to use

Linux/MacOS: Type "vim filename.txt" in terminal (without quotations)
Windows: Open "gVim8.1" from desktop or search in start menu.

Vim Modes (Press Esc to switch between modes)

VIM has many modes, but these four are commonly used:

Normal mode

If you see cursor thicker or little wide, means you're in normal mode.

Normal mode is Vim's default mode and you'll be using it most of the time. When you are in normal mode you can navigate and perform operations throughout the file.

You can move cursor between characters, words, lines, sentences, paragraphs. code blocks and more.

Characters

"h" (move left to character)
"l" (move right to character)

Words

"w" (move to next word)
"e" (move the end of current word)
"b" (move back to the previous word)

Lines

"j" (previous line)
"k" (next line)

Sentences

"(" (previous sentence)
")" (next sentence)

Paragraphs

"{" (previous Paragraph)
"}" (next Paragraph)

For simplification:
image

k (up)
j (down)
h (left)
l (right)
You can also use arrow keys to navigate but for best practice you must use (k,j,h,l).

Insert mode

Press i key in the normal mode to get into insert mode. The word INSERT will appear at the bottom of the screen.
You can think of it like edit mode, because now you can write/delete inside the file.

Keys to get into insert mode:

Make sure you're in normal mode then press,

i (insert from the right of the character)
a (insert from the left of the character)
I (insert from the start of the line)
A (insert from the end of the line)

Once you are done with editing your file, press Esc key to go back to the normal mode.

Command mode

Commands can only be typed in **Normal* mode.*
When you're in normal mode, you can write commands. Every command starts with ":".

Basic commands:

:w (save file)
:q (quit file. but the file must be saved first)
:wq (save and quit)
:q! (quit without saving)

There are many more commands, but these are the common and you'll need these all the time.

Visual mode

When editing text with Vim, visual mode can be extremely useful for identifying chunks of text to be manipulated.

Press the v key to enter visual mode. The word VISUAL will appear at the bottom of the screen.

Use the Arrow keys to highlight the desired text. You can use other navigation commands, such as w to highlight to the beginning of the next word or $ to include the rest of the line.
Once the text is highlighted, press the d key to delete/cut the text.
If you deleted too much or not enough, press u to undo and start again.
Move your cursor to the new location and press p to paste the text.

Tips and tricks

  1. To go forward 3 words, instead of www, we can do 3w.
  2. To go down 10 lines, instead of jjjjjjjjjj, we can do 10j.
  3. dw to delete a word.
  4. dd to delete a word in a line.
  5. x to delete a character.
  6. r followed by new character to replace a character.

There's still a lot to learn. Keep exploring. Happy coding.

If you learnt something from this article, please like, comment and share.

Follow me on Instagram.
Follow me on Facebook.
Follow me on Twitter.


This content originally appeared on DEV Community and was authored by Vishwa Mittar


Print Share Comment Cite Upload Translate Updates
APA

Vishwa Mittar | Sciencx (2021-10-03T10:58:54+00:00) VIM – Beginners Guide. Retrieved from https://www.scien.cx/2021/10/03/vim-beginners-guide/

MLA
" » VIM – Beginners Guide." Vishwa Mittar | Sciencx - Sunday October 3, 2021, https://www.scien.cx/2021/10/03/vim-beginners-guide/
HARVARD
Vishwa Mittar | Sciencx Sunday October 3, 2021 » VIM – Beginners Guide., viewed ,<https://www.scien.cx/2021/10/03/vim-beginners-guide/>
VANCOUVER
Vishwa Mittar | Sciencx - » VIM – Beginners Guide. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/10/03/vim-beginners-guide/
CHICAGO
" » VIM – Beginners Guide." Vishwa Mittar | Sciencx - Accessed . https://www.scien.cx/2021/10/03/vim-beginners-guide/
IEEE
" » VIM – Beginners Guide." Vishwa Mittar | Sciencx [Online]. Available: https://www.scien.cx/2021/10/03/vim-beginners-guide/. [Accessed: ]
rf:citation
» VIM – Beginners Guide | Vishwa Mittar | Sciencx | https://www.scien.cx/2021/10/03/vim-beginners-guide/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.