sponsor Vim development Vim logo Vim Book Ad

clone : Create a duplicate clone of the current buffer.

 script karma  Rating 18/8, Downloaded by 2314  Comments, bugs, improvements  Vim wiki

created by
Ingo Karkat
 
script type
utility
 
description
DESCRIPTION
Often, it is efficient to base a new file (e.g. unit test, meeting minutes) on
an existing one, and just apply the changes instead of re-creating the entire
file. In Vim, one can create a duplicate copy of the current buffer via
:write {newfile} or :saveas {newfile}. But the former doesn't
automatically open the cloned file, whereas with the latter, you're losing the
buffer containing the original file. Also, the identical clone is immediately
persisted, which may confuse automated build tools (or yourself, should you
get interrupted and forget to make the actual modifications). Transferring the
buffer contents via :yank is also cumbersome and clobbers a register.

This plugin offers a :CloneAs command that duplicates the current buffer (or
only parts of it) without persisting the clone right away (only on :w), and
keeping the original buffer loaded and unmodified inside Vim (with
:SCloneAs, it'll even be kept displayed in a window).

Oftentimes, duplication in source code is a code smell. Please use this plugin
responsibly ;-)

SEE ALSO
The cloneSimilar.vim plugin (vimscript #4897) adds additional command
variants powered by the EditSimilar.vim (vimscript #2544) plugin.

USAGE
:[range]CloneAs[!] {file}
                        Duplicate and edit the current buffer / specified
                        lines with a new name, keep the existing one.
                        Writes over existing buffer with [!].
:[range]SCloneAs[!] {file}
                        Duplicate and split the current buffer / specified
                        lines with a new name, keep the existing one.

                        The original cursor position / window view will be
                        kept, as well as the 'fileformat', 'fileencoding' and
                        'filetype' settings of the original buffer.
 
install details
INSTALLATION
The code is hosted in a Git repo at
    https://github.com/inkarkat/vim-clone
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 clone*.vmb.gz
    :so %
To uninstall, use the :RmVimball command.

DEPENDENCIES
- Requires Vim 7.0 or higher.
- Requires the ingo-library.vim plugin (vimscript #4433), version 1.033 or
  higher.

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

The :SCloneAs command uses the default 'splitbelow' behavior; you can
influence this via:
    let g:clone_splitmode = 'belowright'
 

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
clone-1.10.vmb.gz 1.10 2024-11-14 7.0 Ingo Karkat - ENH: Also emit custom User BufCloneFile event.
- ENH: Support [!] to overwrite an existing target buffer.  *** You need to update to ingo-library (vimscript #4433) version 1.033! ***
clone-1.02.vmb.gz 1.02 2014-06-19 7.0 Ingo Karkat - Avoid setting 'filetype' when it already has the value of the original buffer, because even that triggers FileType autocmds, and may result in disturbing duplicate messages.
- Also handle unreadable files (and directories). *** You need to update to ingo-library (vimscript #4433) version 1.019! ***
clone-1.01.vmb.gz 1.01 2014-04-25 7.0 Ingo Karkat - Allow cloning into unloaded buffer.
- Suppress BufNewFile event, and instead emit the more appropriate BufRead event for the clone buffer.
clone-1.00.vmb.gz 1.00 2014-03-18 7.0 Ingo Karkat Initial upload
ip used for rating: 142.132.191.50

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