Skip to content

Advanced Usage

Pretty print JSON without starting the interactive mode:

sh
echo '{"name": "world"}' | fx .
echo '{"name": "world"}' | fx .

Syntactic Sugar

Mapping is a frequently used process. To perform mapping, you can use the following command

sh
curl https://fx.wtf/issues.json | fx '.map(x => x.title)'
curl https://fx.wtf/issues.json | fx '.map(x => x.title)'

Or you can use the syntactic sugar:

sh
curl https://fx.wtf/issues.json | fx '.[].title'
curl https://fx.wtf/issues.json | fx '.[].title'

The [] operator is a shorthand for .flatMap(x => x).

It is useful when you need to flatten the array:

sh
curl https://fx.wtf/issues.json | fx '.[].labels[].name'
curl https://fx.wtf/issues.json | fx '.[].labels[].name'

TIP

Fx also provides even shorter syntax for mapping:

sh
curl https://fx.wtf/issues.json | fx @.title
curl https://fx.wtf/issues.json | fx @.title

The @ operator is a shorthand for .map(x => x). One benefit of using @ instead of .[] is that @ character does not require quotes in bash/zsh.

Modifying Input

Use spread operator to modify the input:

sh
echo '{"count": 1}' | fx '{...x, count: x.count + 1}'
echo '{"count": 1}' | fx '{...x, count: x.count + 1}'

Or to modify nested objects, use comma , operator:

sh
curl https://api.github.com/repos/golang/go | fx '.owner.login = "me", x'
curl https://api.github.com/repos/golang/go | fx '.owner.login = "me", x'

INFO

The , operator is a JavaScript feature that evaluates each of its operands (from left to right) and returns the value of the last operand.

Fetching Data

If passed function returns a promise, fx will wait for it to resolve:

sh
echo '"https://medv.io/*"' | fx 'fetch(x)' '.text()'
echo '"https://medv.io/*"' | fx 'fetch(x)' '.text()'

Non-JSON Data

For non-JSON content, use the --raw or -r flag:

sh
ls | fx -r '[x, x.includes(".md")]'
ls | fx -r '[x, x.includes(".md")]'

Combine --raw and --slurp (or -rs) to process as a single string array:

sh
ls | fx -rs '.filter(x => x.includes(".md"))'
ls | fx -rs '.filter(x => x.includes(".md"))'

Utilize the skip symbol in fx to skip printing certain results:

sh
ls | fx -r '.includes(".md") ? this : skip'
ls | fx -r '.includes(".md") ? this : skip'

.fxrc.js

fx can recognize the .fxrc.js file, either in the current directory or home directory.

Here's how you can define custom functions:

js
global.addOne = x => x + 1
global.addOne = x => x + 1

Invoke your custom function in fx:

sh
echo '1' | fx addOne
echo '1' | fx addOne

Global Packages

In order to require globally installed packages via Node.js (npm i -g lodash), you need to set the NODE_PATH environment variable.

Add the following line to your .bashrc or .zshrc:

sh
export NODE_PATH=$(npm root -g)
export NODE_PATH=$(npm root -g)

Now you can require globally installed packages:

js
global._ = require('lodash')
global._ = require('lodash')

Themes

fx supports themes. To use a theme, set the FX_THEME environment variable:

sh
export FX_THEME=3
export FX_THEME=3

To see all available themes, run:

sh
fx --themes
fx --themes

You can contribute your own theme by adding it to the theme.go file.