Basic Find and Replace In Vim, you can find and replace text using the :substitute ( :s ) command. To run commands in Vim, you must be in normal mode, the default mode when starting the editor. To go back to normal mode from any other mode, just press the 'Esc' key.
Change and repeat Search for text using / or for a word using * . In normal mode, type cgn (change the next search hit) then immediately type the replacement. Press Esc to finish. From normal mode, search for the next occurrence that you want to replace ( n ) and press . to repeat the last change.
The % is a shortcut that tells vi to search all lines of the file for search_string and change it to replacement_string . The global ( g ) flag at the end of the command tells vi to continue searching for other occurrences of search_string . To confirm each replacement, add the confirm ( c ) flag after the global flag.
Try execute the following or put in into your .vimrc
vnoremap <C-r> "hy:%s/<C-r>h//gc<left><left><left>
By pressing ctrl+r in visual mode, you will be prompted to enter text to replace with. Press enter and then confirm each change you agree with y or decline with n.
This command will override your register h
so you can choose other one (by changing h
in the command above to another lower case letter) that you don't use.
This quick and simple mapping search for visually highlighted text (without over-writing the h register) and without using the terminal dependant + register:
" search for visually hightlighted text
vnoremap <c-f> y<ESC>/<c-r>"<CR>
If you dont like the ctrl-f change it to your preference
Once the text is highlighted you can always do a s
ubstitution simply by typing:
%s//<your-replacement-string>
... because a blank search on the s
command uses the last searched for string.
This one works also (at least for selections in a single line / selections that don't contain any special characters)
y
:s/<C-r>0/
0
is the yank register.
As other people mentioned, there are faster ways to do this, but if you've just started learning Vim (like me), this is one of the 'generic' ways.
//edit: I've just realized that the 'register' approach will fail if there are characters like tabs or newlines or /
characters in the selection (I'd have to manually process those characters somehow in order for the :s
command to work):
The accepted answer works great unless you have special characters in your visual selection. I hacked together two scripts (Jeremy Cantrell's posted here & Peter Odding's) to make a command that will allow you to visual select a string that you want to find even if it has special regex characters in it.
" Escape special characters in a string for exact matching.
" This is useful to copying strings from the file to the search tool
" Based on this - http://peterodding.com/code/vim/profile/autoload/xolox/escape.vim
function! EscapeString (string)
let string=a:string
" Escape regex characters
let string = escape(string, '^$.*\/~[]')
" Escape the line endings
let string = substitute(string, '\n', '\\n', 'g')
return string
endfunction
" Get the current visual block for search and replaces
" This function passed the visual block through a string escape function
" Based on this - https://stackoverflow.com/questions/676600/vim-replace-selected-text/677918#677918
function! GetVisual() range
" Save the current register and clipboard
let reg_save = getreg('"')
let regtype_save = getregtype('"')
let cb_save = &clipboard
set clipboard&
" Put the current visual selection in the " register
normal! ""gvy
let selection = getreg('"')
" Put the saved registers and clipboards back
call setreg('"', reg_save, regtype_save)
let &clipboard = cb_save
"Escape any special characters in the selection
let escaped_selection = EscapeString(selection)
return escaped_selection
endfunction
" Start the find and replace command across the entire file
vmap <leader>z <Esc>:%s/<c-r>=GetVisual()<cr>/
I've included this in my vimrc if that's more useful to anyone.
From Vim 7.4, you can use the gn
motion which selects regions of text that match the current search pattern.
After using cgn
to change the text in currently selected match (or dgn
to delete), in normal mode, you can use .
to repeat the command and operate on the next match.
There's an episode of Vimcast showing this feature.
From http://vim.wikia.com/wiki/Search_and_replace_in_a_visual_selection:
When text is visually selected, press : to enter a command. The command line will automatically enter the range:
:'<,'>
You can enter a command such as s/red/green/g to replace each red with green in all lines of the last visual selection. The command will appear as:
:'<,'>s/red/green/g
To repeat an Ex command over a previously selected block, use the : history. That is, press : then , then edit a previous command.
Mykola Golubyev, Thanks for the tip! Following uses the "+" register which (depending on your terminal) already contains the highlighted text, saving from using the "h" register.
vnoremap <C-r> <Esc>:%s/<C-r>+//gc<left><left><left>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With