sponsor Vim development Vim logo Vim Book Ad

Multi-Replace : Perfom non-cumulative multi-replace

 script karma  Rating 9/3, Downloaded by 685  Comments, bugs, improvements  Vim wiki

created by
eiji kumiai
 
script type
utility
 
description
This script provides several functions to do multiple-string replacements.

The quick brown fox jumped over the six lazy dogs.
replace (dogs=fox, fox=dogs, x=ck) will give
The quick brown dogs jumped over the sick lazy fox.

Note that values are non-cumulative and longest-string-first, which is why
"x=ck" does not interfere with "dogs=fox" or "fox=dogs" replacements.
This can be used to specify exceptions in a replacement list.

Typical uses:
- Replacing variable names (eg code localization / code reuse)
- Replacing keywords with translations
- Generating code from a template
- Swapping keywords
  etc

Its main function is provided  by the MultiReplace function, which performs
non-cumulative replacements, replacing occurrences of hash keys with their corresponding hash values. Hash values may be arrays, in which case values are rotated across the lines containing the key value. Range of lines to replace may also be specified.

MultiReplace(dict, from, to)

Several functions that create the "dict" hash are also provided.

1) Read values from prompt
MultiReplace(ReadUserlist(), 1,  "$")

2) Replacement dictionary
A buffer may be also specified. A replacement list buffer will contain TAB separated pairs, with the string to find on the left and the string to replace with on the right. Such a replacement would be invoked with

MultiReplace(ReadDictionary("#"), 1, "$")

Where the "#" here refers to the alternate buffer:
ReadDictionary(buffer)

3) Look up table
A buffer containing delimiter-separated values my also be used. To use a csv that does not contain quote marks (ie, no quotes and no values with linebreaks), and replace all occurrences from column 1 with values from column 4:

MultiReplace(ReadColumns("#", ",", 1, 4), 1, "$")

ReadColumns is defined as
ReadColumns(buffer, sep, keycol, valcol)

Mappings
The following mappings are included in the file. This allows you to select a range of text, enter ",rem" and then enter "=" and specify find=replace pairs for a quick replacement.

:map ,rem :call MultiReplace("",1,"$")
:vmap ,rem :call MultiReplace("",line("'<"),line("'>"))




 
install details
Insert contents into your vimrc file.
 

rate this script Life Changing Helpful Unfulfilling 
script versions (upload new version)

Click on the package to download.

package script version date Vim version user release notes
multireplace.vim 1.1 2010-02-04 7.0 eiji kumiai Fixed problem with "N" becoming "n" when in ignorecase mode.
multireplace.vim 1.0 2010-01-21 7.0 eiji kumiai Initial upload
ip used for rating: 13.58.112.1

If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
   
Vim at Github