{"id":13405636,"url":"https://github.com/jlevy/repren","last_synced_at":"2025-04-05T00:10:51.925Z","repository":{"id":18514597,"uuid":"21711622","full_name":"jlevy/repren","owner":"jlevy","description":"Rename anything","archived":false,"fork":false,"pushed_at":"2022-06-27T21:34:29.000Z","size":114,"stargazers_count":347,"open_issues_count":19,"forks_count":39,"subscribers_count":13,"default_branch":"master","last_synced_at":"2024-10-19T01:48:22.614Z","etag":null,"topics":["command-line","python","refactoring","regular-expression","renames","search-and-replace"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jlevy.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-07-10T21:51:51.000Z","updated_at":"2024-09-14T17:38:30.000Z","dependencies_parsed_at":"2022-07-30T14:19:59.871Z","dependency_job_id":null,"html_url":"https://github.com/jlevy/repren","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jlevy%2Frepren","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jlevy%2Frepren/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jlevy%2Frepren/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jlevy%2Frepren/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jlevy","download_url":"https://codeload.github.com/jlevy/repren/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247266565,"owners_count":20910836,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["command-line","python","refactoring","regular-expression","renames","search-and-replace"],"created_at":"2024-07-30T19:02:07.106Z","updated_at":"2025-04-05T00:10:51.905Z","avatar_url":"https://github.com/jlevy.png","language":"Python","readme":"# repren\n\n![But call me repren for short](https://github.com/jlevy/repren/blob/master/images/awkward-150.jpg)\n\n---\n\n✨ **NEW:** [v1.0](https://github.com/jlevy/repren/releases/tag/1.0.1) is now updated for Python 3.10-3.13! ✨\n\n---\n\n\n## Rename Anything\n\nRepren is a simple but flexible command-line tool for rewriting file contents according to a\nset of regular expression patterns, and to rename or move files according to patterns.\nEssentially, it is a general-purpose, brute-force text file refactoring tool.\n\nFor example, repren could rename all occurrences of certain class and variable names in a\nset of Java source files, while simultaneously renaming the Java files according to the same\npattern.\n\nIt's more powerful than usual options like `perl -pie`, `rpl`, or `sed`:\n\n- It can also rename files, including moving files and creating directories.\n\n- It supports fully expressive regular expression substitutions.\n\n- It performs group renamings, i.e. rename \"foo\" as \"bar\", and \"bar\" as \"foo\" at once,\n  without requiring a temporary intermediate rename.\n\n- It is careful. It has a nondestructive mode, and prints clear stats on its changes.\n  It leaves backups.\n  File operations are done atomically, so interruptions never leave a previously existing\n  file truncated or partly edited.\n\n- It supports \"magic\" case-preserving renames that let you find and rename identifiers with\n  case variants (lowerCamel, UpperCamel, lower_underscore, and UPPER_UNDERSCORE)\n  consistently.\n\n- It has this nice documentation!\n\nIf file paths are provided, repren replaces those files in place, leaving a backup with\nextension \".orig\".\n\nIf directory paths are provided, it applies replacements recursively to all files in the\nsupplied paths that are not in the exclude pattern.\nIf no arguments are supplied, it reads from stdin and writes to stdout.\n\n## Examples\n\nPatterns can be supplied in a text file, with one or more replacements consisting of regular\nexpression and replacement.\nFor example:\n\n```\n# Sample pattern file\nfrobinator\u003ctab\u003eglurp\nWhizzleStick\u003ctab\u003eAcmeExtrudedPlasticFunProvider\nfigure ([0-9+])\u003ctab\u003eFigure \\1\n```\n\n(Where `\u003ctab\u003e` is an actual tab character.)\nEach line is a replacement.\nEmpty lines and #-prefixed comments are ignored.\n\nAs a short-cut, a single replacement can be specified on the command line using `--from`\n(match) and `--to` (replacement).\n\nExamples:\n\n```bash\n# Here `patfile` is a patterns file.\n# Rewrite stdin:\nrepren -p patfile \u003c input \u003e output\n\n# Shortcut with a single pattern replacement (replace foo with bar):\nrepren --from foo --to bar \u003c input \u003e output\n\n# Rewrite a few files in place, also requiring matches be on word breaks:\nrepren -p patfile --word-breaks myfile1 myfile2 myfile3\n\n# Rewrite whole directory trees. Since this is a big operation, we use\n# `-n` to do a dry run that only prints what would be done:\nrepren -n -p patfile --word-breaks --full mydir1\n\n# Now actually do it:\nrepren -p patfile --word-breaks --full mydir1\n\n# Same as above, for all case variants:\nrepren -p patfile --word-breaks --preserve-case --full mydir1\n\n# Same as above but including only .py files and excluding the tests directory\n# and any files or directories starting with test_:\nrepren -p patfile --word-breaks --preserve-case --full --include='.*[.]py$' --exclude='tests|test_.*' mydir1\n```\n\n## Usage\n\nRun `repren --help` for full usage and flags.\n\nIf file paths are provided, repren replaces those files in place, leaving a backup with\nextension \".orig\".\nIf directory paths are provided, it applies replacements recursively to all files in the\nsupplied paths that are not in the exclude pattern.\nIf no arguments are supplied, it reads from stdin and writes to stdout.\n\n## Alternatives\n\nAren't there standard tools for this already?\n\nIt's a bit surprising, but not really.\nGetting the features right is a bit tricky, I guess.\nThe\n[standard](http://stackoverflow.com/questions/11392478/how-to-replace-a-string-in-multiple-files-in-linux-command-line/29191549)\n[answers](http://stackoverflow.com/questions/6840332/rename-multiple-files-by-replacing-a-particular-pattern-in-the-filenames-using-a)\nlike *sed*, *perl*, *awk*, *rename*, *Vim* macros, or even IDE refactoring tools, often\ncover specific cases, but tend to be error-prone or not offer specific features you probably\nwant.\nThings like nondestructive mode, file renaming as well as search/replace, multiple\nsimultaneous renames/swaps, or renaming enclosing parent directories.\nAlso many of these vary by platform, which adds to the corner cases.\nInevitably you end up digging through the darker corners of some man page or writing ugly\nscripts that would scare your mother.\n\n## Installation\n\nNo dependencies except Python 3.10+. It's easiest to install with pip:\n\n```bash\npip install repren\n```\n\nOr, since it's just one file, you can copy the\n[repren.py](https://raw.githubusercontent.com/jlevy/repren/master/repren/repren.py) script\nsomewhere convenient and make it executable.\n\n## Try It\n\nLet's try a simple replacement in my working directory (which has a few random source\nfiles):\n\n```bash\n$ repren --from frobinator-server --to glurp-server --full --dry-run .\nDry run: No files will be changed\nUsing 1 patterns:\n  'frobinator-server' -\u003e 'glurp-server'\nFound 102 files in: .\n- modify: ./site.yml: 1 matches\n- rename: ./roles/frobinator-server/defaults/main.yml -\u003e ./roles/glurp-server/defaults/main.yml\n- rename: ./roles/frobinator-server/files/deploy-frobinator-server.sh -\u003e ./roles/glurp-server/files/deploy-frobinator-server.sh\n- rename: ./roles/frobinator-server/files/install-graphviz.sh -\u003e ./roles/glurp-server/files/install-graphviz.sh\n- rename: ./roles/frobinator-server/files/frobinator-purge-old-deployments -\u003e ./roles/glurp-server/files/frobinator-purge-old-deployments\n- rename: ./roles/frobinator-server/handlers/main.yml -\u003e ./roles/glurp-server/handlers/main.yml\n- rename: ./roles/frobinator-server/tasks/main.yml -\u003e ./roles/glurp-server/tasks/main.yml\n- rename: ./roles/frobinator-server/templates/frobinator-webservice.conf.j2 -\u003e ./roles/glurp-server/templates/frobinator-webservice.conf.j2\n- rename: ./roles/frobinator-server/templates/frobinator-webui.conf.j2 -\u003e ./roles/glurp-server/templates/frobinator-webui.conf.j2\nRead 102 files (190382 chars), found 2 matches (0 skipped due to overlaps)\nDry run: Would have changed 2 files, including 0 renames\n```\n\nThat was a dry run, so if it looks good, it's easy to repeat that a second time, dropping\nthe `--dry-run` flag.\nIf this is in git, we'd do a git diff to verify, test, then commit it all.\nIf we messed up, there are still .orig files present.\n\n## Patterns\n\nPatterns can be supplied using the `--from` and `--to` syntax above, but that only works for\na single pattern.\n\nIn general, you can perform multiple simultaneous replacements by putting them in a\n*patterns file*. Each line consists of a regular expression and replacement.\nFor example:\n\n```\n# Sample pattern file\nfrobinator\u003ctab\u003eglurp\nWhizzleStick\u003ctab\u003eAcmeExtrudedPlasticFunProvider\nfigure ([0-9+])\u003ctab\u003eFigure \u0001\n```\n\n(Where `\u003ctab\u003e` is an actual tab character.)\n\nEmpty lines and #-prefixed comments are ignored.\nCapturing groups and back substitutions (such as \u0001 above) are supported.\n\n## Examples\n\n```\n# Here `patfile` is a patterns file.\n# Rewrite stdin:\nrepren -p patfile \u003c input \u003e output\n\n# Shortcut with a single pattern replacement (replace foo with bar):\nrepren --from foo --to bar \u003c input \u003e output\n\n# Rewrite a few files in place, also requiring matches be on word breaks:\nrepren -p patfile --word-breaks myfile1 myfile2 myfile3\n\n# Rewrite whole directory trees. Since this is a big operation, we use\n# `-n` to do a dry run that only prints what would be done:\nrepren -n -p patfile --word-breaks --full mydir1\n\n# Now actually do it:\nrepren -p patfile --word-breaks --full mydir1\n\n# Same as above, for all case variants:\nrepren -p patfile --word-breaks --preserve-case --full mydir1\n```\n\n## Notes\n\n- As with sed, replacements are made line by line by default.\n  Memory permitting, replacements may be done on entire files using `--at-once`.\n\n- As with sed, replacement text may include backreferences to groups within the regular\n  expression, using the usual syntax: \\1, \\2, etc.\n\n- In the pattern file, both the regular expression and the replacement may contain the usual\n  escapes `\\n`, `\\t`, etc.\n  (To match a multi-line pattern, containing `\\n`, you must must use `--at-once`.)\n\n- Replacements are all matched on each input file, then all replaced, so it's possible to\n  swap or otherwise change names in ways that would require multiple steps if done one\n  replacement at at a time.\n\n- If two patterns have matches that overlap, only one replacement is applied, with\n  preference to the pattern appearing first in the patterns file.\n\n- If one pattern is a subset of another, consider if `--word-breaks` will help.\n\n- If patterns have special characters, `--literal` may help.\n\n- The case-preserving option works by adding all case variants to the pattern replacements,\n  e.g. if the pattern file has foo_bar -\u003e xxx_yyy, the replacements fooBar -\u003e xxxYyy, FooBar\n  -\u003e XxxYyy, FOO_BAR -\u003e XXX_YYY are also made.\n  Assumes each pattern has one casing convention.\n\n- The same logic applies to filenames, with patterns applied to the full file path with\n  slashes replaced and then and parent directories created as needed, e.g.\n  `my/path/to/filename` can be rewritten to `my/other/path/to/otherfile`. (Use caution and\n  test with `-n`, especially when using absolute path arguments!)\n\n- Files are never clobbered by renames.\n  If a target already exists, or multiple files are renamed to the same target, numeric\n  suffixes will be added to make the files distinct (\".1\", \".2\", etc.).\n\n- Files are created at a temporary location, then renamed, so original files are left intact\n  in case of unexpected errors.\n  File permissions are preserved.\n\n- Backups are created of all modified files, with the suffix \".orig\".\n\n- By default, recursive searching omits paths starting with \".\". This may be adjusted with\n  `--exclude`. Files ending in `.orig` are always ignored.\n\n- Data is handled as bytes internally, allowing it to work with any encoding or binary\n  files.\n  File contents are not decoded unless necessary (e.g., for logging).\n  However, patterns are specified as strings in the pattern file and command line arguments,\n  and file paths are handled as strings for filesystem operations.\n\n\n## Contributing\n\nContributions and issues welcome!\nCheck the output of the test script and if it has changed or needs updating,\nand commit the clean log changes if you submit a PR.\n\n## License\n\nMIT.","funding_links":[],"categories":["Python","For Developers","python"],"sub_categories":["Directory Navigation"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjlevy%2Frepren","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjlevy%2Frepren","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjlevy%2Frepren/lists"}