Fuzzy File Search in Linux Console

Fuzzy file search in Linux console

You may find fzf useful. It's a general purpose fuzzy finder written in Go that can be used with any list of things: files, processes, command history, Git branches, etc.

Its install script will setup a Ctrl+T keybinding for your shell. Pressing Ctrl+T lets you fuzzy-search for a file or directory and put its path on your console.

The following GIF shows example usage of fzf including its Vim integration:

Animation of using FZF

Find file modes in console (fuzzy completion)

I've started work on a bash completion module to do (non-recursive) fuzzy completion. It currently works, but with a few quirks I'm trying to iron out.

https://github.com/mgalgs/fuzzy_bash_completion

Usage:

source fuzzy_bash_completion
source fuzzy_log_setup_functions

# easy-setup for many commands if you use the bash_completion package
fuzzy_replace_filedir_xspec
# or, for individual commands:
fuzzy_setup_for_command cd # set up fuzzy completion for cd

More usage info on the github page.

How to achieve AJAX(interactive) kind of SEARCH in LINUX to FIND files?

Stumbled aross this old question, found it interesting and thought I'd give it a try. This BASH script worked for me:

#!/bin/bash
# Set MINLEN to the minimum number of characters needed to start the
# search.
MINLEN=2
clear
echo "Start typing (minimum $MINLEN characters)..."
# get one character without need for return
while read -n 1 -s i
do
# get ascii value of character to detect backspace
n=`echo -n $i|od -i -An|tr -d " "`
if (( $n == 127 )) # if character is a backspace...
then
if (( ${#in} > 0 )) # ...and search string is not empty
then
in=${in:0:${#in}-1} # shorten search string by one
# could use ${in:0:-1} for bash >= 4.2
fi
elif (( $n == 27 )) # if character is an escape...
then
exit 0 # ...then quit
else # if any other char was typed...
in=$in$i # add it to the search string
fi
clear
echo "Search: \""$in"\"" # show search string on top of screen
if (( ${#in} >= $MINLEN )) # if search string is long enough...
then
find "$@" -iname "*$in*" # ...call find, pass it any parameters given
fi
done

Hope this does what you intend(ed) to do. I included a "start dir" option, because the listings can get quite unwieldy if you search through a whole home folder or something. Just dump the $1 if you don't need it.
Using the ascii value in $n it should be easily possible to include some hotkey functionality like quitting or saving results, too.

EDIT:

If you start the script it will display "Start typing..." and wait for keys to be pressed. If the search string is long enough (as defined by variable MINLEN) any key press will trigger a find run with the current search string (the grep seems kind of redundant here). The script passes any parameters given to find. This allows for better search results and shorter result lists. -type d for example will limit the search to directories, -xdev will keep the search on the current file sytem etc. (see man find). Backspaces will shorten the search string by one, while pressing Escape will quit the script. The current search string is displayed on top. I used -iname for the search to be case-insensitive. Change this to `-name' to get case-sensitive behaviour.

How to find fuzzy matched commands in bash

the max limit is 15, you can temporary change this as below;

find CommandNotFound.py

sudo find / -name "CommandNotFound.py"

open this file with root privilege;

sudo gedit /usr/lib/python3/dist-packages/CommandNotFound/CommandNotFound.py

change max_len 15 to 150 ;

def print_spelling_suggestion(self, word, min_len=3, max_len=150): 

or change as below;

...
#if len(possible_alternatives) > max_len:
# print(_("No command '%s' found, but there are %s similar ones") % (word, len(possible_alternatives)), file=sys.stderr)
#elif len(possible_alternatives) > 0:
if len(possible_alternatives) > 0:
print(_("No command '%s' found, did you mean:") % word, file=sys.stderr)
for (w, p, c) in possible_alternatives:
print(_(" Command '%s' from package '%s' (%s)") % (w, p, c), file=sys.stderr)
...

test

$ icc
No command 'icc' found, did you mean:
Command 'hcc' from package 'uhexen2' (multiverse)
Command 'hcc' from package 'lam4-dev' (universe)
Command 'kcc' from package 'kcc' (universe)
Command 'fcc' from package 'fcc' (universe)
Command 'ico' from package 'x11-apps' (main)
Command 'zcc' from package 'z88dk-bin' (universe)
Command 'gcc' from package 'gcc' (main)
Command 'cc' from package 'clang-3.4' (universe)
Command 'cc' from package 'clang-3.5' (universe)
Command 'cc' from package 'tcc' (universe)
Command 'cc' from package 'clang-3.3' (universe)
Command 'cc' from package 'gcc' (main)
Command 'ecc' from package 'ecere-dev' (universe)
Command 'rcc' from package 'qtchooser' (main)
Command 'iac' from package 'libpolyorb2-dev' (universe)
Command 'xicc' from package 'xicc' (universe)
Command 'inc' from package 'nmh' (universe)
Command 'inc' from package 'mailutils-mh' (universe)
Command 'tcc' from package 'tcc' (universe)
Command 'ick' from package 'intercal' (universe)
Command 'ucc' from package 'exult-studio' (multiverse)
Command 'bcc' from package 'bcc' (main)
Command 'icp' from package 'renameutils' (universe)
Command 'ica' from package 'italc-client' (universe)
Command 'irc' from package 'epic5' (universe)
Command 'irc' from package 'epic4' (universe)
Command 'irc' from package 'ircii' (universe)
Command 'imc' from package 'italc-management-console' (universe)
Command 'ncc' from package 'tinyos-tools' (universe)
Command 'scc' from package 'scheme2c' (universe)

How do I fuzzy find all files containing specific text using ripgrep and fzf and open it VSCode

The --vimgrep option to rg returns just what the doctor ordered. That's what I used in the vscode extension issue request you put in:

https://github.com/rlivings39/vscode-fzf-quick-open/commit/101a6d8e44b707d11e661ca10aaf37102373c644

It returns data like:

$ rg --vimgrep
extension.ts:5:5:let fzfTerminal: vscode.Terminal | undefined = undefined;
extension.ts:6:5:let fzfTerminalPwd: vscode.Terminal | undefined = undefined;

Then you can cut out the first 3 fields and pass them to code -g:

rg --vimgrep --color ansi | fzf --ansi --print0 | cut -z -d : -f 1-3 | xargs -0 -r code -g

Fuzzy file searching by directory?

I use the feature "Search in Everywhere" in RubyMine.

Here http://mrhaki.blogspot.ru/2014/02/search-for-anything-with-search.html description.



Related Topics



Leave a reply



Submit