[Linux] zsh, oh-my-zsh 로 쉘 꾸미기

문연수·2021년 8월 10일
3

Linux (Fedora)

목록 보기
6/19
post-thumbnail

쉘(shell)은 커맨드 라인 명령어 해석기(command-line interpreter)이다. 말이 복잡한데 그냥 사용자의 명령어를 읽어서 사용자에 요구의 맞는 동작을 수행하는 운영체제와 사용자의 징검다리라고 생각하면 쉽다. 쉘에는 다양한 종류가 있는데 리눅스 기본 쉘은 bash 이다. 이번 글에서는 bash 가 아닌 다양한 테마와 플러그인을 사용할 수 있는 zsh + oh-my-zsh 를 사용해볼 것이다.

1. zsh 설치

필자 역시 설치하는 과정을 보여주기 위해 바닥부터 다시 설치해볼 예정이다. 가장 먼저 할 일은 zsh 설치이다. Fedora 34 에는 zsh 패키지가 있기 때문에 패키지 매니저를 통해 쉽게 설치 가능하다.

sudo dnf install zsh

이제 터미널을 열었다 닫으면 bash 에서 zsh 로 변경된 것을 볼 수...

있으나 설정 화면이 나온다. 그냥 0 입력하고 빠져 나오면 된다.

쉘의 프롬프트 물결표 앞의 기호가 $ 에서 % 로 바뀐 것을 볼 수 있다. 터미널을 다시 껐다 켜도 % 가 나오면, 성공적으로 zsh 로 변경한 것이다.

2. oh-my-zsh 설치

사실 zsh 만으론 딱히 뭘 할 수가 없다. 예쁜 테마와 실용적인 플러그인 사용을 위해선 oh-my-zsh 를 설치해야 한다. 설치 방법도 무척이나 간단하다:

sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

이게 끝이다. 입력하고 나면 기본 쉘 변경을 묻는데 그냥 Y 하면 된다:

그럼 아래와 같이 쉘이 멋지게 바뀐다:

테마 변경

이제 본격적으로 테마를 변경해보겠다. 테마 변경도 무쟈게 쉽다. 이 사이트에 들어가서 마음에 드는 테마를 찾고 설정 값만 변경하면 된다.

마음에 드는 테마를 찾았다면 홈 디렉터리에 있는 .zshrc 파일(~/.zshrc)을 열어, 아래와 같이 ZSH_THEME 의 값을 원하는 테마명으로 바꾸면 된다:

필자는 bira 테마를 사용할 것이다. 독자들도 마음에 드는 테마를 찾아 사용하길 바란다.

플러그인 설치

oh-my-zsh 는 테마 뿐만 아니라 유용한 플러그인들도 제공한다. 가장 쓸만한 플러그인 두 가지를 추천하려 한다.

하이라이팅 기능 (zsh-syntax-highlighting)

git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting

위 명령어를 입력하면 자동으로 설치가 된다. 이제 아래와 같이 .zshrc 파일을 수정해서 플러그인을 등록하면 끝난다.

자동완성 기능 (zsh-autosuggestions)

두 번째 추천 플러그인은 zsh-autosuggestions 이다. 설치는 아래와 같이 진행 가능하다:

git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions

역시 .zshrc 파일에 플러그인 명을 쉽게 적용할 수 있다. 단 주의해야 할 점은 플러그인을 구분하기 위해선 공백(whitespace)만을 사용해야 한다는 것이다. 그 외에 구분자(,, ;, etc.) 들은 필요치 않다.


사실 위에서 소개한 플러그인들 때문에 oh-my-zsh 쓰는 것이라 해도 과언이 아니다. 정말 편리하고 좋다. 한번 쓰고 나면 bash 는 쳐다볼 수도 없게 된다.

3. 유용한 프로그램

필자가 쉘에서 작업할 때 유용하게 사용하는 프로그램을 소개하려 한다.

batcat

리눅스에서 보통 파일의 내용을 잠깐 흝어볼 때 cat 명령어를 사용하곤 한다. 이 cat 명령어의 결과를 더 아름답게 꾸며주는 프로그램이 있으니... 그것은 바로 batcat 이다.

힘의 차이가 느껴지는가? 설치도 매우 간단하다.

sudo dnf install bat

끝이다. 이제 마음껏 bat 를 휘둘러라.

fzf

탐색을 혁신적으로 바꿔준 프로그램이다. zsh 쉘과 연동하면 단축키까지 이용 가능하다. 그 자체로도 하나의 프로그램처럼 쓸 수 있기 때문에 플러그인 말고 프로그램으로 따로 빼두었다. 설치 후 oh-my-zsh 에 등록하면 단축키 사용 역시 가능하다.

설치는 아래와 같이 가능하다:

sudo dnf install fzf

이후 fzf 명령어 사용이 가능한데 fzf 는 사실상 문자열 매칭 프로그램이다. 가령 아래와 같이 사용 가능하다.

find * | fzf
직접 문자열 입력해서 탐색

나름 좋아 보이긴 하는데 이런 질문도 종종 나온다:

그냥 pipe 열어서 grep 으로 쏴주면 안되나?

틀린 얘긴 아니지만 fzfzsh 와 연동했을 때가 정말 쓰기 좋다.

.zshrc 파일에 아래와 같이 fzf 프로그램을 플러그인으로 등록한다:

그럼 아래와 같은 세 개의 명령어가 사용 가능해진다:

  • CTRL+T 현재 선택한 파일을 커맨드 라인에 붙여 넣는다.
  • CTRL+R 최근 사용했던 명령어를 커맨드라인에 붙여 넣는다.
  • ALT+C 선택한 디렉터리로 현재 디렉토리를 변경한다.

위 세 개의 명령어를 어떠한 명령어 입력 필요없이, 심지어 다른 명령어 입력 중에도 바로 사용할 수 있다.

4. 설정 파일

필자가 사용하는 zshrc 설정파일이다. 플러그인들과 bat 프로그램에 대해선 설명했는데, 쉘 가장 마지막 줄에 있는 프로그램들에 대해서는 아직 설명하지 못했다.

# If you come from bash you might have to change your $PATH.
# export PATH=$HOME/bin:/usr/local/bin:$PATH

# Path to your oh-my-zsh installation.
export ZSH="/home/mythos/.oh-my-zsh"

# Set name of the theme to load --- if set to "random", it will
# load a random theme each time oh-my-zsh is loaded, in which case,
# to know which specific one was loaded, run: echo $RANDOM_THEME
# See https://github.com/ohmyzsh/ohmyzsh/wiki/Themes
ZSH_THEME="bira"

# Set list of themes to pick from when loading at random
# Setting this variable when ZSH_THEME=random will cause zsh to load
# a theme from this variable instead of looking in $ZSH/themes/
# If set to an empty array, this variable will have no effect.
# ZSH_THEME_RANDOM_CANDIDATES=( "robbyrussell" "agnoster" )

# Uncomment the following line to use case-sensitive completion.
# CASE_SENSITIVE="true"

# Uncomment the following line to use hyphen-insensitive completion.
# Case-sensitive completion must be off. _ and - will be interchangeable.
# HYPHEN_INSENSITIVE="true"

# Uncomment the following line to disable bi-weekly auto-update checks.
# DISABLE_AUTO_UPDATE="true"

# Uncomment the following line to automatically update without prompting.
# DISABLE_UPDATE_PROMPT="true"

# Uncomment the following line to change how often to auto-update (in days).
# export UPDATE_ZSH_DAYS=13

# Uncomment the following line if pasting URLs and other text is messed up.
# DISABLE_MAGIC_FUNCTIONS="true"

# Uncomment the following line to disable colors in ls.
# DISABLE_LS_COLORS="true"

# Uncomment the following line to disable auto-setting terminal title.
# DISABLE_AUTO_TITLE="true"

# Uncomment the following line to enable command auto-correction.
# ENABLE_CORRECTION="true"

# Uncomment the following line to display red dots whilst waiting for completion.
# Caution: this setting can cause issues with multiline prompts (zsh 5.7.1 and newer seem to work)
# See https://github.com/ohmyzsh/ohmyzsh/issues/5765
# COMPLETION_WAITING_DOTS="true"

# Uncomment the following line if you want to disable marking untracked files
# under VCS as dirty. This makes repository status check for large repositories
# much, much faster.
# DISABLE_UNTRACKED_FILES_DIRTY="true"

# Uncomment the following line if you want to change the command execution time
# stamp shown in the history command output.
# You can set one of the optional three formats:
# "mm/dd/yyyy"|"dd.mm.yyyy"|"yyyy-mm-dd"
# or set a custom format using the strftime function format specifications,
# see 'man strftime' for details.
# HIST_STAMPS="mm/dd/yyyy"

# Would you like to use another custom folder than $ZSH/custom?
# ZSH_CUSTOM=/path/to/new-custom-folder

# Which plugins would you like to load?
# Standard plugins can be found in $ZSH/plugins/
# Custom plugins may be added to $ZSH_CUSTOM/plugins/
# Example format: plugins=(rails git textmate ruby lighthouse)
# Add wisely, as too many plugins slow down shell startup.
plugins=(
	git
	zsh-autosuggestions
	zsh-syntax-highlighting
	fzf
)

source $ZSH/oh-my-zsh.sh

# User configuration

# export MANPATH="/usr/local/man:$MANPATH"

# You may need to manually set your language environment
# export LANG=en_US.UTF-8

# Preferred editor for local and remote sessions
# if [[ -n $SSH_CONNECTION ]]; then
#   export EDITOR='vim'
# else
#   export EDITOR='mvim'
# fi

# Compilation flags
# export ARCHFLAGS="-arch x86_64"

# Set personal aliases, overriding those provided by oh-my-zsh libs,
# plugins, and themes. Aliases can be placed here, though oh-my-zsh
# users are encouraged to define aliases within the ZSH_CUSTOM folder.
# For a full list of active aliases, run `alias`.
#
# Example aliases
# alias zshconfig="mate ~/.zshrc"
# alias ohmyzsh="mate ~/.oh-my-zsh"
function cs () {
	if [ $# -ne 1 ]; then
		ls
	else
		cd $1
		ls
	fi
}

alias cat="bat"
alias vi="nvim"
alias vim="nvim"
alias cl="clear"
export PATH=/home/mythos/Documents/scripts:$PATH

# when shell is started
# neofetch
fortune -s | cowsay -f dragon-and-cow | boxes -d spring | lolcat -r

다음의 프로그램들이 궁금하다면 필자가 작성한 글을 확인하길 바란다. zshrc 의 가장 마지막 줄이 실행되면 다음과 같은 결과가 쉘 진입 시 나타나게 된다:

profile
2000.11.30

2개의 댓글

comment-user-thumbnail
2023년 8월 17일

좋은정보 감사합니다

1개의 답글