diff --git a/docs/REFERENCE.md b/docs/REFERENCE.md index c081ca81755..57d6e3dad03 100644 --- a/docs/REFERENCE.md +++ b/docs/REFERENCE.md @@ -381,6 +381,7 @@ The following configuration variables are available: | `bitwarden` | `command` | string | `bw` | Bitwarden CLI command | | `cd` | `args` | []string | *none* | Extra args to shell in `cd` command | | | `command` | string | *none* | Shell to run in `cd` command | +| `completion` | `custom` | bool | `false` | Enable custom shell completions | | `diff` | `args` | []string | *see `diff` below* | Extra args to external diff command | | | `command` | string | *none* | External diff command | | | `exclude` | []string | *none* | Entry types to exclude from diffs | diff --git a/internal/cmd/completioncmd.go b/internal/cmd/completioncmd.go index bf54ced21d7..0b2c67f8899 100644 --- a/internal/cmd/completioncmd.go +++ b/internal/cmd/completioncmd.go @@ -7,6 +7,10 @@ import ( "github.com/spf13/cobra" ) +type completionCmdConfig struct { + Custom bool `mapstructure:"custom"` +} + func (c *Config) newCompletionCmd() *cobra.Command { completionCmd := &cobra.Command{ Use: "completion shell", diff --git a/internal/cmd/config.go b/internal/cmd/config.go index a6dc3a805e0..9b2fa8e2f89 100644 --- a/internal/cmd/config.go +++ b/internal/cmd/config.go @@ -122,13 +122,14 @@ type Config struct { keyring keyringData // Command configurations, settable in the config file. - Add addCmdConfig `mapstructure:"add"` - CD cdCmdConfig `mapstructure:"cd"` - Diff diffCmdConfig `mapstructure:"diff"` - Docs docsCmdConfig `mapstructure:"docs"` - Edit editCmdConfig `mapstructure:"edit"` - Git gitCmdConfig `mapstructure:"git"` - Merge mergeCmdConfig `mapstructure:"merge"` + Add addCmdConfig `mapstructure:"add"` + CD cdCmdConfig `mapstructure:"cd"` + Completion completionCmdConfig `mapstructure:"completion"` + Diff diffCmdConfig `mapstructure:"diff"` + Docs docsCmdConfig `mapstructure:"docs"` + Edit editCmdConfig `mapstructure:"edit"` + Git gitCmdConfig `mapstructure:"git"` + Merge mergeCmdConfig `mapstructure:"merge"` // Command configurations, not settable in the config file. apply applyCmdConfig @@ -1954,6 +1955,10 @@ func (c *Config) targetRelPathsBySourcePath( func (c *Config) targetValidArgs( cmd *cobra.Command, args []string, toComplete string, ) ([]string, cobra.ShellCompDirective) { + if !c.Completion.Custom { + return nil, cobra.ShellCompDirectiveDefault + } + toCompleteAbsPath, err := chezmoi.NewAbsPathFromExtPath(toComplete, c.homeDirAbsPath) if err != nil { cobra.CompErrorln(err.Error()) diff --git a/internal/cmd/testdata/scripts/completion_unix.txt b/internal/cmd/testdata/scripts/completion_unix.txt index fc65224f36a..d5da7e3aade 100644 --- a/internal/cmd/testdata/scripts/completion_unix.txt +++ b/internal/cmd/testdata/scripts/completion_unix.txt @@ -44,6 +44,9 @@ $HOME/.file -- golden/complete-target-home-dot-f -- $HOME/.file :4 +-- home/user/.config/chezmoi/chezmoi.toml -- +[completion] + custom = true -- home/user/.local/share/chezmoi/dot_dir/file -- # contents of .dir/file -- home/user/.local/share/chezmoi/dot_file --