Comby

Comby

  • Get started
  • Docs
  • Projects & Talks
  • GitHub
  • Blog

›Usage

Getting Started

  • Overview
  • Get Started

Usage

  • Basic Usage
  • Rewrite Properties
  • Syntax Reference
  • Advanced Usage
  • Configuration Files
  • Cheat Sheet

API

  • API Reference

Resources

  • Get Help
  • FAQ
Edit

Rewrite properties

Comby has convenient built-in properties to transform and substitute matched values for certain use cases that commonly crop up when rewriting code. For example, :[hole].Capitalize will capitalize a string matched by hole (if possible).

echo 'these are words 123' | comby -stdin ':[[x]]' ':[[x]].Capitalize' -lang .txt
--- /dev/null
+++ /dev/null
@@ -1,1 +1,1 @@
-these are words 123
+These Are Words 123

playground ↗

Properties are recognized in the rewrite template and substituted according to the predefined behavior. Property accesses cannot be chained. Below are the current built-in properties.

String converters

PropertyBehavior
.lowercaseConvert letters to lowercase
.UPPERCASEConvert letters to uppercase
.CapitalizeCapitalize the first character if it is a letter
.uncapitalizeLowercase the first character if it is a letter
.UPPER_SNAKE_CASEConvert camelCase to snake_case (each capital letter in camelCase gets a _ prepended). Then uppercase letters.
.lower_snake_caseConvert camelCase to snake_case (each capital letter in camelCase gets a _ prepended). Then lowercase letters.
.UpperCamelCaseConvert snake_case to CamelCase (each letter after _ in snake_case is capitalized, and the _ removed). Then capitalize the first character.
.lowerCamelCaseConvert snake_case to CamelCase (each letter after _ in snake_case is capitalized, and the _ removed). Then lowercase the first character.

playground ↗

Sizes

PropertyBehavior
.lengthSubstitute the number of characters of the hole value
.linesSubstitute the number of lines of the hole value

playground ↗

Positions

PropertyBehavior
.lineSubstitute the starting line number of this hole
.line.startAlias of .line
.line.endSubstitute the ending line number of this hole
.columnSubstitute the starting column number of this hole (also known as character)
.column.startAlias of .column
.column.endSubstitute the ending column number of this hole
.offsetSubstitute the starting byte offset of this hole in the file
.offset.startAlias of .offset
.offset.endSubstitute the ending byte offset of this hole in the file

Note: only .offset is available in the playground. .line and .column are currently only supported on the command-line, and on files (not via -stdin).

playground ↗

File context

PropertyBehavior
.fileSubstitute the absolute file path of the file where this hole matched
.file.pathAlias of .file
.file.nameSubstitute the file name of the file where this hole matched (basename)
.file.directorySubstitute the file directory of the file where hole matched (dirname)

Use on the command line to see this in action.

Identity (for escaping property names)

PropertyBehavior
.valueSubstitute the text value of this hole (for escaping, see below)

Resolving clashes with property names

Let’s say you want to literally insert the text .length after a hole. We can’t use :[hole].length because that reserved syntax will substitute the length of the match, and not insert the text .length. To resolve a clash like this, simply use :[hole].value instead of :[hole] to substitute the value of :[hole]. Then, append .length to the template. This will cause the .length, to be interpreted literally:

echo 'a word' | comby -stdin ':[[x]]' ':[x].value.length is :[x].length' -lang .txt
--- /dev/null
+++ /dev/null
@@ -1,1 +1,1 @@
-a word
+a.length is 1 word.length is 4

playground ↗

The way this works is that :[hole].value acts as an escape sequence so that any conflicting .<property> can be interpreted literally by simply appending it to :[hole].value.

← Basic UsageSyntax Reference →
  • Resolving clashes with property names

© 2022 @rvtond · Get started · Docs · Projects & Talks · Blog · Twitter