Let's get started ! You have learned how to navigate and switch between your command center (Normal mode) and your text typing zone (Insert mode).
Now it's time to actually shape your text like a master sculptor! We're diving into the essentials of Editing, Searching, and File Operations. Get ready to delete, change, copy, paste, find, and save with newfound speed and precision. Let's unleash more of Vim's power! πͺβοΈ
Sculpting Text in Normal Mode: Your Editing Toolkit π οΈ
Remember, Normal mode is where the magic happens for manipulating text without actually typing new content (unless a command takes you to Insert mode). Make sure you're in Normal mode (press Esc if unsure) before trying these!
Deleting Text (Making Things Vanish!):
xβ Deletes the single character directly under your cursor. Think of it as "x-ing out" a letter.dwβ Deletes from the cursor to the beginning of the next word (delete word). This is super handy for removing words quickly.ddβ Deletes the entire current line. A quick double tapddand the line is gone! Poof!- Pro tip: You can prefix these with a number.
2dddeletes two lines.3dwdeletes three words.5xdeletes five characters.
- Pro tip: You can prefix these with a number.
Changing Text (Delete and Insert in One Go!):
Changing text commands often delete something and then automatically pop you into Insert mode so you can type the replacement.
cwβ Changes from the cursor to the end of the word (change word). It deletes the rest of the word and puts you in Insert mode.ccβ Changes the entire current line. It deletes the whole line, leaves a blank line, and puts you in Insert mode ready to type the new line. Similar toddfollowed byo.C(Shift+c) β Changes from the cursor to the end of the line. Deletes the rest of the line and enters Insert mode.
Yanking (Copying) and Pasting (The Replicator!):
In Vim, "copying" is called "yanking."
yyβ Yanks (copies) the entire current line into Vim's unnamed register (like a clipboard).ywβ Yanks from the cursor to the end of the word.- You can also use Visual mode to select text and then press
yto yank the selection.
Once you've yanked something:
pβ Pastes the yanked text after the cursor (if it was characters) or below the current line (if it was a line).P(Shift+p) β Pastes the yanked text before the cursor or above the current line.
So, a common workflow: yy to yank a line, move your cursor, then p to paste it.
Oops! Undo and Redo (Your Time Machine) βͺβ©
Made a mistake? Deleted too much? Vim's undo is your best friend.
uβ In Normal mode, pressuto undo the last change (or series of small changes). You can press it multiple times to go further back.
Changed your mind about undoing?
Ctrl+R(Control R) β In Normal mode, this will redo the changes that you just undid.
These two are lifesavers! Don't be afraid to experiment, knowing you can always u your way back.
Finding Your Needle: Searching in Vim π΅οΈββοΈ
Need to find a specific word or pattern in your file? Vim's search capabilities are powerful.
/patternβ To search forward in the file:- In Normal mode, type
/. Your cursor will jump to the command line at the bottom. - Type the
pattern(the text you're looking for). - Press Enter. Vim will jump to the first match after your cursor.
Example:/hellowill find the next "hello".
- In Normal mode, type
?patternβ To search backward in the file:- In Normal mode, type
?. - Type the
pattern. - Press Enter. Vim will jump to the first match before your cursor.
- In Normal mode, type
nβ After a search, pressnto jump to the next occurrence of the pattern (in the same direction as the original search).N(Shift+n) β PressNto jump to the previous occurrence of the pattern (in the opposite direction of the original search).
You can make searches case insensitive or use powerful regular expressions, but for now, just knowing /, ?, n, and N is a great start!
Managing Your Work: Basic File Operations πΎπ
You'll often need to save your work or open other files. These are done in Command Line mode (start by typing : from Normal mode).
:wβ Write (save) the current file. If you haven't named the file yet (e.g., you just startedvimwithout a filename), you can give it a name here::w mynewfile.txt.:w new_filename.txtβ Write (save) the current buffer to a new file namednew_filename.txt. This is like a "Save As" operation. The original file (if any) remains unchanged, and you'll now be editingnew_filename.txt.:e filenameβ Edit (open) a file. If you want to openanotherfile.txtin your current Vim window (replacing what you're currently editing, so save first if needed!), type:e anotherfile.txt.
Remember our quitting commands from before? :wq saves and quits. :q! quits without saving.
Global Makeover: Replacing Text π
Need to change all occurrences of "apple" to "orange"? Vim's substitute command is your tool. This is also a Command Line mode command.
:s/old/new/gβ This is the basic form for substitution.sstands for substitute.oldis the text you want to replace.newis the text you want to replace it with.gstands for global, meaning replace all occurrences on the current line.- If you omit
g, it only replaces the first occurrence on the current line.
Example: To change the first "cat" to "dog" on the current line:
:s/cat/dog/
Example: To change all "cat"s to "dog"s on the current line::s/cat/dog/g:%s/old/new/gβ The%symbol here means "on all lines in the file". So, this command will replace all occurrences ofoldwithnewthroughout the entire file. This is very powerful!
Example: To change every instance of "error" to "feature" in your whole document::%s/error/feature/g(Use with care, especially the first few times!):%s/old/new/gcβ Addingcat the end means "confirm". Vim will find each match and ask you if you want to replace it (y/n/a/q/l). This is much safer when doing global replacements!
Congrats ! You've just unlocked a massive chunk of Vim's editing power. Deleting, changing, yanking, pasting, undoing, searching, and replacing are the bread and butter of text manipulation.
The beauty of Vim is how these simple commands can be combined and repeated. Don't try to memorize everything at once. Pick a few commands, practice them until they feel natural, then add a few more ! π