Skip to content
Nev Delap edited this page Dec 12, 2021 · 44 revisions

ned Usage and Recipes

The examples in this page use long options for the options it is explaining, and short options for common options that are the same as those in grep that you probably already know. See the ned Usage section in the README for a description of all the options and their abbreviations and the TL;DR section for a list of examples.

Default Options

Put default options into NED_DEFAULTS. This example causes ned, by default, to recurse, to ignore non-ASCII/UTF-8 files, (--ignore-non-utf8), and to always show colors.

# In bash.
export NED_DEFAULTS='-Ru --colors=always'
# In fish.
set -gx NED_DEFAULTS '-Ru --colors=always'

Searching

Using colors with less.

Use the -R option on less to page through colored output.

> ned --colors=always 'something' | less -R

Using multiple neds in a pipe line.

Use --no-line-numbers, (-L), and make sure --colors is not set or is not set to always, (for all but the last ned at least, depending on what you're trying to achieve). This example is from a script that needed to get the width of the eDP1 monitor, in the fish shell.

> set -x NED_DEFAULTS '-L'
> xrandr | ned eDP1 | ned -g 1 ' ([0-9])+x'
1920
> # or...
> xrandr | ned eDP1 | ned --stdout '.* ([0-9]+)x.*' --replace '$1'
1920
>

Replacing/Bulk Editing

Tip: Stage or commit any existing changes before doing bulk edits so that you can reset and retry until you get the result you are looking for.

Replacing in a pipe line.

To replace in stdin and send it to stdout use the --stdout option. Update: this is the default behaviour when reading for stdin from version 1.3.0. This example is from a script that finds and extracts the id of a Wacom tablet's stylus that it uses in subsequent xsetwacom commands, since the ids can change with each reboot.

> xsetwacom --list devices | ned -ws -i --stdout --colors=never '.*stylus[^\n]*?id: ([0-9]+).*' --replace '$1'
19
>

Bulk case changes, including where they might span multiple lines.

The --whole-files option means matches are not line oriented - . matches newlines and matches like <p>.*?</p> can span lines, unlike sed, and we can do replacements, unlike grep.

The --case-replacements option turns on replacements that convert to UPPERCASE, lowercase, Title Case, or Sentence case, using \U, \L, \I, and \F, respectively. \E ends the case change. The option is off by default since most of the time is it not used. Unless you have a special need or you are setting NED_DEFAULTS in a script, it shouldn't generally be put in NED_DEFAULTS.

Use the --include, --exclude, and --exclude-dir options to ensure you are changing only the files that you intend to change. Especially that you don't accidentally nuke your .git directory. This example sets all h1's to Title Case in MadCap Flare topic and snippet files.

> ned --whole-files --recursive --case-replacements --include '*.htm' --include '*.flsnp' --exclude-dir '.git' '(<h1>)(.*?)(</h1>)' --replace '$1\I$2\E$3