Closed5

Zsh ディレクトリ間の移動を極める。

極力 cd を打たない

zsh
## 親に
alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../..'
alias -='cd -'

## よく使う directory
alias d='cd $DEV'
alias n='cd $PROFILE'
alias p='cd $PRIVATE'
alias .='cd $DOTFILES;'
~/.zshrc
## directory がある場合、direcotry 名で移動。
setopt auto_cd
~/.zshrc
## directory 作成後、cd を打たないで移動。
function mk () {
  mkdir $1; cd $1;
}

fd + fzf

~/.zshrc
# 3 depth の directory 一覧を fzf して移動。
function f() {
  dir=$(fd -t d -d 3 | fzf --height 40%)
  if [ "$(echo $dir)" ]; then
    cd $dir
  fi
}

# 開発ディレクトリから 3 depth の directory 一覧を fzf して移動。
function ff() {
  baseDir=$DEV
  dir=$(fd -t d --base-directory $baseDir -d 3 | fzf --height 40%)
  cd $baseDir/$dir
}

## parent directory fzf
function b() {
  dir=$(cd ..; fd -t d -d 1 | fzf --height 40%)
  if [ "$(echo $dir)" ]; then
    cd ../$dir
  fi
}

find + fzf

find を使うとこんな感じです。

~/.zshrc
function f() {
  local dir=$(find . -maxdepth 3 -type d | fzf)
  if [ "$(echo $dir)" ]; then
    cd $dir
  fi
}

z コマンドの使用。

https://github.com/rupa/z

man z

NAME
       z - jump around

SYNOPSIS
       z [-chlrtx] [regex1 regex2 ... regexn]

AVAILABILITY
       bash, zsh

DESCRIPTION
       Tracks your most used directories, based on 'frecency'.
       After  a  short  learning  phase, z will take you to the most 'frecent'
       directory that matches ALL of the regexes given on the command line, in
       order. For example, z foo bar would match /foo/bar but not /bar/foo.
zsh
curl -s "https://raw.githubusercontent.com/rupa/z/master/z.sh" > ~/bin/z
(cd ~/bin/; chmod +x z)
~/.zshrc
# bin
export PATH=$HOME/bin:"$PATH"
source "$HOME/bin/z"

## z + fzf
function zf() {
  local dir=$(z | sort -rn | cut -c 12- | fzf --height 40%)
  if [ -n "$dir" ]; then
    cd $dir
    zle accept-line
  else
    return 1
  fi
}

zsh bindkey で関数のキーバインドの設定

~/.zshrc
# ctrl+j で zf 関数を呼び出す。
zle -N zf
bindkey '^j' zf

こういう設定も面白いかも

~/.zshrc
function up() {
  cd ..
  zle accept-line
}
zle -N up
bindkey '^k' up
~/.zshrc
function back() {
  cd -
  zle accept-line
}
zle -N back
bindkey '^o' back

bindkey command で確認できるので便利

< ~ > bindkey
"^A" end-of-line
"^B" backward-char
"^C" self-insert
"^D" backward-delete-char
"^E" forward-char
"^F" autosuggest-accept
"^G" list-expand
"^H" vi-backward-delete-char
"^I" complete-word
"^J" zf
"^K" up
"^L" clear-screen
"^M" accept-line
"^N" history-substring-search-down
"^O" back
"^P" history-substring-search-up
"^Q" vi-quoted-insert
"^R" history-search-multi-word
"^S"-"^T" self-insert
"^U" vi-kill-line
"^V" "nvim .^M"
"^W" vi-backward-kill-word
"^Y"-"^Z" self-insert
"^[" vi-cmd-mode
"^[OA" up-line-or-history
"^[OB" down-line-or-history
"^[OC" vi-forward-char
"^[OD" vi-backward-char
"^[[200~" bracketed-paste
"^[[A" up-line-or-history
"^[[B" down-line-or-history
"^[[C" vi-forward-char
"^[[D" vi-backward-char
"^\\\\"-"~" self-insert
"^?" vi-backward-delete-char
"\M-^@"-"\M-^?" self-insert
このスクラップは3ヶ月前にクローズされました
ログインするとコメントできます