From 2b584586ed1caf15429625da981575ee35d407b8 Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Sun, 9 Feb 2025 11:53:35 +0900 Subject: Add --accept-nth option to transform the output This option can be used to replace a sed or awk in the post-processing step. ps -ef | fzf --multi --header-lines 1 | awk '{print $2}' ps -ef | fzf --multi --header-lines 1 --accept-nth 2 This may not be a very "Unix-y" thing to do, so I've always felt that fzf shouldn't have such an option, but I've finally changed my mind because: * fzf can be configured with a custom delimiter that is a fixed string or a regular expression. * In such cases, you'd need to repeat the delimiter again in the post-processing step. * Also, tools like awk or sed may interpret a regular expression differently, causing mismatches. You can still use sed, cut, or awk if you prefer. Close #3987 Close #1323 --- src/options.go | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'src/options.go') diff --git a/src/options.go b/src/options.go index 99d58634..2b310612 100644 --- a/src/options.go +++ b/src/options.go @@ -41,6 +41,7 @@ Usage: fzf [options] integer or a range expression ([BEGIN]..[END]). --with-nth=N[,..] Transform the presentation of each line using field index expressions + --accept-nth=N[,..] Define which fields to print on accept -d, --delimiter=STR Field delimiter regex (default: AWK-style) +s, --no-sort Do not sort the result --literal Do not normalize latin script letters @@ -544,6 +545,7 @@ type Options struct { Normalize bool Nth []Range WithNth []Range + AcceptNth []Range Delimiter Delimiter Sort int Track trackOption @@ -666,6 +668,7 @@ func defaultOptions() *Options { Normalize: true, Nth: make([]Range, 0), WithNth: make([]Range, 0), + AcceptNth: make([]Range, 0), Delimiter: Delimiter{}, Sort: 1000, Track: trackDisabled, @@ -2383,6 +2386,14 @@ func parseOptions(index *int, opts *Options, allArgs []string) error { if opts.WithNth, err = splitNth(str); err != nil { return err } + case "--accept-nth": + str, err := nextString("nth expression required") + if err != nil { + return err + } + if opts.AcceptNth, err = splitNth(str); err != nil { + return err + } case "-s", "--sort": if opts.Sort, err = optionalNumeric(1); err != nil { return err -- cgit v1.2.3