DEV Community

SAHIL
SAHIL

Posted on

1

Mastering Advanced Vim Commands – Navigate, Search, Replace Like a Pro πŸ’»

We discussed the Basic Vim commands in

Now it’s time to level up your Vim skills with advanced β€” yet beginner-friendly β€” commands that can save time, improve workflow, and make terminal editing a breeze.

Let’s get started! πŸš€

🧱 Normal Mode Editing Commands

Once you're in Normal Mode (press Esc), you can perform actions like deleting, copying, and pasting β€” no mouse required.

βœ‚οΈ Line Editing Commands

dd    β†’ Delete (cut) the current line  
cc    β†’ Cut the line and enter Insert Mode  
yy    β†’ Copy (yank) the current line  
p     β†’ Paste below the current line  
P     β†’ Paste above the current line  
Enter fullscreen mode Exit fullscreen mode

πŸ“ Make sure your cursor is on the line you want to act upon.

πŸ”’ Repeating Commands with Numbers

You can prepend a number to most commands to apply them multiple times:

3dd   β†’ Delete 3 lines (current + 2 below)  
2yy   β†’ Copy 2 lines  
5cc   β†’ Cut 5 lines and enter Insert Mode  
Enter fullscreen mode Exit fullscreen mode

This is a huge productivity booster when editing in bulk.

🧭 Navigation in Vim

Vim offers fast, precise navigation with simple keystrokes:

Key Action
h Move left
j Move down
k Move up
l Move right
w Jump to next word
b Jump to previous word
0 Jump to beginning of line
$ Jump to end of line
gg Jump to start of file
G Jump to end of file (Shift + g)

🎯 Combine these with commands like d, y, c for powerful results.

Example: dw = delete word, yw = yank word.

πŸ” Searching in Vim

Use these commands to search for patterns in your file.

πŸ”Ž Basic Search

/pattern     β†’ Search forward  
?pattern     β†’ Search backward  
n            β†’ Go to next match  
N            β†’ Go to previous match  
Enter fullscreen mode Exit fullscreen mode

πŸ”§ Useful Pattern Examples

/root           β†’ Find next occurrence of β€œroot”  
/^user          β†’ Match lines starting with β€œuser”  
/bash$          β†’ Match lines ending with β€œbash”  
/foo.*bar/      β†’ Match anything from β€œfoo” to β€œbar”  
Enter fullscreen mode Exit fullscreen mode
Symbol Meaning
^ Start of line
$ End of line
. Any character
* Zero or more
\< / \> Word boundary

Use :nohl to clear search highlights from the screen if they remain after searching.

πŸ”„ Substitution (Find & Replace)

Vim’s :s command helps you quickly replace text.

πŸ“˜ Syntax

:[range]s/pattern/replacement/[flags]
Enter fullscreen mode Exit fullscreen mode

πŸ” Component Breakdown

Part Description
range Line range (% = entire file)
pattern Text to search for
replacement Text to replace it with
flags Optional: g = global, c = confirm, i = ignore case

πŸ”§ Practical Examples

:%s/foo/bar/g         β†’ Replace all "foo" with "bar" in entire file  
:1,10s/cat/dog/g      β†’ Replace from lines 1 to 10  
:%s/hello/world/gc    β†’ Replace with confirmation prompt  
:%s/\<admin\>/user/g  β†’ Replace whole word "admin" with "user"  
:%s/test//g           β†’ Remove all occurrences of "test"  
Enter fullscreen mode Exit fullscreen mode

πŸ”‚ Shell Integration

Use ! in command mode to temporarily drop to the terminal, then return to Vim after running a command.

:!date        β†’ Show the system date without exiting Vim  
Enter fullscreen mode Exit fullscreen mode

To paste the output of a shell command directly into the file at the cursor position:

:.!date       β†’ Insert the date output into the file  
Enter fullscreen mode Exit fullscreen mode

To switch to another file temporarily inside Vim:

:edit /path/to/targetfile  
Enter fullscreen mode Exit fullscreen mode

🧩 Extra Handy Settings

:set number       β†’ Show line numbers  
:set nonumber     β†’ Hide line numbers  
:set ic           β†’ Ignore case in search  
:set noic         β†’ Make search case-sensitive  
Enter fullscreen mode Exit fullscreen mode

In normal mode:

  • Use Shift+D to delete everything after the cursor on the same line.

πŸ“š Bonus Tip – Use vimtutor

Practice all these commands interactively in your terminal:

vimtutor
Enter fullscreen mode Exit fullscreen mode

It’s built-in and walks you through essential Vim usage in under 30 minutes β€” perfect for beginners.

🧠 Summary Table

Task Vim Command
Delete current line dd
Copy current line yy
Paste below p
Move to next word w
Move to end of line $
Go to file start gg
Search for β€œroot” /root
Replace "a" with "b" :%s/a/b/g
Replace with confirmation :%s/foo/bar/gc

βœ… Final Thoughts

Vim may feel intimidating at first β€” but once you get the hang of these commands, it becomes one of the fastest, most powerful editors available.

Start slow, practice often, and soon you’ll be editing like a pro.

πŸ’¬ Have a favorite Vim trick? Share it below!

πŸ’‘ Need help with something Vim-related? I’m here to help.

vim #linux #terminal #productivity #devops #cloud #100DaysOfLinux

A developer toolkit for building lightning-fast dashboards into SaaS apps

A developer toolkit for building lightning-fast dashboards into SaaS apps

Embed in minutes, load in milliseconds, extend infinitely. Import any chart, connect to any database, embed anywhere. Scale elegantly, monitor effortlessly, CI/CD & version control.

Get early access

Top comments (0)

πŸ‘‹ Kindness is contagious

Explore this insightful write-up, celebrated by our thriving DEV Community. Developers everywhere are invited to contribute and elevate our shared expertise.

A simple "thank you" can brighten someone’s dayβ€”leave your appreciation in the comments!

On DEV, knowledge-sharing fuels our progress and strengthens our community ties. Found this useful? A quick thank you to the author makes all the difference.

Okay