DEV Community

Cover image for Getting started with VI Editor
kaustubh yerkade
kaustubh yerkade

Posted on

Getting started with VI Editor

VI is a powerful text editor. that operates in several modes. Here's a quick guide to get you started with vi.

but first, here's how to exit VI--> 🤗
To exit VI with save - Press Esc , then : then wq!
To exit without save - Press Esc , then : then q!

Modes in vi

1. Normal Mode: The mode you are in when you first open vi. In this mode, you can navigate the file, Explore file contents and enter commands.

Image description

2. Insert Mode: In this mode, you can insert text. To enter insert mode, press i from normal mode.

Image description

3. Command-Line Mode:This mode is used to type commands that start with ':'.

Image description

Switching Between Modes--
From Normal to Insert Mode: Press i (to insert before the cursor) or a (to insert after the cursor).
From Insert to Normal Mode: Press Esc.
To Insert into command mode Press: ':'

Basic Navigation
ESC +
h : Move left
j : Move down
k : Move up
l : Move right
0 : Move to the beginning of the line
$ : Move to the end of the line
w : Move to the beginning of the next word
b : Move to the beginning of the previous word
G : Move to the end of the file
gg : Move to the beginning of the file

Editing Text
ESC+
i : Insert text before curser
a : Insert text after curser
o : Insert new line below curser
O : Insert new line above curser
x : Delete the character under the cursor
dw : Delete the word
dd : Delete the entire line
u : Undo the last change
Ctrl + r : Redo
yy : Copy the line
99999yy : select and copy everything
+p : to paste all
p : Paste the copied line after the current line
r : Replace the character under the cursor
ggVg : select all text

Saving and Exiting
: +
:w -Save the file
:q -Quit vi
:wq -Save and quit
:q! -Quit without saving

Searching
?pattern : Search & highlight the pattern in the file.nohlsearch to remove highlight.
/pattern : Search for pattern in the file. Press n to go to the next occurrence and N to go to the previous one.

Replacing
:s/old/new/g: Replace all occurrences of old with new in the current line.
:%s/old/new/g :Replace all occurrences of old with new in the entire file.

Customization
:set number : Show line numbers
:set nonumber : Hide line numbers
:set autoindent : Enable auto-indentation
:set noautoindent : Disable auto-indentation
:set tabstop=<n> : Set the number of spaces per tab to <n>
:set shiftwidth=<n> : Set the number of spaces for indentation to <n>
:syntax on : Enable syntax highlighting
:syntax off : Disable syntax highlighting
:colo blue : to chnage vi theme to blue

Image description


To open or create a file with vi:
vi file_name.txt

Image description

Image description

To save changes and exit vi-
Press Esc to ensure you are in command mode.
Type :wq and press Enter.

Image description

To exit without saving-
Press Esc to enter command mode.
Type :q! and press Enter.

Image description


*VI Cheat Sheet - *
Image description

Image description


Bonus-

Advanced Navigation
Ctrl + d : Scroll down half a screen
Ctrl + u : Scroll up half a screen
Ctrl + f : Scroll forward one full screen
Ctrl + b : Scroll backward one full screen
H : Move to the top of the screen
M : Move to the middle of the screen
L : Move to the bottom of the screen

Advanced Editing

Deleting
d$ : Delete from the cursor to the end of the line
d0 : Delete from the cursor to the beginning of the line
dG : Delete from the current line to the end of the file
dgg : Delete from the current line to the beginning of the file

Changing
cw : Change (replace) from the cursor to the end of the word
c$ : Change from the cursor to the end of the line
c0 : Change from the cursor to the beginning of the line
cG : Change from the current line to the end of the file

Yanking (Copying)
yw : Yank (copy) from the cursor to the end of the word
y$ : Yank from the cursor to the end of the line
y0 : Yank from the cursor to the beginning of the line
yG : Yank from the current line to the end of the file
ygg : Yank from the current line to the beginning of the file

Visual Mode
v : Start visual mode, use arrow keys to select text
V : Start line-wise visual mode, use arrow keys to select lines
Ctrl + v : Start block visual mode, use arrow keys to select a block of

text
d : Delete the selected text
y : Yank (copy) the selected text
~ : Change the case of the selected text
'>' : Indent the selected text
< : Unindent the selected text

Multi-file Editing
:e filename : Edit another file
:bnext or :bn : Go to the next buffer (file)
:bprev or :bp : Go to the previous buffer
:bd : Delete a buffer (close the file)

Macros
q<letter> : Start recording a macro to the specified register (letter a-z)
q : Stop recording the macro
@<letter> : Play the recorded macro
@@ : Repeat the last played macro

Working with Multiple Files
:split filename : Split the window and open another file
:vsplit filename : Vertically split the window and open another file
Ctrl + w, Ctrl + w : Switch between split windows
:close : Close the current split window

Image description

Other Useful Commands
:!<command>: Execute an external command
Example: :!ls to list directory contents

:r !<command> : Insert the output of an external command into the file
Example: :r !date to insert the current date and time

Check out the amazing game-

Image description

Top comments (0)