Post

linux에 fzf 사용하기

목차


개요

Ubuntu 에서 fzf를 설치하고 사용하는 법을 알아보도록 하자.

필자의 작업 환경은 Ununtu 18.04 LTS 이며 apt로 fzf를 설치할 수 없어 git으로 설치를 진행하였다.

설치

~/.zsh/.fzf 디렉토리에 설치하려고 한다.
git으로 클론 받아 install 스크립트를 실행하면 끝난다.

1
2
git clone --depth 1 https://github.com/junegunn/fzf.git ~/.zsh/.fzf
~/.zsh/.fzf/install

설치를 하려고 하면 다음과 같이 질문을 한다.

  1. fuzzy auto-completion 사용 여부
  2. key bindings 사용 여부 (해당 옵션을 사용하면 ctrl+t 단축키 등으로 fzf를 빠르게 실행할 수 있다.)
  3. 쉘 설정 파일 업데이트 여부
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  - Checking fzf executable ... 0.37.0
Do you want to enable fuzzy auto-completion? ([y]/n) y
Do you want to enable key bindings? ([y]/n) y

Generate /home/test/.fzf.bash ... OK
Generate /home/test/.fzf.zsh ... OK

Do you want to update your shell configuration files? ([y]/n) y

Update /home/test/.bashrc:
  - [ -f ~/.fzf.bash ] && source ~/.fzf.bash
    + Added

Update /home/test/.zsh/zcomp/.zshrc:
  - [ -f ~/.fzf.zsh ] && source ~/.fzf.zsh
    + Added

Finished. Restart your shell or reload config file.
   source ~/.bashrc  # bash
   source /home/test/.zsh/zcomp/.zshrc   # zsh

Use uninstall script to remove fzf.

For more information, see: https://github.com/junegunn/fzf

설정 파일 업데이트

정상적으로 설치가 되었다면 홈 디렉토리에 .fzf.bash.fzf.zsh 파일이 생성되었을 것이다.

bash를 사용한다면 .fzf.bash, zsh를 사용한다면 .fzf.zsh 를 사용하면 될 것이다.
(사실 두 파일 차이 거의 없다. diff를 해보니 .zsh .bash 확장자만 다르고 내용은 같았다.)

.fzf.zsh 를 열어보니 내용은 다음과 같았다.

1
2
3
4
5
6
7
8
9
10
11
12
13
# Setup fzf
# ---------
if [[ ! "$PATH" == */home/test/.zsh/.fzf/bin* ]]; then
  PATH="${PATH:+${PATH}:}/home/test/.zsh/.fzf/bin"
fi

# Auto-completion
# ---------------
[[ $- == *i* ]] && source "/home/test/.zsh/.fzf/shell/completion.zsh" 2> /dev/null

# Key bindings
# ------------
source "/home/test/.zsh/.fzf/shell/key-bindings.zsh"

단순히 이대로 사용하고 싶은 사람의 경우 .zshrc 파일에
source $HOME/.fzf.zsh 를 추가하고 사용하면 된다.

bash 유저라면 .bashrc 파일에 source $HOME/.fzf.bash 를 추가하면 된다.

그러나 나는 zsh 설정의 경우 .zshrc 에 몰아서 관리하고 싶어 해당 내용을 zsh로 옮겼다.

아래는 .zshrc에 추가한 내용이다.
절대 경로로 박혀 있는 홈 디렉토리 내용을 $HOME 변수로 대체하였고
.zsh/.fzf 디렉토리가 없으면 동작하지 않도록 if 문으로 감쌌다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# Setup fzf
# git clone --depth 1 https://github.com/junegunn/fzf.git ~/.zsh/.fzf
# ~/.zsh/.fzf/install
# ---------
if [ -d $HOME/.zsh/.fzf/ ]; then
  if [[ ! "$PATH" == *${HOME}/.zsh/.fzf/bin* ]]; then
    PATH="${PATH:+${PATH}:}$HOME/.zsh/.fzf/bin"
  fi
  # Auto-completion
  # ---------------
  [[ $- == *i* ]] && source "$HOME/.zsh/.fzf/shell/completion.zsh" 2> /dev/null

  # Key bindings
  # ------------
  source "$HOME/.zsh/.fzf/shell/key-bindings.zsh"
fi

이후 source ~/.zshrc (배쉬 유저는 source ~/.bashrc) 를 실행하면 끝이다.

검색 설정

기본적으로 fzf는 숨긴 디렉토리 내 파일까지 검색해주지 않는다.
이 경우 export FZF_DEFAULT_COMMAND 를 설정해주면 된다.

1
export FZF_DEFAULT_COMMAND='find . -type f'

설정 시 숨긴 디렉토리 내 파일까지 검색해준다.

검색 속도가 느리면 find 대신 fd 를 사용해 파일을 검색하도록 설정하면 된다.

1
2
3
4
5
6
7
8
9
10
# fd 명령어가 존재하면 fd를 사용하게끔 설정함
if [ "$(command -v fd)" != "" ]; then
  export FZF_DEFAULT_COMMAND='fd --type file --follow --hidden --no-ignore'
  # .git 디렉토리를 제외하고 검색
  # export FZF_DEFAULT_COMMAND='fd --type file --follow --hidden --no-ignore --exclude .git'
  # export FZF_DEFAULT_COMMAND='fd --type file --follow --hidden --no-ignore --color=always --exclude .git'
else
  # export FZF_DEFAULT_COMMAND=''
  export FZF_DEFAULT_COMMAND='find . -type f'
fi

fd 설치 방법은 여기 참조

사용법

-m 옵션 사용 시 여러 파일을 선택할 수 있다.

  • ctrl+k or ctrl+p : 위로 이동 \
  • ctrl+j or ctrl+n : 아래로 이동

  • enter : 선택

다중 파일 선택 기능 사용 시

  • tab : 파일 선택
  • shift+tab : 파일 선택 해제
  • enter : 선택된 파일로 끝내기

응용

xargs 등을 활용하여 다양한 방법으로 사용할 수 있다.

절대경로 출력하기

1
2
3
4
5
# readlink -e $(fzf)
# fzf | xargs -I {} readlink -e {}
fzf | xargs readlink -e

/home/test/jfhib.github.io/wiki/algorithm/prefix-sum.md

vi로 열기

1
2
3
vi $(fzf)
# vi $(fzf)
# vi `fzf`

remote-ssh의 경우 code로 열기

1
2
3
4
5
6
7
8
9
10
11
12
13
code $(fzf)
code `fzf`
fzf | xargs code

function fcode()
{
  # [ "$(command -v fzf)" != "" ] && echo "asdfasdf"
  local fzfres=$(fzf)
  if [ "$fzfres" != "" ]; then
    code $fzfres
  fi
}
fcode
This post is licensed under CC BY 4.0 by the author.