Welcome to new things

[Technical] [Electronic work] [Gadget] [Game] memo writing

Using peco/fzf has made the selection process in the Linux shell dramatically easier.

I was looking for some kind of tool to help me select clusters for Kubernetes, and I saw a tool called peco.

PECO is an independent tool that has nothing to do with Kubernetes, but when I used it, it was extremely useful!

Unfortunately, when I use peco with WSL, the shell color is not right and I can't operate keys through remote desktop, I decided to use fzf, a tool equivalent to "peco", based on the following article.

What is PECO FZF?

PECO/FZF is an extreme example,

"A tool that interactively selects the screen input from standard input and outputs the selected lines."

It is. Hmmm. I don't really understand from this....

For example, if the "ls" execution result screen is piped to "fzf" as input, a file selection UI can be easily created.

ls -lFa | fzf

Using peco/fzf dramatically eases the selection process in the Linux shell.

Multiple selections can be made with the "-m" option.

ls -lFa | fzf -m

Using peco/fzf dramatically eases the selection process in the Linux shell.

The selected line is output as a whole line, but sometimes all you actually want is the filename.

As is the case with both fzf and peco, there is no column parsing function, and to extract just the filename, you must pipe it to awk or other software.

For example, since the filename is the ninth column, it can be retrieved with "awk '{print $9}'".

ls -lFa | fzf -m | awk '{print $9}'

Using peco/fzf dramatically eases the selection process in the Linux shell.

This makes it easy to create a UI that interactively selects and deletes files.

ls -lFa | fzf -m | awk '{print $9}' | xargs rm

Using peco/fzf dramatically eases the selection process in the Linux shell.

Moreover, if you enter a character in the middle of the text, it will be narrowed down and displayed by the character you entered!

Using peco/fzf dramatically eases the selection process in the Linux shell.

It is very convenient!

install

I installed it with "Using git" in the ReadMe of Official Site.

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

When you are asked several questions and you answer "yes" appropriately, a file "\~/.fzf.bash" will be created and ".bashrc" will be used to read it.

The executable file is in the folder "fzf" downloaded from git, and "\~/.fzf.bash" will pass through the path.

key binding

KEY ACTION
Ctrl-p Move Up
Ctrl-n Move Down
Ctrl-i selection toggle
Enter selection final
Ctrl-c cancel

customize

The manual is displayed with man fzf.

If the environment variable FZF_DEFAULT_OPTS is set to a customized option, it will be set as the default option.

I preferred the default settings of PECO, so I customized it as follows.

  • -m": Multiple choice
  • "---reverse": means inverse
  • --ansi": reflects the color of the input
  • --color=bg+:#666666": whiter background for the cursor line

.bashrc

export FZF_DEFAULT_OPTS="-m --reverse --ansi --color=bg+:#666666"

shortcut

Since it is troublesome to write commands every time, it seems to be a common usage to create functions and register them in .bashrc or other files.

Also, even if you cancel the selection with fzf, the pipeline will run to the end, so you should make sure to check the selection result.

For example, the command to select and remove a docker image

.bashrc

function docker-rmi(){
    selection=$(docker images | fzf | awk '{print $3}')
    [ -n "$selection" ] && docker rmi $selection 
}

Using peco/fzf dramatically eases the selection process in the Linux shell.

impressions

In working with shells,

  1. (display at) a glance
  2. Select ID and copy
  3. Paste the ID into another command and run it.

I often have to do this, but it's a chore every time....

Thanks to PECO/FZF, my work efficiency has gone through the roof! Thank you!

It would have been even better if there was an option to "output the \~th column" so I would not have to use awk. Also, it would have been nice to be able to specify a command to be executed when selected, so that fzf alone would be complete.

But maybe this simplicity is good.

Reference Articles

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com

www.ekwbtblog.com