Fzf for the Win

Linux Bash fzf

fzf is a command line fuzzy finder that can be used to automatically filter a list of items. Think of it as an interactive search tool, where items get filtered as you type characters in your terminal.

The video below shows a basic interaction using a list or files from the fd search utility:

fzf can also be used with other Bash tasks, like history, ssh and even file/dir completion. The GitHub page has a lot documentation on how to implement auto completion.

You can also use the --preview option to output the current selection into a preview box, and even call a command to be used with that value. For example, we can preview all the files in a folder by searching for files (with find or fd), piping the output to fzf, and then using a program like cat (on the example below I’m using bat, which is a clone of cat with the addition of syntax highlight and other cool things) to preview the files.

fd -d 1 -t f | fzf --preview 'bat --color "always" {}' --preview-window=right:60%

I’ve covered only the very basic usage for fzf, but it should give you an idea of how powerful this finder utility is. On future posts I’m going to cover other use cases, like the git workflow that I use.


References:

Bash Theme Powerline 2column

Bash Linux

powerline-2column is a simple powerline like prompt for Bash that displays information in two columns.

The prompt provides the following information:

Left Side

  • Folder icon for:
    • Git folder with provider icon
    • Home folder
    • Dropbox folder
  • Username
  • Hostname (when connecting via SSH)
  • Current path
  • Git status
    • untracked
    • uncommited
    • ahead/behind

Right Side

  • Previous exit code
  • Battery status
  • sudo cached credentials
  • Time

Check out some of the things it can do on the video below.

You can download a copy from my GitHub repo - https://github.com/victorbrca/powerline-2column

Count Lines of Codes With Cloc

Linux Programming

Have you ever had the nead to count the lines of code in a project or a folder? If you did, or want to, cloc is a neat utility that can help with just that.

As per it’s description on the GitHub page, “cloc counts blank lines, comment lines, and physical lines of source code in many programming languages”. It displays a summary of file types and counted files, and then it breaks down a list of lines of code per language.

Here’s the output from my bash-config project:

 ~/Git/bash-config $ cloc .
      45 text files.
      43 unique files.                              
       4 files ignored.

github.com/AlDanial/cloc v 1.80  T=0.29 s (146.6 files/s, 10151.3 lines/s)
--------------------------------------------------------------------------------
Language                      files          blank        comment           code
--------------------------------------------------------------------------------
Bourne Again Shell               38            355            440           1525
Bourne Shell                      2             41             32            276
Markdown                          2             52              0            187
--------------------------------------------------------------------------------
SUM:                             42            448            472           1988
--------------------------------------------------------------------------------

And here’s a view of a more complex project with multiple languages:

     346 text files.
     338 unique files.                                          
     110 files ignored.

github.com/AlDanial/cloc v 1.80  T=0.75 s (319.4 files/s, 45153.6 lines/s)
----------------------------------------------------------------------------------------
Language                              files          blank        comment           code
----------------------------------------------------------------------------------------
Bourne Shell                            102           3417           3753          14810
Bourne Again Shell                       28           1209           1040           5480
XML                                       3             88             13            713
DOS Batch                                 6             90             24            448
Markdown                                  5            122              0            411
Visual Basic                              5             56             31            331
ERB                                      66             69              0            322
HTML                                      4             12             62            182
SQL                                      10             65            203            166
Korn Shell                                2             33             65            115
Velocity Template Language                1              0              0            114
Python                                    3             18             45             59
CSS                                       1              1              1             24
Java                                      1              3              3             22
Ruby                                      1              6              5             11
----------------------------------------------------------------------------------------
SUM:                                    238           5189           5245          23208
----------------------------------------------------------------------------------------

cloc is available on many distros default repo, as well as npm install. To install it use:

sudo apt install cloc                  # Debian, Ubuntu
sudo yum install cloc                  # Red Hat, Fedora
sudo dnf install cloc                  # Fedora 22 or later
sudo pacman -S cloc                    # Arch
sudo emerge -av dev-util/cloc          # Gentoo https://packages.gentoo.org/packages/dev-util/cloc
sudo apk add cloc                      # Alpine Linux
sudo pkg install cloc                  # FreeBSD
sudo port install cloc                 # Mac OS X with MacPorts
brew install cloc                      # Mac OS X with Homebrew
choco install cloc                     # Windows with Chocolatey
scoop install cloc                     # Windows with Scoop

How to Migrate Unifi Controller

Linux Networking

Quick instructions on how to migrate a Unifi controller on Linux. Note that it requires SSH access to the AP and a bit of downtime.

a. Logon to your old Unifi controller, go to Settings=>Auto Backup and download a backup

Note: Force a new backup if you have new changes

b. Browse to your AP, write down the IP address

c. Select the AP, go to Config=>Manage Device=>Forget this device and click on Forget (click ok on the alert)

d. Login to the new controller and on the first screen restore the backup you saved on step a

e. Once the controller is back up, SSH into the AP with the default user (ubnt:ubnt or root:ubnt) and run the following command (change [controler_ip] for the IP of your controller)

set-inform http://[controller-ip]:8080/inform

f. On the new controller, under devices, the AP should be showing for ‘adoption’. Click on ADOPT

How to Install and Configure Vim Plug

vim bash Linux

Quick step by step instructions to get you started with the Vim plugin manager vim-plug.

a- Install the vim-plug package (Arch) from the AUR repo

aur/vim-plug 0.10.0-1 [installed] (19) (0.05)
    A vim plugin manager

b. Configure your ~/.vimrc with a section for the new plugins. This is where you will define all the plugins that you want to use. I would also add this section at the top of your file.

"==============================================================================
" Plugin Manager
"==============================================================================

call plug#begin('~/.vim/plugged')

" Initialize plugin system
call plug#end()

c. Add a plugin to the plugin section. If you don’t know what to add you can start with vim-illuminate, which highlights multiple instances of the same word that is under the cursor.

call plug#begin('~/.vim/plugged')

"Vim plugin for selectively illuminating other uses of the current word under the cursor
"https://github.com/RRethy/vim-illuminate
Plug 'RRethy/vim-illuminate'

" Initialize plugin system
call plug#end()

d. Now open a file with vim, or if you already have a file open, source your vimrc with :source ~/.vimrc

e. Now that we have the plugin section and a plugin defined, call vim-plug to install your plugin with :PlugInstall

And that’s it! You are ready to start using your new plugin.

Whenever you want to install a plugin, repeat steps “c” through “e”.

code with