VI editor

A comprehensive guide to using the VI/Vim text editor in Linux, from basic operations to advanced features.

πŸš€ Getting Started

Opening and Closing

# Starting VI
vi filename           # Open file in VI
vim filename         # Open file in VIM
view filename        # Open in read-only mode
vimdiff file1 file2  # Compare files

# Saving and Quitting
:w                   # Save changes
:q                   # Quit (if no changes)
:wq or :x            # Save and quit
:q!                  # Quit without saving
ZZ                   # Save and quit

πŸ“ Basic Navigation

Moving the Cursor

h                    # Move left
j                    # Move down
k                    # Move up
l                    # Move right
0                    # Start of line
$                    # End of line
w                    # Next word
b                    # Previous word
gg                   # Start of file
G                    # End of file

Screen Navigation

ctrl+f               # Page forward
ctrl+b               # Page backward
ctrl+d               # Half page forward
ctrl+u               # Half page backward
H                    # Move to top of screen
M                    # Move to middle of screen
L                    # Move to bottom of screen

✏️ Basic Editing

Inserting Text

i                    # Insert before cursor
I                    # Insert at start of line
a                    # Append after cursor
A                    # Append at end of line
o                    # Open line below
O                    # Open line above

Deleting Text

x                    # Delete character
dd                   # Delete line
dw                   # Delete word
d$                   # Delete to end of line
d0                   # Delete to start of line
D                    # Delete to end of line

Copying and Pasting

yy                   # Copy line
yw                   # Copy word
y$                   # Copy to end of line
p                    # Paste after cursor
P                    # Paste before cursor

πŸ”„ Undo and Redo

History Operations

u                    # Undo last change
ctrl+r               # Redo last change
U                    # Undo changes on line
.                    # Repeat last command

πŸ” Search and Replace

Search Operations

/pattern             # Search forward
?pattern             # Search backward
n                    # Next occurrence
N                    # Previous occurrence
*                    # Search word under cursor
#                    # Search word under cursor backward

Replace Operations

:s/old/new          # Replace first occurrence in line
:s/old/new/g        # Replace all occurrences in line
:%s/old/new/g       # Replace all occurrences in file
:%s/old/new/gc      # Replace with confirmation

πŸ“‹ Visual Mode

Visual Selection

v                    # Character visual mode
V                    # Line visual mode
ctrl+v               # Block visual mode
gv                   # Reselect last selection

Visual Operations

>                    # Indent selection
<                    # Unindent selection
y                    # Copy selection
d                    # Delete selection
~                    # Change case

🎯 Advanced Navigation

Word Navigation

e                    # End of word
ge                   # End of previous word
W                    # Next WORD
B                    # Previous WORD
E                    # End of WORD

Line Navigation

:n                   # Go to line n
nG                   # Go to line n
ngg                  # Go to line n
%                    # Matching parenthesis
{                    # Previous paragraph
}                    # Next paragraph

πŸ› οΈ Advanced Editing

Change Operations

cw                   # Change word
cc                   # Change line
c$                   # Change to end of line
ci"                  # Change inside quotes
ca"                  # Change around quotes
ct<char>             # Change until character

Text Objects

iw                   # Inner word
aw                   # A word
is                   # Inner sentence
as                   # A sentence
ip                   # Inner paragraph
ap                   # A paragraph

πŸ“‘ Multiple Files

Buffer Operations

:ls                  # List buffers
:bn                  # Next buffer
:bp                  # Previous buffer
:bd                  # Delete buffer
:b name              # Switch to buffer

Window Operations

:sp filename         # Split horizontal
:vsp filename        # Split vertical
ctrl+w h/j/k/l      # Navigate windows
ctrl+w +/-          # Resize window
ctrl+w =            # Equal size windows

🎨 Customization

Settings

:set number         # Show line numbers
:set nonumber      # Hide line numbers
:set paste         # Paste mode
:set nopaste       # Normal mode
:set syntax=on     # Enable syntax highlighting

Configuration

~/.vimrc           # User configuration file
:colorscheme name  # Change color scheme
:set tabstop=4     # Set tab width
:set expandtab     # Use spaces for tabs

πŸ“š Macros

Recording

q<letter>          # Start recording to register
q                  # Stop recording
@<letter>          # Play macro
@@                 # Repeat last macro

πŸ’‘ Best Practices

  1. Navigation

    • Use motions over arrow keys

    • Learn text objects

    • Master search operations

    • Use marks for quick navigation

  2. Editing

    • Use registers effectively

    • Learn compound commands

    • Use macros for repetitive tasks

    • Master visual mode

  3. Configuration

    • Customize your .vimrc

    • Use plugins wisely

    • Create useful mappings

    • Document your settings

πŸ”§ Advanced Features

Marks

m<letter>          # Set mark
'<letter>          # Go to mark line
`<letter>          # Go to mark position
:marks             # List all marks

Registers

"<letter>y         # Yank to register
"<letter>p         # Paste from register
:reg               # List registers
"+ or "*          # System clipboard

Tags

:tag function      # Jump to function
ctrl+]            # Jump to definition
ctrl+t            # Jump back
:tags             # Show tag stack

πŸŽ“ Learning Path

  1. Beginner Level

    • Basic navigation (hjkl)

    • Basic editing (i, a, d, y, p)

    • Save and quit commands

    • Simple searches

  2. Intermediate Level

    • Text objects

    • Visual mode

    • Multiple windows

    • Basic macros

  3. Advanced Level

    • Complex text objects

    • Advanced macros

    • Registers

    • Custom mappings

πŸš€ Productivity Tips

  1. Speed Optimization

    • Use relative numbers

    • Master text objects

    • Use marks

    • Create mappings

  2. Common Workflows

    • Multiple file editing

    • Search and replace

    • Code navigation

    • Block operations

  3. Plugin Management

    • Essential plugins

    • Plugin managers

    • Custom configurations

    • Performance tuning

Last updated

Was this helpful?