Skip to content
Myr

Add 'r' flag (raw) for unquoted output

Before adoption: this commit came in with the history the project adopted, so no landing records it.

By default, placeholder expressions are automatically quoted to ensure they are safely passed as arguments to external programs. The r flag ({r}, {r1}, etc.) disables this behavior, outputting the evaluated value without quotes. For example, echo 'foo bar' | fzf --preview 'echo {} {r}' The preview command becomes: echo 'foo bar' foo bar Since `{r}` expands to unquoted "foo bar", 'foo' and 'bar' are passed as separate arguments. **Use with caution** Unquoted output can lead to broken commands. echo "let's go" | fzf --preview 'echo {r}' Close #4330

authored by Junegunn Choi · committed by Junegunn Choi · parent ba6d1b8 · Browse files · 3 files+21 added−4 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+21 added−4 removed

  • other:+21 added−4 removed

3 files+21 added−4 removed

modified:man/man1/fzf.1+6 added
766 unmodified lines
767e.g.768 \fBfzf \-\-multi \-\-preview='head \-10 {+}'769 git log \-\-oneline | fzf \-\-multi \-\-preview 'git show {+1}'\fR770771Each expression expands to a quoted string, so that it's safe to pass it as an772argument to an external command. So you should not manually add quotes around773the curly braces. But if you don't want this behavior, you can put774\fBr\fR flag (raw) in the expression (e.g. \fB{r}\fR, \fB{r1}\fR, etc).775Use it with caution as unquoted output can lead to broken commands.776777When using a field index expression, leading and trailing whitespace is stripped778from the replacement string. To preserve the whitespace, use the \fBs\fR flag.779780A placeholder expression with \fBf\fR flag is replaced to the path of
1127 unmodified lines
modified:src/terminal_test.go+8 added
73 unmodified lines
74 // {}, strip ansi75 result = replacePlaceholderTest("echo {}", true, Delimiter{}, printsep, false, "query", items1)76 checkFormat("echo {{.O}} foo{{.I}}bar baz{{.O}}")7778 // {r}, strip ansi79 result = replacePlaceholderTest("echo {r}", true, Delimiter{}, printsep, false, "query", items1)80 checkFormat("echo foo'bar baz")8182 // {r..}, strip ansi83 result = replacePlaceholderTest("echo {r..}", true, Delimiter{}, printsep, false, "query", items1)84 checkFormat("echo foo'bar baz")8586 // {}, with multiple items87 result = replacePlaceholderTest("echo {}", true, Delimiter{}, printsep, false, "query", items2)88 checkFormat("echo {{.O}}foo{{.I}}bar baz{{.O}}")89
611 unmodified lines
modified:src/terminal.go+7 added-4 removed
36 unmodified lines
37As such it is not useful for validation, but rather to generate test38cases for example.3940 \\?(?: # escaped type41 {\+?s?f?RANGE(?:,RANGE)*} # token type41 {\+?s?f?r?RANGE(?:,RANGE)*} # token type42 {q[:s?RANGE]} # query type43 |{\+?n?f?} # item type (notice no mandatory element inside brackets)44 )45 RANGE = (?:
18 unmodified lines
64// After this duration, users can press CTRL-C to terminate the command.65const blockDuration = 1 * time.Second6667func init() {68 placeholder = regexp.MustCompile(`\\?(?:{[+sf]*[0-9,-.]*}|{q(?::s?[0-9,-.]+)?}|{fzf:(?:query|action|prompt)}|{\+?f?nf?})`)68 placeholder = regexp.MustCompile(`\\?(?:{[+sfr]*[0-9,-.]*}|{q(?::s?[0-9,-.]+)?}|{fzf:(?:query|action|prompt)}|{\+?f?nf?})`)69 whiteSuffix = regexp.MustCompile(`\s*$`)70 offsetComponentRegex = regexp.MustCompile(`([+-][0-9]+)|(-?/[1-9][0-9]*)`)71 offsetTrimCharsRegex = regexp.MustCompile(`[^0-9/+-]`)72
554 unmodified lines
627 preserveSpace bool628 number bool629 forceUpdate bool630 file bool631 raw bool632}633634type searchRequest struct {635 sort bool
3146 unmodified lines
3782 case 'n':3783 flags.number = true3784 case 'f':3785 flags.file = true3786 case 'r':3787 flags.raw = true3788 case 'q':3789 flags.forceUpdate = true3790 trimmed += string(char)3791 default:
143 unmodified lines
3935 // now.3936 return "''"3937 }3938 return strconv.Itoa(int(n))3936 case flags.file:3939 case flags.file || flags.raw:3940 return item.AsString(params.stripAnsi)3941 default:3942 return params.executor.QuoteEntry(item.AsString(params.stripAnsi))3943 }
33 unmodified lines
39773978 if !flags.preserveSpace {3979 str = strings.TrimSpace(str)3980 }3978 if !flags.file {3981 if !flags.file && !flags.raw {3982 str = params.executor.QuoteEntry(str)3983 }3984 return str3985 }
2380 unmodified lines