Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,35 @@ sk --shell fish > ~/.config/fish/completions/sk.fish

## Key Bindings

You can generate shell completions for your preferred shell using the `--key-bindings` flag with one of the supported shells: `bash`, `zsh`, `fish`:

#### Option 1: Source directly in your current shell session

```sh
# For bash
source <(sk --key-bindings bash)

# For zsh
source <(sk --key-bindings zsh)

# For fish
sk --key-bindings fish | source
```

#### Option 2: Save to a file to be loaded automatically on shell startup

```sh
# For bash, add to ~/.bashrc
echo 'source <(sk --key-bindings bash)' >> ~/.bashrc # Or save to ~/.bash_completion

# For zsh, add to ~/.zshrc
sk --key-bindings zsh >> ~/.zshrc

# For fish, add to ~/.config/fish/conf.d/
sk --key-bindings fish > ~/.config/fish/conf.d/skim_key_bindings.fish
source ~/.config/fish/conf.d/skim_key_bindings.fish
```

Some commonly used key bindings:

| Key | Action |
Expand Down
13 changes: 12 additions & 1 deletion man/man1/sk.1
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
.SH NAME
sk \- sk \- fuzzy finder in Rust
.SH SYNOPSIS
\fBsk\fR [\fB\-\-tac\fR] [\fB\-\-min\-query\-length\fR] [\fB\-\-no\-sort\fR] [\fB\-t\fR|\fB\-\-tiebreak\fR] [\fB\-n\fR|\fB\-\-nth\fR] [\fB\-\-with\-nth\fR] [\fB\-d\fR|\fB\-\-delimiter\fR] [\fB\-e\fR|\fB\-\-exact\fR] [\fB\-\-regex\fR] [\fB\-\-algo\fR] [\fB\-\-case\fR] [\fB\-b\fR|\fB\-\-bind\fR] [\fB\-m\fR|\fB\-\-multi\fR] [\fB\-\-no\-multi\fR] [\fB\-\-no\-mouse\fR] [\fB\-c\fR|\fB\-\-cmd\fR] [\fB\-i\fR|\fB\-\-interactive\fR] [\fB\-I \fR] [\fB\-\-color\fR] [\fB\-\-no\-hscroll\fR] [\fB\-\-keep\-right\fR] [\fB\-\-skip\-to\-pattern\fR] [\fB\-\-no\-clear\-if\-empty\fR] [\fB\-\-no\-clear\-start\fR] [\fB\-\-no\-clear\fR] [\fB\-\-show\-cmd\-error\fR] [\fB\-\-layout\fR] [\fB\-\-reverse\fR] [\fB\-\-height\fR] [\fB\-\-no\-height\fR] [\fB\-\-min\-height\fR] [\fB\-\-margin\fR] [\fB\-p\fR|\fB\-\-prompt\fR] [\fB\-\-cmd\-prompt\fR] [\fB\-\-ansi\fR] [\fB\-\-tabstop\fR] [\fB\-\-info\fR] [\fB\-\-no\-info\fR] [\fB\-\-inline\-info\fR] [\fB\-\-header\fR] [\fB\-\-header\-lines\fR] [\fB\-\-history\fR] [\fB\-\-history\-size\fR] [\fB\-\-cmd\-history\fR] [\fB\-\-cmd\-history\-size\fR] [\fB\-\-preview\fR] [\fB\-\-preview\-window\fR] [\fB\-q\fR|\fB\-\-query\fR] [\fB\-\-cmd\-query\fR] [\fB\-\-expect\fR] [\fB\-\-read0\fR] [\fB\-\-print0\fR] [\fB\-\-print\-query\fR] [\fB\-\-print\-cmd\fR] [\fB\-\-print\-score\fR] [\fB\-1\fR|\fB\-\-select\-1\fR] [\fB\-0\fR|\fB\-\-exit\-0\fR] [\fB\-\-sync\fR] [\fB\-\-pre\-select\-n\fR] [\fB\-\-pre\-select\-pat\fR] [\fB\-\-pre\-select\-items\fR] [\fB\-\-pre\-select\-file\fR] [\fB\-f\fR|\fB\-\-filter\fR] [\fB\-\-shell\fR] [\fB\-\-tmux\fR] [\fB\-h\fR|\fB\-\-help\fR] [\fB\-V\fR|\fB\-\-version\fR]
\fBsk\fR [\fB\-\-tac\fR] [\fB\-\-min\-query\-length\fR] [\fB\-\-no\-sort\fR] [\fB\-t\fR|\fB\-\-tiebreak\fR] [\fB\-n\fR|\fB\-\-nth\fR] [\fB\-\-with\-nth\fR] [\fB\-d\fR|\fB\-\-delimiter\fR] [\fB\-e\fR|\fB\-\-exact\fR] [\fB\-\-regex\fR] [\fB\-\-algo\fR] [\fB\-\-case\fR] [\fB\-b\fR|\fB\-\-bind\fR] [\fB\-m\fR|\fB\-\-multi\fR] [\fB\-\-no\-multi\fR] [\fB\-\-no\-mouse\fR] [\fB\-c\fR|\fB\-\-cmd\fR] [\fB\-i\fR|\fB\-\-interactive\fR] [\fB\-I \fR] [\fB\-\-color\fR] [\fB\-\-no\-hscroll\fR] [\fB\-\-keep\-right\fR] [\fB\-\-skip\-to\-pattern\fR] [\fB\-\-no\-clear\-if\-empty\fR] [\fB\-\-no\-clear\-start\fR] [\fB\-\-no\-clear\fR] [\fB\-\-show\-cmd\-error\fR] [\fB\-\-layout\fR] [\fB\-\-reverse\fR] [\fB\-\-height\fR] [\fB\-\-no\-height\fR] [\fB\-\-min\-height\fR] [\fB\-\-margin\fR] [\fB\-p\fR|\fB\-\-prompt\fR] [\fB\-\-cmd\-prompt\fR] [\fB\-\-ansi\fR] [\fB\-\-tabstop\fR] [\fB\-\-info\fR] [\fB\-\-no\-info\fR] [\fB\-\-inline\-info\fR] [\fB\-\-header\fR] [\fB\-\-header\-lines\fR] [\fB\-\-history\fR] [\fB\-\-history\-size\fR] [\fB\-\-cmd\-history\fR] [\fB\-\-cmd\-history\-size\fR] [\fB\-\-preview\fR] [\fB\-\-preview\-window\fR] [\fB\-q\fR|\fB\-\-query\fR] [\fB\-\-cmd\-query\fR] [\fB\-\-expect\fR] [\fB\-\-read0\fR] [\fB\-\-print0\fR] [\fB\-\-print\-query\fR] [\fB\-\-print\-cmd\fR] [\fB\-\-print\-score\fR] [\fB\-1\fR|\fB\-\-select\-1\fR] [\fB\-0\fR|\fB\-\-exit\-0\fR] [\fB\-\-sync\fR] [\fB\-\-pre\-select\-n\fR] [\fB\-\-pre\-select\-pat\fR] [\fB\-\-pre\-select\-items\fR] [\fB\-\-pre\-select\-file\fR] [\fB\-f\fR|\fB\-\-filter\fR] [\fB\-\-shell\fR] [\fB\-\-key\-bindings\fR] [\fB\-\-tmux\fR] [\fB\-h\fR|\fB\-\-help\fR] [\fB\-V\fR|\fB\-\-version\fR]
.SH DESCRIPTION
sk \- fuzzy finder in Rust
.PP
Expand Down Expand Up @@ -750,6 +750,17 @@ Supported shells: bash, zsh, fish, powershell, elvish
Note: While PowerShell completions are supported, Windows is not supported for now.
.br

.br
[\fIpossible values: \fRbash, elvish, fish, powershell, zsh]
.TP
\fB\-\-key\-bindings\fR \fI<SHELL>\fR
Generate key binding script

Generate key binding script for the specified shell: bash, zsh, fish, etc. The output can be directly sourced or saved to a file for automatic loading. Examples: source <(sk \-\-key\-bindings bash) (immediate use) sk \-\-key\-bindings bash >> ~/.bashrc (persistent use)

Supported shells: bash, zsh, fish
.br

.br
[\fIpossible values: \fRbash, elvish, fish, powershell, zsh]
.SH VERSION
Expand Down
6 changes: 5 additions & 1 deletion shell/completion.bash
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ _sk() {

case "${cmd}" in
sk)
opts="-t -n -d -e -b -m -c -i -I -p -q -1 -0 -f -x -h -V --tac --min-query-length --no-sort --tiebreak --nth --with-nth --delimiter --exact --regex --algo --case --bind --multi --no-multi --no-mouse --cmd --interactive --color --no-hscroll --keep-right --skip-to-pattern --no-clear-if-empty --no-clear-start --no-clear --show-cmd-error --layout --reverse --height --no-height --min-height --margin --prompt --cmd-prompt --ansi --tabstop --info --no-info --inline-info --header --header-lines --history --history-size --cmd-history --cmd-history-size --preview --preview-window --query --cmd-query --expect --read0 --print0 --print-query --print-cmd --print-score --select-1 --exit-0 --sync --pre-select-n --pre-select-pat --pre-select-items --pre-select-file --filter --shell --tmux --extended --literal --cycle --hscroll-off --filepath-word --jump-labels --border --no-bold --pointer --marker --phony --help --version"
opts="-t -n -d -e -b -m -c -i -I -p -q -1 -0 -f -x -h -V --tac --min-query-length --no-sort --tiebreak --nth --with-nth --delimiter --exact --regex --algo --case --bind --multi --no-multi --no-mouse --cmd --interactive --color --no-hscroll --keep-right --skip-to-pattern --no-clear-if-empty --no-clear-start --no-clear --show-cmd-error --layout --reverse --height --no-height --min-height --margin --prompt --cmd-prompt --ansi --tabstop --info --no-info --inline-info --header --header-lines --history --history-size --cmd-history --cmd-history-size --preview --preview-window --query --cmd-query --expect --read0 --print0 --print-query --print-cmd --print-score --select-1 --exit-0 --sync --pre-select-n --pre-select-pat --pre-select-items --pre-select-file --filter --shell --key-bindings --tmux --extended --literal --cycle --hscroll-off --filepath-word --jump-labels --border --no-bold --pointer --marker --phony --help --version"
if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
Expand Down Expand Up @@ -209,6 +209,10 @@ _sk() {
COMPREPLY=($(compgen -W "bash elvish fish powershell zsh" -- "${cur}"))
return 0
;;
--key-bindings)
COMPREPLY=($(compgen -W "bash elvish fish powershell zsh" -- "${cur}"))
return 0
;;
--tmux)
COMPREPLY=($(compgen -f "${cur}"))
return 0
Expand Down
5 changes: 5 additions & 0 deletions shell/completion.fish
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ elvish\t''
fish\t''
powershell\t''
zsh\t''"
complete -c sk -l key-bindings -d 'Generate key binding script' -r -f -a "bash\t''
elvish\t''
fish\t''
powershell\t''
zsh\t''"
complete -c sk -l tmux -d 'Run in a tmux popup' -r
complete -c sk -l hscroll-off -d 'Reserved for later use' -r
complete -c sk -l jump-labels -d 'Reserved for later use' -r
Expand Down
1 change: 1 addition & 0 deletions shell/completion.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ _sk() {
'-f+[Query for filter mode]:FILTER:_default' \
'--filter=[Query for filter mode]:FILTER:_default' \
'--shell=[Generate shell completion script]:SHELL:(bash elvish fish powershell zsh)' \
'--key-bindings=[Generate key binding script]:SHELL:(bash elvish fish powershell zsh)' \
'--tmux=[Run in a tmux popup]' \
'--hscroll-off=[Reserved for later use]:HSCROLL_OFF:_default' \
'--jump-labels=[Reserved for later use]:JUMP_LABELS:_default' \
Expand Down
25 changes: 24 additions & 1 deletion skim/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ extern crate skim;
extern crate time;

use clap::{CommandFactory, Error, Parser};
use clap_complete::generate;
use clap_complete::{Shell, generate};

use derive_builder::Builder;
use std::fs::File;
Expand Down Expand Up @@ -80,12 +80,35 @@ fn sk_main() -> Result<i32, SkMainError> {
let mut opts = parse_args()?;

// Handle shell completion generation if requested
#[cfg(feature = "cli")]
if let Some(shell) = opts.shell {
// Generate completion script directly to stdout
generate(shell, &mut SkimOptions::command(), "sk", &mut io::stdout());
return Ok(0);
}

// Handle key bindings generation if requested
#[cfg(feature = "cli")]
if let Some(shell) = opts.key_bindings {
// Generate key binding script directly to stdout

use std::{thread::sleep, time::Duration};
let script = match shell {
Shell::Bash => include_str!("../../../shell/key-bindings.bash"),
Shell::Fish => include_str!("../../../shell/key-bindings.fish"),
Shell::Zsh => include_str!("../../../shell/key-bindings.zsh"),
_ => unimplemented!(),
};
sleep(Duration::from_secs(10));
for line in script.lines()
/*.skip(1)*/
{
println!("{line}");
}

return Ok(0);
}

let reader_opts = SkimItemReaderOption::default()
.ansi(opts.ansi)
.delimiter(&opts.delimiter)
Expand Down
18 changes: 18 additions & 0 deletions skim/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,21 @@ pub struct SkimOptions {
)]
pub shell: Option<clap_complete::Shell>,

/// Generate key binding script
///
/// Generate key binding script for the specified shell: bash, zsh, fish, etc.
/// The output can be directly sourced or saved to a file for automatic loading.
/// Examples: `source <(sk --key-bindings bash)` (immediate use)
/// `sk --key-bindings bash >> ~/.bashrc` (persistent use)
///
/// Supported shells: bash, zsh, fish
#[cfg(feature = "cli")]
#[cfg_attr(
feature = "cli",
arg(long, value_name = "SHELL", help_heading = "Scripting", value_enum)
)]
pub key_bindings: Option<clap_complete::Shell>,

/// Run in a tmux popup
///
/// Format: `sk --tmux <center|top|bottom|left|right>[,SIZE[%]][,SIZE[%]]`
Expand Down Expand Up @@ -1045,6 +1060,8 @@ impl Default for SkimOptions {
filter: Default::default(),
#[cfg(feature = "cli")]
shell: Default::default(),
#[cfg(feature = "cli")]
key_bindings: Default::default(),
tmux: Default::default(),
extended: Default::default(),
literal: Default::default(),
Expand Down Expand Up @@ -1097,6 +1114,7 @@ impl SkimOptions {

self
}

pub fn init_histories(&mut self) {
if let Some(histfile) = &self.history_file {
self.query_history.extend(read_file_lines(histfile).unwrap_or_default());
Expand Down
Loading