Vim is a modal text editor for writing code, where you navigate round the screen with a keyboard instead of a mouse.
Highly configurable Vim text editor is a favorite among system administrators and programmers. It is well-known for being strong and effective, but it requires a lot of study, particularly for inexperienced users. Because Vim is based on an older editor named Vi, the terms “Vi/Vim” are frequently used interchangeably.
It was called VI – a Linux text editor which came about in 1976 by BILL JOY. Then it was improved in 1991 by BRAM MOOLENAAR.
WHY WOULD I WANT TO USE VIM?
Your fingers should always be glued to the keyboard if you write code on a daily basis. Your productivity sort of goes down the drain every time you touch the mouse.
Learning to code with VIM is similar to learning to play an instrument; it will hurt at first, but eventually the discomfort will lead to more accurate and useful code editing.
-
It is a timeless ability. That is to say, once you learn something, you will always remember it.
-
It facilitates quicker and more effective work within your editor.
There are basically four (4) modes you need to be concerned about.
-
Normal Mode – This is the default mode when you open Vim. In this mode, you can move around the file and use commands to edit text.
-
Insert Mode – This mode allows you to insert text into the file. You can switch to insert mode by pressing i (for “insert”) from normal mode.
-
Visual Mode – This mode is used for selecting text. You can switch to visual mode by pressing v in normal mode.
-
Command Mode – This mode lets you execute commands that affect the whole file or the editor itself, such as saving the file or exiting Vim. You can access this mode by pressing : in normal mode.
NORMAL MODE
This mode allows you to move your cursor around. The most foundational movement you need to know are
-
j – This moves your cursor downwards
-
k – moves your cursor to upwards.
-
l – moves the cursor to the right.
-
h – moves the cursor to the left.
-
gg – Go to the top of the file.
-
G – Go to the bottom of the file.
h & l can be tedious when using it to navigate your way through because it moves letter by letter, but vim is fast, so we use w to move the cursor to the right word by word and b to the left. Something equivalent to pressing ctrl
+ left
and right
arrow keys for Windows or the holding option
and pressing the arrow
keys
on Mac. Those movements are called motion. A motion is anything that moves the cursor.
COMMAND COUNT MOTION
Three elements make up the concept of the COMMAND COUNT MOTION in Vim: a motion, an optional count, and a command. These elements provide you the ability to manipulate text precisely and powerfully.
COMMAND: The desired action, like removing, editing, or yanking (copying) text.
Examples are
-
y (yank/copy)
-
c (change)
-
d (delete)
COUNT (optional): A number that specifies how many times the command should be repeated or how many units of motion to apply the command to. If omitted, the default is 1.
Example:
2 – (apply the command twice or move two units)
MOTION: The direction or range of text to which the command applies. It defines what part of the text the command should operate on.
Examples:
w: (word)
e: (end of word)
$: (end of the line)
0: (beginning of the line)
G: (end of the file)
gg: (start of the file)
Examples:
dw:
COMMAND: d (delete)
MOTION: w (word)
Result: Deletes from the current cursor position to the beginning of the next word.
d2w:
COMMAND: d (delete)
COUNT: 2 (apply to two words)
MOTION: w (word)
Result: Deletes the next two words.
c$:
COMMAND: c (change)
MOTION: $ (to the end of the line)
Result: Changes the text from the cursor position to the end of the line.
y2e:
COMMAND: y (yank/copy)
COUNT: 2 (apply to the next two word endings)
MOTION: e (end of word)
Result: Copies the text from the cursor position to the end of the second word.
NOTE: To perform the COMMAND COUNT MOTION, you have to switch to Normal Mode by pressing Esc. By staying in Normal Mode, you can efficiently navigate and manipulate text using the COMMAND COUNT MOTION pattern.
INSERT MODE
If you notice the cursor in Vim isn’t like your normal cursor you are familiar with. The cursor for Vim is fat and covers an entire character, it is something you should get use to if eventually you want to start using this cool editor.
In Vim, Insert Mode is where you can enter and modify text. To switch to Insert Mode from Normal Mode, you can use one of the following commands:
Ways to Enter Insert Mode:
-
i
– Enter Insert Mode at the cursor’s current position. -
I
– Enter Insert Mode at the beginning of the current line. -
a
– Enter Insert Mode after the cursor (i.e., one position to the right). -
A
– Enter Insert Mode at the end of the current line. -
o
– Open a new line below the current line and enter Insert Mode. -
O
– Open a new line above the current line and enter Insert Mode.
Example:
- Pressing
i
in Normal Mode will switch you to Insert Mode, allowing you to start typing text at the current cursor position.
Exiting Insert Mode:
- To exit Insert Mode and return to Normal Mode, press
Esc
.
Once in Insert Mode, you can type and edit text as you would in any other text editor.
VISUAL MODE
Visual Mode in Vim is used for selecting text. Once text is selected, you can perform operations like copying, deleting, or changing it. There are a few types of Visual Mode, each with its own way of selecting text:
Types of Visual Mode:
-
Character-wise Visual Mode (
v
):-
Enter: Press
v
in Normal Mode. - Usage: Selects text character by character. Move the cursor to expand or shrink the selection.
-
Enter: Press
-
Line-wise Visual Mode (
V
):-
Enter: Press
V
(uppercase) in Normal Mode. - Usage: Selects entire lines. Move the cursor up or down to select additional lines.
-
Enter: Press
-
Block-wise Visual Mode (
Ctrl-v
):-
Enter: Press
Ctrl-v
in Normal Mode. - Usage: Selects a block of text, which can be useful for selecting columns of text. Move the cursor to expand or shrink the block selection.
-
Enter: Press
Example Operations in Visual Mode:
-
Copying Selected Text: After selecting text, press
y
to yank (copy) it. -
Deleting Selected Text: After selecting text, press
d
to delete it. -
Changing Selected Text: After selecting text, press
c
to change it (you’ll be switched to Insert Mode to type the new text).
Exiting Visual Mode:
-
Press
Esc
: To exit Visual Mode and return to Normal Mode.
Visual Mode is versatile and can significantly speed up text manipulation tasks in Vim.
COMMAND-LINE MODE
Command-Line Mode in Vim allows you to enter and execute commands that affect the entire file or the Vim environment. You access Command-Line Mode by pressing :
in Normal Mode. This mode is used for a wide range of commands, from saving and quitting files to performing complex text manipulations and configuration tasks.
Basic Commands in Command-Line Mode:
-
Saving and Quitting:
-
Save the file:
:w
-
Save and quit:
:wq
or:x
-
Quit without saving:
:q!
-
Save the file:
-
Searching and Replacing:
-
Search for a pattern:
: /pattern
-
Replace all occurrences in the file:
: %s/old/new/g
-
Replace in a specific range:
:10,20s/old/new/g
(replaces in lines 10 to 20)
-
Search for a pattern:
-
Navigation and File Operations:
-
Go to a specific line:
:line_number
(e.g.,:25
to go to line 25) -
Open a new file:
:e filename
(e.g.,:e newfile.txt
)
-
Go to a specific line:
-
Undo and Redo:
-
Undo changes:
:undo
(or justu
in Normal Mode) -
Redo changes:
:redo
(orCtrl-r
in Normal Mode)
-
Undo changes:
-
Configuration:
-
Set options:
:set option=value
(e.g.,:set number
to show line numbers) -
Show current settings:
:set
-
Set options:
-
Command History:
-
Access previous commands: Press
:
and then use the arrow keys to navigate through command history.
-
Access previous commands: Press
Example Workflow:
- Press
:
in Normal Mode to enter Command-Line Mode. - Type the command you want to execute (e.g.,
:wq
to save and quit). - Press
Enter
to execute the command.
Command-Line Mode is powerful for managing files, configuring Vim, and performing advanced text operations. It complements Normal and Visual Modes by allowing you to execute broader commands and manage your text editor environment.
Source link
lol