# Vim Setup and Basics

Describes how to setup vim using a popular vimrc and explains the basics.

## Search and Replace

                             ?
Up
g#        #        N       Find    n      *         g*
Prev     Prev     Prev      /     Next   Next     Next
Partial  Current  Match    Down   Match  Current  Partial
Match    Word                            Word     Match


Delete from cursor to the end of a search:

d/pattern/e<Enter>


Yank (copy) from cursor to beginning of previous “pattern”:

y?pattern<Enter>


Search and replace:

:%s/search/replace/


The previous searchs and repleaces only the first occurence of the pattern in each line of the current buffer %. To replace all occurences in all lines (not just the first) use:

:%s/search/replace/g


Other useful search flags:

:[range]s[ubstitute]/{pattern}/{string}/[flags] [count]

• c Confirm or skip each match
• i Ignore case
• I Case-sensitive
• n Show number of matches (non-destructive)
• p Print matching lines

Use scopes for search to act on either the current line or the entire file:

:%s/a/b/


A leading percent (denotes the current buffer) searches all lines in the current file.

:s/a/b/


Omit the percent to search only the current line.

:.,a s/a/b/


Complicated ranges are possible. This searches from the cursor (.) to mark a. Search ranges work with lines not characters. Other ways to specify a line:

• . Current line
• +5 Five lines down
• -3 Three lines up
• 1 Line one of buffer
• $ Last line of buffer • % All lines in buffer • 't Position mark t • /pattern/ Next line where pattern matches (Also try ?patter?) To see the range, use visual mode v. Use o to reposition the cursor to the other end of the region. Use the range specifier :'<,'> s/a/b/ to search in the visually selected region. You can use global command to execute a command on all lines that match a pattern. :[range]g[lobal]/{pattern}/[cmd]  • # Show matches with line numbers • d Delete matching lines • y Yank matching lines • normal {command} Execute an extended sequence Show lines and line numbers where pattern occures: :g/pattern/#  Delete blank lines: :g/^$/d


Yank lines after the one that match:

:g/pattern/+y
`

Tags:

Categories:

Updated: