Installation and use of exa, fzf and bat software

   this article only deals with the simple use of the above software, which is strongly personal. For more comprehensive and detailed use, please refer to the official website.

1, What's good about the new software

1. cat,less =>bat

   the commonly used cat command is used to connect multiple files and print to standard output. Its disadvantages are:
   ① when viewing a large file, the text flashes quickly on the screen, so fast that you can't see the content at all. Although shortcut keys to control scrolling are provided, people often directly use Ctrl+c (interrupt) to terminate the command execution. Also use the method of cat + pipe character + less.
   ② the output content of cat itself does not have a line number, so you need to add the - n parameter to get it;
   ③ the input content of cat cannot match the search keyword, which can be realized with the help of cat + pipeline character + less.
   at this time, the advantages of bat are reflected. It itself is equivalent to the function of cat + pipe symbol + less. The above shortcomings can be solved. It also comes with vim's automatic highlighting and automatic paging function when the file content is too long.


2. ls => exa

   exa tries to become a more distinctive and user-friendly version of LS. It has powerful functions, is more friendly than ls, and the output results will be faster. Most importantly, it will not conflict with LS commands.
  see the official website for details exa.

3. ctrl+r => fzf

  it is an interactive Unix command line tool written in GO language. It can be used to find any list content, files, historical commands, locally bound host s, processes, Git branches, processes, etc. All command-line tools can generate a list, and the output can be searched and found on fzf through pipe.


2, Software installation

   the brief introduction is as follows. See the previous article for the detailed installation and use of conda Installation and use of conda under Linux

1. conda installation software

1,# Activate conda and enter the base environment
      conda/source activate
2,#Switch to the work environment to be installed
      conda activate work
3,#Installing software using conda
      mamba install bat exa fzf
4,# Software use
      which bat exa fzf

2. Add to bashrc

vim ~/.bashrc
## change command to alias
alias cat="*/Software/Miniconda/envs/test/bin/bat"
alias less="*/Software/Miniconda/envs/test/bin/bat"
alias ls="*/Software/Miniconda/envs/test/bin/exa"
## The addition of fzf is complex. The details are described in 3.2 software use fzf below

3, Use of software

1. exa

A blogger in the reference document explained the main parameters as follows:
(1) Display configuration

  • -1, – oneline: one entry per line
  • -G. – grid: display entries as a grid (default)
  • -l. – long: displays the details and attributes of the extension
  • -R. – recurse: recurse to the directory
  • -T. – recurse to tree as directory
  • -x. – across: sort the grid horizontally, not downward
  • -F. – classify: displays the type indicator by file name
  • – colo[u]r: when to use terminal colors
  • – colo[u]r-scale: highlight the level of file size
  • – icons: display icons
  • – no icons: do not display icons (always overwrite – icons)

(2) Filter configuration

  • -a. – all: Show hidden files and "point" files
  • -d. – list dirs: list directories like normal files
  • -50. – level=(depth): limits the depth of recursion
  • -r. – reverse: reverse the sort order
  • -s. – sort=(field): by which field
  • – group directories first: list directories in front of other files
  • -D. – only dirs: list directories only
  • – git ignore: ignore Files mentioned in gitignore
  • -1. – ignore glob = (globs): glob mode of the file to ignore (pipe delimited)

(3) Optional parameters with - l

  • -b. – binary: lists the file sizes with binary prefixes
  • -B. – bytes: lists the file size in bytes without any prefix
  • -g. – group: lists the groups of each file
  • -h. – header: add a header row for each column
  • -H. – links: lists the number of hard links per file
  • -i. – inode: list the inode number of each file
  • -m. – modified: use the modified timestamp field
  • -S. – blocks: lists the number of file system blocks per file
  • -t. – time=(field): which timestamp field to use
  • -u. – accessed: use the accessed timestamp field
  • -U. – created: use the created timestamp field
  • -@, – extended: lists the extended attributes and size of each file
  • – changed: use the changed timestamp field
  • – Git: lists the Git status of each file if it is tracked or ignored
  • – time style: how to format a timestamp
  • – no permissions: prohibit permission field
  • – octal permissions: lists the permissions of each file in octal format
  • – no filesize: disable file size field
  • – no user: disable user field
  • – no time: suppress the time field
## exa -hlg -s=time -r parameter is approximately equal to LS - lhta (commonly used by myself)
## Set shortcut keys for common parameters to facilitate subsequent use. Parameter meaning: list files and folders under the directory - l, display column information - h, display file group information - g, sort by time - s=time, reverse sort - r; 
    alias ll='exa -hlg -s=time -r'  
# 1. Display all files under the folder, including hidden files;
   ll -a /dir/
# 2. The contents of the displayed subdirectories and their contained subdirectories;
   ll  -R /dir/
# 3. The tree recursively displays the contents of the directory and its subdirectories;
   ll -T /dir/
# 4. The learning command recommended on the Internet displays other information, such as index node, file / directory size, block, user and group, etc.
   exa -abghHliS /dir/


2. fzf

   the most commonly used shortcut keys for fzf should be Ctrl + r and Ctrl + t. Ctrl-r is used to search in history, and Ctrl-t is used to search the contents of the current folder.

   fzf will start in "extended search" mode by default. In this mode, you can enter multiple search keywords separated by spaces, such as ^ music. MP3 $, sbtrkt! Fire

TokenMatch typeDescription
sbtrktfuzzy-matchMatch sbtrkt
^musicprefix-exact-matchStart with music
.mp3$suffix-exact-matchWith mp3 end
'wildexact-match(quoted)Accurately include wild
!fireinverse-exact-matchfire not included
!.mp3$inverse-suffix-exact-matchNo mp3 end

Common functions are as follows:

# The following uses are from the official website examples, and most of the reference documents are the translation of the official website examples;
# 1. Basic use, filter all files under the current path, and press enter to return to the selected file;
    fzf --preview 'cat {}' # Preview files under current path
# 2. fzf changes the full screen display to 40% of the specified height;
export FZF_DEFAULT_OPTS='--height 40% --reverse --border' 

# 3. Define function fd
# Define the function fd in bashrc to filter all folders under the current directory, and press enter to go directly to the directory; The more complex the directory structure is, the more obvious the effect is;
fd() {
  local dir
  dir=$(find ${1:-.} -path '*/\.*' -prune \
				  -o -type d -print 2> /dev/null | fzf +m) &&
  cd "$dir"

# 4. Define function fe
# Define the function fe in bashrc to filter all files in the current directory, press enter and directly vim open the selected file;
fe() {
  IFS=$'\n' out=("$(fzf --preview 'cat {}' --query="$1" --exit-0 --expect=ctrl-o,ctrl-e)")
  key=$(head -1 <<< "$out")
  file=$(head -2 <<< "$out" | tail -1)
  if [ -n "$file" ]; then
    [ "$key" = ctrl-o ] && open "$file" || ${EDITOR:-vim} "$file"

# 5. Define function fh
# Define the function fh in bashrc. Its function is approximately equal to ctrl+r, search for the command to be re executed from the historical command, and press enter to return to the selected command line;
# fh - repeat history
fh() {
  echo $( ([ -n "$ZSH_NAME" ] && fc -l 1 || history) | fzf +s --tac | sed -E 's/ *[0-9]*\*? *//' | sed -E 's/\\/\\\\/g')

# Since the function has been added to bashr, enter fzf, fd, fe and fh on the command line to realize their respective functions;

It can also be used temporarily at other times (hardly used):

#1. Change the default setting of fzf, change the full screen display to 40% of the customized height, preview the contents of the file where the cursor is located, and press enter to return to the selected file;
export FZF_DEFAULT_OPTS="--height 40% --layout=reverse --preview '(highlight -O ansi {} || cat {}) 2> /dev/null | head -500'"
# 2. Search the history of ps and history;
ps -ef | fzf
history | fzf


3. bat

# 1. In normal use, open one or more files, enter "/" to trigger the search function, and "q" to exit;
   bat file1 file2
# 2. The specified line of the output file is closed on the left and closed on the right;
   bat -r 10:15
# 3. View 165 languages supported by bat and their corresponding file formats;
   bat -L
# 4. It can be connected with commands such as ls with pipe symbol;
   ls -l | bat
# 5. bat results all contain the number of lines. At this time, sed can be used to print without the number of lines
   sed -n '10,15p' file


4, Reference documents

Software official website

  1. cat command
  2. bat software
  3. exa software
  4. fzf software

2. Related blogs

  1. Cool command line tools on linux/unix (1)
  2. Bat – A Cat Clone with Syntax Highlighting and Git Integration
  3. The linux exa command shows the file experience better than ls
  4. I didn't expect that the exa command is really so easy to use. It directly replaces ls
  5. Use fzf artifact to create Linux command line history that can be quickly queried and modified

Keywords: Python Linux Anaconda

Added by Cherry on Sat, 05 Mar 2022 18:49:07 +0200