Search and Replace Visually
When you need to search for and replace in only part of a file:
- visually select the block
- type :
- vim inserts ‘<,’> to denote your selection
- search as usual by typing s/oldword/newword/g
- ‘g’ ensures all occurrence, not just the first are changed
To search without visually selecting:
- appending c, prompts for confirmation before replacing
The Power of Global Commands
Syntax:
How do you use this? Say you’d like to delete all blank lines…
Or simply display all line with Joe or Fred:
Or to yank all lines with the word “fred” alter command to y A
Delete everything in between:
Add or delete on many lines, at once!
Say you’re working with:
To surround all the dates with quotes:
- visually select
- type I (or A) (must be capital)
- then type “ (or any word you desire)
- escape
et voila:
Tags, Parenthesis, and all that
With Tim Pope’s Surround Plugin
becomes
through cst<p>. In English:”change surrounding tag to <p>”
Similarly, cs(“ changes () to “”
To surround a word with a <span> tag: ysiw<span>
And best of all, for an entire line, select visually then S to surround with tag of choice.
Pasting
If you paste while in insert mode, Vim will paste poorly formatted text:
There are two solutions:
-
If you brew install Vim (7.4+), use the unnamed register: ”+ p will properly paste from the system’s clipboard.
-
Use paste mode
- :set paste
- paste in insert mode
- :set nopaste (to exit pastemode)
You then get nicely formatted code:
Small Goodies
- gv reselects last visual area
- g; jump to last edit
- s deletes character and activates insert mode