sponsor Vim development Vim logo Vim Book Ad

ReplaceWithSameIndentRegister : Replace lines with the contents of a register, keeping the original indent.

 script karma  Rating 7/4, Downloaded by 688  Comments, bugs, improvements  Vim wiki

created by
Ingo Karkat
script type
Replacing existing lines with the contents of a register is a very common task
during editing. Often, one wants to adapt the indent of the pasted lines to
the current indent, e.g. when replacing a nested conditional from a yank that
originated at a different nesting level.

This plugin combines the replacement functionality of the
ReplaceWithRegister.vim plugin with the indent-adjustment done by the
built-in ]p command. With a simple mapping, you can replace line(s) with the
contents of a register while adjusting the indent to that of the first
replaced line.

- ReplaceWithRegister.vim (vimscript #2703) implements the general case of
  replacing text including the indent (also with a {motion} mapping for
  arbitrary text, not just lines).

[count]["x]grR          Replace [count] lines with the contents of register x
                        and adjust the indent to the first replaced line (like
                        pasting with ]p).
{Visual}["x]gR          Replace the selection with the contents of register x
                        and adjust the indent to the first selected line.
                        As the replacement is always entire lines, this only
                        works with linewise selections (V).
install details
The code is hosted in a Git repo at
You can use your favorite plugin manager, or "git clone" into a directory used
for Vim packages. Releases are on the "stable" branch, the latest unstable
development snapshot on "master".

This script is also 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 ReplaceWithSameIndentRegister*.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.000 or
- repeat.vim (vimscript #2136) plugin (optional)
  To support repetition with a register other than the default register, you
  need version 1.1 or later.
- visualrepeat.vim (vimscript #3848) plugin (version 2.00 or higher; optional)

If you want to use different mappings, map your keys to the
<Plug>ReplaceWithSameIndentRegister... mapping targets _before_ sourcing the
script (e.g. in your vimrc):
    nmap <Leader>R  <Plug>ReplaceWithSameIndentRegisterLine
    xmap <Leader>R  <Plug>ReplaceWithSameIndentRegisterVisual

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
ReplaceWithSameIndentRegister-1.01.vmb.gz 1.01 2019-11-19 7.0 Ingo Karkat - BUG: {count}grR does not repeat the count.
- BUG: Starting with v_gR, repeating in normal mode with {count}., repeating again uses the original number of selected lines, not the overridden {count}.
- Suppress "--No lines in buffer--" message when replacing the entire buffer, and combine "Deleted N lines" / "Added M lines" into a single message that is given when either previous or new amount of lines reaches 'report'.
ReplaceWithSameIndentRegister-1.00.vmb.gz 1.00 2014-10-29 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.