diff --git a/README.md b/README.md index 285ba4fc87e..779d4b9fb91 100644 --- a/README.md +++ b/README.md @@ -311,6 +311,10 @@ or `py`. - `FZF_DEFAULT_OPTS` - Default options - e.g. `export FZF_DEFAULT_OPTS="--layout=reverse --inline-info"` +- `FZF_DEFAULT_OPTS_FILE` + - If you prefer to manage default options in a file, set this variable to + point to the location of the file + - e.g. `export FZF_DEFAULT_OPTS_FILE=~/.fzfrc` ### Options diff --git a/man/man1/fzf.1 b/man/man1/fzf.1 index c10f334efcc..158f2d0c463 100644 --- a/man/man1/fzf.1 +++ b/man/man1/fzf.1 @@ -867,6 +867,9 @@ this case make sure that the command is POSIX-compliant. .B FZF_DEFAULT_OPTS Default options. e.g. \fBexport FZF_DEFAULT_OPTS="--extended --cycle"\fR .TP +.B FZF_DEFAULT_OPTS_FILE +The location of the file that contains the default options. e.g. \fBexport FZF_DEFAULT_OPTS_FILE=~/.fzfrc\fR +.TP .B FZF_API_KEY Can be used to require an API key when using \fB--listen\fR option. If not set, no authentication will be required by the server. You can set this value if diff --git a/src/options.go b/src/options.go index dcb70dcb7f3..2b1af5a2ba6 100644 --- a/src/options.go +++ b/src/options.go @@ -421,8 +421,10 @@ func help(code int) { os.Exit(code) } +var errorContext = "" + func errorExit(msg string) { - os.Stderr.WriteString(msg + "\n") + os.Stderr.WriteString(errorContext + msg + "\n") os.Exit(exitError) } @@ -2167,13 +2169,36 @@ func ParseOptions() *Options { } } - // Options from Env var - words, _ := shellwords.Parse(os.Getenv("FZF_DEFAULT_OPTS")) + // 1. Options from $FZF_DEFAULT_OPTS_FILE + if path := os.Getenv("FZF_DEFAULT_OPTS_FILE"); path != "" { + bytes, err := os.ReadFile(path) + if err != nil { + errorContext = "$FZF_DEFAULT_OPTS_FILE: " + errorExit(err.Error()) + } + + words, parseErr := shellwords.Parse(string(bytes)) + if parseErr != nil { + errorContext = path + ": " + errorExit(parseErr.Error()) + } + if len(words) > 0 { + parseOptions(opts, words) + } + } + + // 2. Options from $FZF_DEFAULT_OPTS string + words, parseErr := shellwords.Parse(os.Getenv("FZF_DEFAULT_OPTS")) + errorContext = "$FZF_DEFAULT_OPTS: " + if parseErr != nil { + errorExit(parseErr.Error()) + } if len(words) > 0 { parseOptions(opts, words) } - // Options from command-line arguments + // 3. Options from command-line arguments + errorContext = "" parseOptions(opts, os.Args[1:]) postProcessOptions(opts)