sponsor Vim development Vim logo go to HTTPS page Vim Book Ad

ChangeGlobally : Change {motion} text and repeat the substitution.

 script karma  Rating 5/2, Downloaded by 510    Comments, bugs, improvements  Vim wiki

created by
Ingo Karkat
script type
Changing existing text is one of the main editing tasks. In Vim, there are two
approaches: Either use the c and s commands, then quit insert mode; maybe
repeat this via . at another location. Or build a :substitute command for
controlled replacement in the line, range, or buffer.
This plugin implements a hybrid of these two contrasting approaches: It offers
a gc command that works just like built-in c, and after leaving insert
mode applies the local substitution to all other occurrences in the current
line (in case of a small character change) or, when entire line(s) were
changed, to the rest of the buffer.

The gc command hooks itself into the InsertLeave event, then applies
something like :s/\=@"/\=@./g to the line or buffer.

- ReplaceWithRegister (vimscript #2703) simplifies another frequent editing
  task: Replace the selection with the contents of register.
- ChangeGloballySmartCase (vimscript #4322) is an add-on to this plugin that
  implements a gC variant that uses a "smart case" substitution which covers
  variations in upper-/lowercase ("maxSize" vs. "MaxSize") as well as
  different coding styles like CamelCase and underscore_notation ("maxSize",

- multichange.vim (vimscript #4309) uses a command :[range]MultiChange to
  modify the default c command to do an entire-word global substitution in the
  entire range.

[N]["x]gc{motion}       Delete {motion} text [into register x] and start
{Visual}[N]["x]gc       Delete the highlighted text [into register x] and
                        start inserting.
                        After exiting insert mode, the substitution is
                        - For characterwise motions / selections: Globally to
                          the changed line if possible, or globally in the
                          entire buffer when no additional substitution can be
                          made in the changed line or a very large [N] is
                          given g:ChangeGlobally_GlobalCountThreshold.
                        - For linewise motions / selections: Globally (for
                          identical complete lines) in the entire buffer.
                        - [N] times (including the just completed change, so
                          only N > 1 really makes sense), starting from the
                          position of the changed text.
                          Note: A possible [count] inside {motion} is
                          different from [N]; e.g., 2gc3w changes 3 words, and
                          then applies this change 1 more time.

                        The substitution is always done case-sensitive,
                        regardless of the 'ignorecase' setting.
                        When the changed text is surrounded by keyword
                        boundaries (/\<text\>/), only keyword matches are
                        replaced so spurious matches inside keywords (e.g.
                        "IN" inside "rINg") are ignored. This does not apply
                        to visual selections.

["x]gcc                 Delete [count] lines [into register x] and start
                        insert linewise. If 'autoindent' is on, preserve the
                        indent of the first line. After exiting insert mode,
                        the substitution is applied globally.

                        When a command is repeated via ., the previous
                        substitution (without entering a new insert mode) is
                        re-applied to the visual selection, [count] next
                        lines, or the range of the previous substitution.
                        For a command that used a limit [N], the number of
                        substitutions and the start column from where they
                        were applied are kept.

                        With the visualrepeat.vim plugin, commands can be
                        repeated on a blockwise-visual selection. In that
                        - A repeat of gcc matches not only complete lines
                          (they probably aren't fully selected), just the text
                        - Start column and [N] limit restrictions are dropped;
                          the change is applied anywhere inside the selected
                        All these modifications are kept for subsequent
                        repeats of the repeat, so the blockwise repeat can be
                        cleverly employed to remove certain change
                        restrictions for following repeats of different kinds.

[N]["x]gx{motion}       Delete {motion} text [into register x] and apply the
{Visual}[N]["x]gx       Delete the highlighted text [into register x] and
                        apply the deletion, like with gc.
["x]gxx                 Delete [count] lines [into register x] and apply the
                        deletion globally.

Suppose you have a line like this, and you want to change "de" to "en":
    A[lang=de]:after, SPAN[lang=de]:after { content: url("lang.de.gif"); }

With the cursor on the start of any of the "de", type gce, enter the text
"en", then press <Esc>. The line will turn into
    A[lang=en]:after, SPAN[lang=en]:after { content: url("lang.en.gif"); }
You can now re-apply this substitution to other lines or a visual selection
via .
install details
This script is packaged as a vimball. If you have the "gunzip" decompressor
in your PATH, simply edit the *.vmb.gz package in Vim; otherwise, decompress
the archive first, e.g. using WinZip. Inside Vim, install by sourcing the
vimball or via the :UseVimball command.
    vim ChangeGlobally*.vmb.gz
    :so %
To uninstall, use the :RmVimball command.

- Requires Vim 7.0 or higher.
- Requires the ingo-library.vim plugin (vimscript #4433), version 1.011 or
- repeat.vim (vimscript #2136) plugin (optional)
- visualrepeat.vim (vimscript #3848) plugin (version 2.00 or higher; optional)

For a permanent configuration, put the following commands into your vimrc:

To apply a characterwise substitution globally in the entire buffer even when
there are additional substitutions in the current line, a very large [count]
can be supplied. To change the threshold, use:
    let g:ChangeGlobally_GlobalCountThreshold = 999
To turn off this feature, set the threshold to 0.

As it's sometimes not easy to quickly count the number of occurrences to
replace, or particular occurrences have to be skipped, the special count value
of 888 makes the commands switch to confirm each replacement (via the :s_c
flag). You can change this value or turn off this feature by setting it to 0:
    let g:ChangeGlobally_ConfirmCount = 0

If you want to use different mappings, map your keys to the
<Plug>(ChangeGlobally...) and <Plug>(DeleteGlobally...) mapping targets
_before_ sourcing the script (e.g. in your vimrc):
    nmap <Leader>c <Plug>(ChangeGloballyOperator)
    nmap <Leader>cc <Plug>(ChangeGloballyLine)
    xmap <Leader>c <Plug>(ChangeGloballyVisual)
    nmap <Leader>x <Plug>(DeleteGloballyOperator)
    nmap <Leader>xx <Plug>(DeleteGloballyLine)
    xmap <Leader>x <Plug>(DeleteGloballyVisual)

Rating scripts is only available on the HTTPS page

script versions (upload new version)

Click on the package to download.

package script version date Vim version user release notes
ChangeGlobally-1.30.vmb.gz 1.30 2014-12-12 7.0 Ingo Karkat - ENH: Implement global delete (gx{motion}, gxx) as a specialization of an empty change.
ChangeGlobally-1.21.vmb.gz 1.21 2014-04-25 7.0 Ingo Karkat - FIX: Disable global substitution when g:ChangeGlobally_GlobalCountThreshold is 0, as is documented.
- ENH: Confirm each replacement via :s_c flag when a special g:ChangeGlobally_ConfirmCount is given.
ChangeGlobally-1.20.vmb.gz 1.20 2013-11-19 7.0 Ingo Karkat - ENH: Special handling for repeat on blockwise selection (through visualrepeat.vim) that makes more sense.
- Autocmds may interfere with the plugin when they temporarily leave insert mode (i_CTRL-O) or create an undo point (i_CTRL-G_u). Disable them until the user is done inserting.
- Use optional visualrepeat#reapply#VisualMode() for normal mode repeat of a visual mapping. When supplying a [count] on such repeat of a previous linewise selection, now [count] number of lines instead of [count] times the original selection is used.
- Avoid changing the jumplist.
- ENH: Handle undo points created during insertion: Undo the whole insertion sequence (by using :undo with the original change number) and substitute the entire captured insertion, not just the last part, by detecting a multi-change insert and using the original start position instead of the start change mark.
- Add dependency to ingo-library (vimscript #4433). *** You need to separately install ingo-library (vimscript #4433) version 1.011 (or higher)! ***
ChangeGlobally-1.01.vmb.gz 1.01 2013-01-19 7.0 Ingo Karkat BUG: Linewise changes (gcc) causes beep instead of substitution.
ChangeGlobally-1.00.vmb.gz 1.00 2012-11-23 7.0 Ingo Karkat Initial upload
ip used for rating:

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.
SourceForge.net Logo