sponsor Vim development Vim logo Vim Book Ad

EditSimilar : Commands to edit files with a similar filename.

 script karma  Rating 32/10, Downloaded by 1495    Comments, bugs, improvements  Vim wiki

created by
Ingo Karkat
script type
Files edited in Vim often relate to one another; maybe they just differ in
file extensions, are numbered sequentially, or contain common patterns. One
can use the built-in cmdline-completion or filename-modifiers like %:r to
quickly edit a similar file, or even use special plugins, e.g. to alternate
between source and header files (vimscript #31).

This plugin provides custom versions of the :edit, :view, :split, :vsplit,
:sview, :file, :write and :saveas commands which facilitate quick and simple
editing of similarly named files. To quickly edit another file based on the
current file, one can:
- substitute {old} with {new}
- go to previous/next numbered file or add any offset to the number
- go to succeeding / preceding files in the same directory
- change the file extension
To open a set of similar files, it is possible to:
- open all files matching a pattern in split windows

- The PatternsOnText.vim plugin (vimscript #4602) applies the
  {text}={replacement} of :EditSubstitute via :substitute to the text in the
  buffer with :SubstituteWildcard.

- altr (vimscript #4202) lets you set up custom (per-filetype) rules and then
  opens related files through two forward / backward mappings, e.g. allowing
  you to open the autoload file from the plugin.
- nextfile (vimscript #4698) has definitions of related files (like Rails
  controllers, views, model, tests), and can edit a next file via a mapping.
- projectile (https://github.com/tpope/vim-projectile) allows you to define
  per-project settings and navigation commands, e.g. :Eplugin and :Edoc.

Substitutions: Change all occurrences via {text}={replacement} in the
currently edited file (modeled after the Korn shell's "cd {old} {new}"
command). This can also be achieved with the built-in filename-modifiers:
    :edit %:gs?pattern?replacement?
but the syntax is difficult to memorize (it's subtly different from :s) and
harder to type (because one has to use regular expressions instead of the
simpler file wildcards).

:EditSubstitute[!] {text}={replacement} [{text}={replacement} [...]]
:ViewSubstitute[!] {text}={replacement} [{text}={replacement} [...]]
:SplitSubstitute[!] {text}={replacement} [{text}={replacement} [...]]
:VSplitSubstitute[!] {text}={replacement} [{text}={replacement} [...]]
:SViewSubstitute[!] {text}={replacement} [{text}={replacement} [...]]
:DiffSplitSubstitute[!] {text}={replacement} [{text}={replacement} [...]]
:FileSubstitute {text}={replacement} [{text}={replacement} [...]]
:[range]WriteSubstitute[!] {text}={replacement} [{text}={replacement} [...]]
:SaveSubstitute[!] {text}={replacement} [{text}={replacement} [...]]
:BDeleteSubstitute[!] {text}={replacement} [{text}={replacement} [...]]

Plus / Minus: Add an offset to the last (decimal) number in the currently
edited file.
:[N]EditPlus[!] [N]
:[N]EditMinus[!] [N]

Next / Previous: In the directory listing of the current file, go to
succeeding / preceding file entries.
:[N]EditNext [{filelist}]
:[N]EditPrevious [{filelist}]

Root: Change the file extension in the currently edited file.
This is an enhanced version of the built-in:
    :edit %:r.{extension}

:EditRoot[!] {extension}

Pattern: Open all files matching the pattern in split windows, similar to how
:argadd {name} adds all matching files to the argument list.

:SplitPattern [++opt] [+cmd] {file-pattern} [{file-pattern} ...]
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 EditSimilar*.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.019 or
- Optional, recommended: cmdalias plugin (vimscript #746)

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

All these edit commands are about speed; after all, they vie to be a faster
alternative to the built-in commands that take a complete filename. Each
user's Vim setup and behavior is different. Therefore, the previously defined
short command forms :Esubst, :Enext, etc. are gone in version 1.20. Instead,
you are encouraged to define your own shortcuts, depending on your preferences
and needs. A great way to do this (because it allows the definition of pure
lowercase commands) is defining short aliases through the cmdalias plugin
(vimscript #746), like this:
    " Shorten the most frequently used commands from EditSimilar.vim.
    Alias es  EditSubstitute
    Alias sps SplitSubstitute
    Alias epl EditPlus
    Alias emi EditMinus
    Alias en  EditNext
    Alias ep  EditPrevious
    Alias er  EditRoot
    Alias spr SplitRoot
    Alias spp SplitPattern

All :Split..., :VSplit..., :SView, and :DiffSplit... commands obey the default
'splitbelow' and 'splitright' settings. If you want different behavior, you
can insert the appropriate split modifier command via:
    let g:EditSimilar_splitmode = 'rightbelow'
    let g:EditSimilar_vsplitmode = 'rightbelow'
    let g:EditSimilar_diffsplitmode = 'rightbelow'

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
EditSimilar-2.41.vmb.gz 2.41 2014-06-20 7.0 Ingo Karkat - BUG: :{range}WritePlus 999 doesn't actually work, because it executes as 999,999WriteOverBuffer.
- Refactoring: Use ingo#fs#path#Exists(). *** You need to update to ingo-library (vimscript #4433) version 1.019! ***
EditSimilar-2.40.vmb.gz 2.40 2014-04-16 7.0 Ingo Karkat - Add :BDelete... comands, which are especially useful for when "E139: File is loaded in another buffer" is given.
- Add :DiffSplit... comands.
- For next files, escape the dirspec for wildcards to handle peculiar directories.
- Allow to :write partial buffer contents by defining -range=% on :Write... commands that do not yet use the count.
- Add :SaveOverBufferAs and :WriteOverBuffer commands (that with [!] also :bdelete an existing buffer with the same name) and use those in the :Save... and :Write... commands.
- All commands now properly abort on error. *** You need to update to ingo-library (vimscript #4433) version 1.018! ***
EditSimilar-2.32.vmb.gz 2.32 2014-03-13 7.0 Ingo Karkat - Handle dot prefixes (e.g. ".txt") in root completion.
- Also offer multi-extension roots (e.g. ".orig.txt") in root completion, and correctly handle existing roots (e.g. ".orig.t").
- Add workaround for editing via :pedit, which uses the CWD of existing preview window instead of the CWD of the current window; leading to wrong not-existing files being opened when :set autochdir. Work around this by always passing a full absolute filespec. *** You need to update to ingo-library (vimscript #4433) version 1.017! ***
EditSimilar-2.31.vmb.gz 2.31 2013-11-19 7.0 Ingo Karkat - Minor: Also handle :echoerr errors, which don't have an E... number prepended.
- FIX: Non-any completion can yield duplicate roots, too (e.g. foobar.orig.txt + foobar.txt).
- Add dependency to ingo-library (vimscript #4433). *** You need to separately install ingo-library (vimscript #4433) version 1.014 (or higher)! ***
EditSimilar-2.30.vmb.gz 2.30 2012-12-09 7.0 Ingo Karkat CHG: For :FilePlus, :WritePlus, :SavePlus, when a [count] but no [!] is given, try to create an offset one more than an existing file between the current and the passed offset. This lets you use a large [N] to write the file with the next number within [N] for which no file exists yet. Change inspired by
EditSimilar-2.20.vmb.gz 2.20 2012-08-28 7.0 Ingo Karkat - Allow passing of multiple {file-pattern} to :SplitPattern et al. and enable file completion for them.
- Handle optional ++opt +cmd file options and commands in :SplitPattern et al.
EditSimilar.vba.gz 2.10 2012-07-26 7.0 Ingo Karkat ENH: Complete file extensions for any files found in the file's directory for those commands that most of the time are used to create new files; the default search for the current filename's extensions won't yield anything there.
EditSimilar.vba.gz 2.01 2012-06-15 7.0 Ingo Karkat FIX: To avoid issues with differing forward slash / backslash path separator components, canonicalize the glob pattern and filespec. This avoids a "Cannot locate current file" error when there is a mismatch.
EditSimilar.vba.gz 2.00 2012-06-11 7.0 Ingo Karkat - Rename the :EditNext / :EditPrevious commands to :EditPlus / :EditMinus and redefine them to operate on directory contents instead of numerical offsets. *** PLEASE USE THE NEW RENAMED COMMANDS AND UPDATE ANY USAGES IN SCRIPTS AND MAPPINGS ***
- Better modularization of the different similarities.
- BUG: Substituted filenames that only exist in an unpersisted Vim buffer cause a "file does not exist" error when a:isCreateNew isn't set. Also check Vim buffers for a match.
EditSimilar.vba.gz 1.22 2012-02-10 7.0 Ingo Karkat - ENH: Allow [v]split mode different than determined by 'splitbelow' / 'splitright' via configuration.
- Refactoring: Move file extension completion to EditSimilar#Root#Complete() and create the root commands also in the command builder.
- ENH: Omit current buffer's file extension from the completion for EditSimilar-root commands.
- Obsolete the short command forms :Esubst, :Enext, :Eprev; the starting uppercase letter makes them still awkward to type, there's more likely a conflict with other custom commands (e.g. :En -> :Encode, :Enext), and I now believe aliasing via cmdalias.vim is the better way to provide personal shortcuts, instead of polluting the command namespace with all these duplicates.
- Rename :Vsplit... -> :VSplit... and :Sview... -> :SView... as I think this is a more intuitive long form. (And now that the user is encouraged to create his own custom short aliases, anyway.) The only other plugin with similar commands that I know is bufexplorer with its :VSBufExplorer.
EditSimilar.vba.gz 1.19 2011-07-25 7.0 Ingo Karkat Avoid that :SplitPattern usually opens splits in reverse glob order (with default 'nosplitbelow' / 'nosplitright') by forcing :belowright splitting for all splits after the first. I.e. behave more like vim -o {pattern}.
EditSimilar.vba.gz 1.18 2011-06-23 7.0 Ingo Karkat ENH: Implement completion of file extensions for EditSimilar-root commands like :EditRoot.
EditSimilar.vba.gz 1.17 2010-02-25 7.0 Ingo Karkat BUG: :999EditPrevious on 'file00' caused E121: Undefined variable: l:replacement.
EditSimilar.vba.gz 1.16 2009-11-11 7.0 Ingo Karkat BUG: Next / previous commands interpreted files such as 'C406' as hexadecimal. Thanks to Andy Wokula for sending a patch.
EditSimilar.vba.gz 1.15 2009-09-09 7.0 Ingo Karkat Offset commands (:EditNext et al.) now check that the digit pattern does not
accidentally match inside a hexadecimal number (which are unsupported).
EditSimilar.vba.gz 1.14 2009-08-21 7.0 Ingo Karkat - BF: :[N]EditPrevious with supplied [N] would skip over existing smaller number file and would claim that no substituted file existed.
- BF: :[N]EditPrevious with supplied large [N] together with a low original number hogs the CPU because the loop iterates over the entire number range where the resulting offset would be negative.
EditSimilar.vba.gz 1.13 2009-06-30 7.0 Ingo Karkat ENH: :EditNext / :EditPrevious without the optional [count] now skip over gaps
in numbering.
EditSimilar.vba.gz 1.12 2009-05-13 7.0 Ingo Karkat - ENH: {text} in :EditSubstitute can now also contain file wildcards (?, *, **
  and [...]) to save typing.
- ENH: On Windows, {text} in :EditSubstitute can now also use forward slashes
  as path separators (as an alternative to the usual backslashes).
- ENH: Supporting substitutions spanning both pathspec and filename by finally
  applying failed replacements of multi-path elements to the entire filespec.
- Added ":ViewSimilar" and ":SviewSimilar" commands to open similar files in
  read-only mode.
EditSimilar.vba.gz 1.10 2009-02-24 7.0 Ingo Karkat ENH: {replacement} in :EditSubstitute and {extension} in :EditRoot can now
contain file wildcards to save typing.
EditSimilar.vba.gz 1.00 2009-02-18 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