Coder Social home page Coder Social logo

marcusbuffett / pipe-rename Goto Github PK

View Code? Open in Web Editor NEW
386.0 6.0 14.0 293 KB

Rename your files using your favorite text editor

Home Page: https://crates.io/crates/pipe-rename

License: MIT License

Rust 97.23% Python 2.77%
command-line-tool devtool rust

pipe-rename's Introduction

pipe-rename

Crates.io

pipe-rename takes a list of files as input, opens your $EDITOR of choice, then renames those files accordingly.

Installation

cargo install pipe-rename

This will install the renamer binary.

Usage

Usage is simple, just pipe a list of files into renamer. This will open your $EDITOR (or vim, if not set or passed with --editor) -- or %EDITOR% (or Notepad on Windows, if not set or passed with --editor), and once your editor exits it will detect which files were renamed:

ls | renamer

You can also supply filenames as positional arguments. To rename .txt files in the current directory:

renamer *.txt

The default behavior is to rename files, but you can override this. If you want to run git mv old new on each rename, you can do something like this:

ls | renamer --rename-command "git mv"

Help text

Takes a list of files and renames/moves them by piping them through an external editor

USAGE:
    renamer [OPTIONS] [FILES]...

ARGS:
    <FILES>...


OPTIONS:
    -c, --rename-command <COMMAND>
            Optionally set a custom rename command, like 'git mv'

    -e, --editor <EDITOR>
            Optionally set an editor, overriding EDITOR environment variable and default

    -f, --force
            Overwrite existing files

    -h, --help
            Print help information

    -n, --filenames-only
            Only rename filenames

    -p, --pretty-diff
            Prettify diffs

    -u, --undo
            Undo the previous renaming operation

    -V, --version
            Print version information

    -y, --yes
            Answer all prompts with yes

Caveat emptor

NB: it makes sense to be aware of the issues ls can cause in this context, depending on the ls flavor (or substitute, such as lsd, exa ...) used. Please read this document for more information.

While your shell will pass the file names individually, no matter if they contain whitespace, an ls that fails to detect the pipe and print one file name per line will cause issues. Unfortunately ls -Q also isn't a solution here, because unlike the shell -- which will strip quotes prior to passing them to invoked commands -- renamer won't handle the quoted names and will probably complain about non-existent files, too.

Advanced usage

If you have tools like GNU find at your disposal, you can also use the following method:

find -type f -exec renamer {} +

This would execute renamer with all of the files matched by find. You can use additional find predicates such as -name or -ipath to limit which files to rename. There is, however, one caveat: on large lists of files you may encounter multiple invocations of renamer -- and thus your editor -- due to how find ... -exec {} + works. It will pass as many file names on the command line as it can fit but it is limited by ARG_MAX (see getconf ARG_MAX output for how long the overall command line length can be on your system).

Other find flavors would allow the following, but it would invoke renamer -- and thus your editor -- once for every single found file:

find -type f -exec renamer {} \;

In order to sidestep this issue, you can employ xargs in conjunction with find like so (-print is implied for find):

find -type f | xargs renamer --editor vim

The part past xargs is the invocation of renamer without the file names. It exists just to demonstrate how you would pass arguments to renamer using this method.

If your files contain wonky characters you could also try:

find -type f -print0 | xargs -0 renamer --editor vim

Alas, this could be asking for trouble. If your file names contain line breaks, for example, this could confuse renamer which expects a single file name per line when re-reading the edited file.

Known workarounds

renamer will wait for the editor to close, before offering to rename the files according to your changes. Some editors cause issues with this method, because they spawn child processes or similar. This is a list of known workarounds for some editors. Feel free to contribute by sending a pull request or opening an issue and giving the details.

We can work around these issues by explicitly using a method to invoke the desired editor, which works with the assumptions made by renamer. It can be done by passing --editor (short -e) or setting the environment $EDITOR (%EDITOR% with cmd.exe or $env:EDITOR with pwsh.exe).

  • Sublime Text can be used by passing --editor "subl -w" to have it wait until the files are closed

Windows-specific workarounds

  • VS Code can be used by passing --editor "code.cmd -" and then giving the other files
  • VSCodium analogously can be used with --editor "codium.cmd -"

Contributors โœจ


Marcus Buffett

๐Ÿค” ๐Ÿ’ป

Robin Krahl

๐Ÿค” ๐Ÿ’ป ๐Ÿ›

Max Timkovich

๐Ÿค” ๐Ÿ’ป

Benoit de Chezelles

๐Ÿค”

Oliver Schneider

๐Ÿค” ๐Ÿ’ป

This project follows the all-contributors specification. Contributions of any kind welcome!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.