yiskw note

機械学習やプログラミングについて気まぐれで書きます

treeコマンドの改良版であるtreを試してみた


はじめに

f:id:yiskw713:20220119224449g:plain:w800

treeコマンドの改良版であるtreを使用してみました.
treはRust製のツールで,treeをより使いやすくしたものです.
ツリー構造をカラー表示してくれるだけでなく,ファイルやディレクトリの横に表示されている番号を指定することで,
自身が使用しているエディターでそのファイルを開くことも可能です.
treの使い方や設定方法について,こちらにメモを残しておきます.

最近ターミナルの改造にハマっており,その他のツールについてもまとめておりますので,良ければご参照ください.

yiskw713.hatenablog.com

yiskw713.hatenablog.com

treとは

github.com

f:id:yiskw713:20220119223326p:plain:w800

treとは,Rust製のツールでtreeをより使いやすくしたものです.
ツリー構造をカラー表示したり,ファイル横の番号を指定することで,
自身が使用しているエディターでそのファイルを開くことも可能です.

インストール方法

Macを使用している場合はHomebrewで,Rustの環境がある場合はcargoでインストールが可能です.
その他のインストール方法に関してはこちらをご参照ください.

brew install tre-command
# or
cargo install tre-command

インストールが完了したら,使用しているシェルの設定ファイル(.zshrc, .bashrcなど)に以下を書き込みます.

tre() { command tre "$@" -e && source "/tmp/tre_aliases_$USER" 2>/dev/null; }

こちらを設定すると,ツリー構造に表示されたファイルの番号を指定することで,そのファイルをエディタで開けるようになります.
デフォルトでは$EDITORに指定されているエディタを使用します.
$EDITORを設定していない場合は,-eの後ろに使用するエディタを指定することも可能です.

tre() { command tre "$@" -e vim && source "/tmp/tre_aliases_$USER" 2>/dev/null; }

使い方

f:id:yiskw713:20220119230221p:plain
公式レポジトリより引用.
treコマンドを実行した後に,e8コマンドで8番目のファイルをエディターで開いている.

treコマンドを実行することで,ディレクトリのツリー構造を可視化することができます.
その後に特定のファイルをエディタで開きたい場合は,e<ファイル番号>を実行します.
上記の画像のように,ディレクトリ構造を綺麗に可視化しつつ,簡単にファイルを開くことができます!

オプションに関しては以下を確認ください.

~ 
 tre --help
Usage: tre [path] [option]

Print files, directories, and symlinks in tree form.

Hidden files and those configured to be ignored by git will be (optionally)
ignored.

With correct configuration, each displayed file can have a shell alias created
for it, which opens the file in the default editor or an otherwise specified
command.

Path:
    The root path whose content is to be listed. Defaults to ".".

Options:
    -a, --all           Print all files and directories, including hidden
                        ones.
    -d, --directories   Only list directories in output.
    -e, --editor [EDITOR]
                        Create aliases for each displayed result in
                        /tmp/tre_aliases_$USER and add a number in front of
                        file name to indicate the alias name. For example, a
                        number "42" means an shell alias "e42" has been
                        created. Running "e42" will cause the associated file
                        or directory to be open with $EDITOR, or a command
                        specified along with this command.
    -j, --json          Output JSON instead of tree diagram.
    -l, --limit [DEPTH] Limit display depth file tree output.
    -s, --simple        Use normal print despite gitignore settings. '-a' has
                        higher priority.
    -v, --version       Show version number.
    -h, --help          Show this help message.

Project site: https://github.com/dduan/tre

参考