もた日記

くだらないことを真面目にやる

Linuxメモ : Rust製のnaviでインタラクティブにチートシートを探す

github.com

Rust製のnaviはコマンドのチートシートをfzfでインタラクティブに検索できるツール。
fzfの使い方については下記記事を参照。

wonderwall.hatenablog.com


インストール

README.mdのインストール方法によるとbrewcargoなどでインストールできる。

$ cargo install navi

コマンドラインヘルプ。

$ navi --help
navi 2.2.0

USAGE:
    navi [FLAGS] [OPTIONS] [SUBCOMMAND]

FLAGS:
    -h, --help             Prints help information
        --no-autoselect    Prevents autoselection in case of single entry
        --no-preview       Hides preview window
        --print            Instead of executing a snippet, prints it to stdout
    -V, --version          Prints version information

OPTIONS:
        --fzf-overrides <fzf-overrides>
            FZF overrides for cheat selection (must start with an empty space) [env: NAVI_FZF_OVERRIDES=]

        --fzf-overrides-var <fzf-overrides-var>
            FZF overrides for variable selection (must start with an empty space) [env: NAVI_FZF_OVERRIDES_VAR=]

    -p, --path <path>                              List of :-separated paths containing .cheat files [env: NAVI_PATH=]
    -s, --save <save>                              [Experimental] Instead of executing a snippet, saves it to a file

SUBCOMMANDS:
    best      Autoselects the snippet that best matches the query
    fn        Performs ad-hoc functions provided by navi
    help      Prints this message or the help of the given subcommand(s)
    query     Filters results
    repo      Manages cheatsheet repositories
    search    Uses online repositories for cheatsheets
    widget    Shows the path for shell widget files

EXAMPLES:
    navi                                   # default behavior
    navi --print                           # doesn't execute the snippet
    navi --path '/some/dir:/other/dir'     # uses custom cheats
    navi search docker                     # uses online data
    navi query git                         # filters results by "git"
    navi best 'sql create db' root mydb    # uses a snippet as a CLI
    navi repo add denisidoro/cheats        # imports cheats from github.com/denisidoro/cheats
    source <(navi widget zsh)              # loads the zsh widget
    navi --fzf-overrides ' --with-nth 1,2' # shows only the comment and tag columns
    navi --fzf-overrides ' --nth 1,2'      # search will consider only the first two columns
    navi --fzf-overrides ' --no-exact'     # looser search algorithm


使い方

基本

とりあえずnaviコマンドを実行してみる。
fzfがインストールされていないと以下のメッセージが表示される。

f:id:wonder-wall:20200331220745p:plain

fzfがインストールされていれば以下の画面が表示されるので、まずはデフォルトのチートシートをダウンロードしてみる。

f:id:wonder-wall:20200331220759p:plain

以下のように複数のチートシートが表示されるので使いそうなチートシートをTABで選択してEnterを押す(今回はgit, docker, shellを選択)。
右側には各チートシートの内容がプレビューされる。

f:id:wonder-wall:20200331220812p:plain

今回試した環境では~/.local/share/navi以下にチートシートのファイルがダウンロードされたようだ。

f:id:wonder-wall:20200331220826p:plain

これでチートシートが使えるようになったのでnaviコマンドを実行してみる。

f:id:wonder-wall:20200331220838p:plain

検索したい単語を入力していくとfzfで絞り込める。
そして、コマンド例を選択してEnterを押すとそのコマンドが実行できる(コマンドが実行されるので注意)。

f:id:wonder-wall:20200331221021p:plain

以上が基本的な使い方で、その他のチートシートを追加したい場合はnavi repo browseで探せる。

f:id:wonder-wall:20200331224444p:plain


独自チートシート追加

独自チートシートを追加したい場合は、先ほどチートシートがダウンロードされた場所にファイルを作成して、

$ mkdir ~/.local/share/navi/cheats/mycheat
$ touch ~/.local/share/navi/cheats/mycheat/mycheat.cheat

以下のようなフォーマットでコマンド例を記述する。

% cheat test

# test command description
ls -l

これで独自チートシートも検索できる。
詳細についてはREADME.mdを参照。

f:id:wonder-wall:20200331230244p:plain


類似ツール

man--help以外でコマンドの使い方を調べる類似ツールとしては下記がある。

  • tldr
  • cheat
  • howdoi
  • how2
  • cheat.sh

wonderwall.hatenablog.com

wonderwall.hatenablog.com