Giter VIP home page Giter VIP logo

fzf-marks's Introduction

fzf-marks

This plugin can be used to create, delete, and navigate marks in bash and zsh. It depends on Junegunn Choi's fuzzy-finder fzf.

(This video was generated with screenkey -g $(slop -n -f '%g') and simplescreenrecorder.)

Installation

Zsh

If you use zsh, I recommend installing with a plugin manager. In the case of zgen, for example, simply add the following line to your plugin list:

zgen load urbainvaes/fzf-marks

Fish

If you use fish, then you can use fisher:

fisher install urbainvaes/fzf-marks

Bash

If you use bash, or if you use zsh without a plugin manager, source the file fzf-marks.plugin.bash or fzf-marks.plugin.zsh from your shell startup file to enable the plugin.

Bash installation example:

# Clone the git repository in the current directory
git clone https://github.com/urbainvaes/fzf-marks.git

# Add a line to ~/.bashrc to load the plugin whenever bash starts in interactive mode
echo "source $PWD/fzf-marks/fzf-marks.plugin.bash" >> ~/.bashrc

# Source the plugin now so we don't have to restart bash to start using it
source fzf-marks/fzf-marks.plugin.bash

Enabling completion:

In bash, completion should be available automatically. In zsh, completion needs to be enabled explicitly before sourcing the plugin. This is usually done automatically by plugin managers, but it can also be achieved manually with the following command in your .zshrc.

autoload -Uz compinit && compinit

Usage

The script exposes two functions:

  • mark <mark>, to register a new mark to the current directory;
  • fzm [<optional-initial-query>], to jump to or delete a mark using fzf.

Most of the keybindings in the search window are the default fzf ones. The only additions are

  • ctrl-y, to jump to a match;
  • ctrl-t, to toggle a match for deletion;
  • ctrl-d, to delete selected matches.

By default, the plugin binds the key ctrl-g to fzm.

Customization

Config Default Description
FZF_MARKS_FILE ${HOME}/.fzf-marks File containing the marks data
FZF_MARKS_COMMAND fzf --height 40% --reverse Command used to call fzf
FZF_MARKS_JUMP \C-g (bash) or ^g (zsh) Keybinding to fzm
FZF_MARKS_COLOR_LHS 39 (default) ANSI color code of left-hand side
FZF_MARKS_COLOR_RHS 36 (cyan) ANSI color code of right-hand side
FZF_MARKS_COLOR_COLON 33 (yellow) ANSI color code of separator
FZF_MARKS_NO_COLORS 0 Set this to 1 to disable colors
FZF_MARKS_KEEP_ORDER 0 Set this to 1 to keep order of marks

See e.g. here for a description of ANSI color codes.

FAQ

Question: Is it possible to limit the fzf search to the mark label, i.e. to exclude the path from the search?

Yes, this is possible by passing the options -n (for the field number to use for the search) and -d (for the delimiter) to fzf. For example,

FZF_MARKS_COMMAND="fzf --height 40% --reverse -n 1 -d ' : '"

License

MIT

fzf-marks's People

Contributors

3ximus avatar aaronjensen avatar akinomyoga avatar awerebea avatar brendanfalk avatar cdlm avatar cibinmathew avatar crisidev avatar dguo avatar joshuacrewe avatar leonb avatar marsoft avatar michaelmior avatar molovo avatar orestisfl avatar thiagotalma avatar thomd avatar urbainvaes avatar yuzhy8701 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

fzf-marks's Issues

front end to fzm

Hi, thanks for fzf-marks, I use it all the time.

I have wrapped it up in something that makes it more practical for me and maybe makes it more practical for other.

Let me know if something isn't clear.

cdm ()
{
if [[ ${#@} == 0 || ${#@} -gt 1 ]] ; then
fzm
else
matches=$(cat $FZF_MARKS_FILE | awk '{print $1}' | grep "$1" | wc -l)

if [[ $matches == 0 ]] ; then
	fzm
elif [[ $matches -gt 1 ]] ; then
	directory=$(_color_marks <<< $(perl -ane '$F[0] =~ /'$1'/ and print $_' $FZF_MARKS_FILE) | fzf --ansi --height 50% --reverse) 
	[[ $? == 0 ]] && cd $(echo "$directory" | awk '{print $3}')
else
	directory=$(perl -ane '$F[0] =~ /'$1'/ and print $F[2]' $FZF_MARKS_FILE) 
	[[ -n "directory" ]] && cd "$directory" || fzm "$1"
fi

fi
}

_cdm_completion()
{
COMPREPLY=( $( compgen -W "$(cat $FZF_MARKS_FILE | awk '{print $1}' | ([[ -n $2 ]] && grep $2 || cat | tr '
' ' '))" -- ${COMP_WORDS[$COMP_CWORD]} ))
}

[Feature Request] Option to direct cd bookmark in case of exact match

Hey,

first thank you for this cool plugin!

As I just started to use it I already noticed a part of my workflow from a previous shell plugin manager that did not translate well to fzf-marks for me. If I type $ fzm abc and there are many bookmarks who's bookmark name matches abc, it's completely reasonable to open up the FZF selector window (current behaviour).

However in the case it's already an exact match of a bookmark, that is I have a bookmark with name abc, I would expect that I directly cd to that bookmark instead of showing suggestions for other partial matches.

This is important to be able to work fast in the shell. If I know that I want to go to bookmark abc, then $ fzm abc should be enough to take me there. Currently the window will pop up on a exact name match and I need to press enter to go there.

As this is maybe not the behaviour that everyone desires, I would propose that we introduce a new opt-in configuration option:

Config Default Description
FZF_MARKS_JUMP_EXACT_MATCH false With fzm, cd to bookmark directly if the optional-initial-query is an exact match for a bookmark

Fish shell plugin is currently broken

❯ fish -v
fish, version 3.1.0
❯ ~/.config/fish/functions/fzf-marks.plugin.fish (line 1): Invalid redirection target:
_color_marks < $FZF_MARKS_FILE | $FZF_MARKS_COMMAND \
             ^
in command substitution
        called on line 93 of file ~/.config/fish/functions/fzf-marks.plugin.fish
in function 'fzm'
~/.config/fish/functions/fzf-marks.plugin.fish: Unknown error while evaluating command substitution

keybinding disable

I have remapped the key binding for fzm to ^k , which works fine for me, but I noticed that ^g is still active.

Is there a way to completely disable the ^g key mapping completely?

(feature request) Inserting marked directory in cmd rather than jump to it

First of all, thanks again for fzm, using it daily!

One thing that could be a nice improvement is placing a selected dir into the command line rather than jumping to it.
I.e. say just want to list the content of a marked dir, being on the shell, typying ls hitting ctrl-G to fire up fzm,
I could imagine there to be an additional option to just copy the marked dir to the shell. Ending up in ls myselecteddir on the cmd.

I could have look to implement it myself if you don't have time etc, but would have to get proficient first.

Naivly, I'd imagine to create a new function similar to the jump function, but instead of cd pasting the dir on the cmd:

cd "${jumpdir}" || return

I.e. instead of cd using echo, which doesn't quite work, but I wouldn't know to make it work, maybe I'm just missing the right command..

(Feature Request) Open marked files with default $EDITOR

Hi, first of all.... this is amazing, THANK YOU!!!
When I saw this initially, I thought, as it can mark files as well as directories, that pressing ENTER on a file would open the file just as it does a directory, but it appears that it just navigates to the file's directory instead.
I think it would be alot nicer to open any mark, not just directories, otherwise marking files is not needed, we can already just mark the directory instead which is better.
For example: I marked my 'zshrc' & 'bashrc' when I could've just marked '/etc' instead which gives me access to both files through one shortcut.

My feauture request would make FZM's file marking a very valuable feature!
FZM could default to: default $EDITOR (if set)
If not: look for 'nano' & 'vi/vim' as both of these editors are pre-installed in most distros now unless...
a variable for us to set like: "FZM_EDITOR" to let us pick our own, this could serve an extra purpose also! adding this means we could fill up FZM with all of our project's files and have them open in vim, but still have my default editor set as micro for opening normal txt/rc/configs when outside of FZM.
Thank you again.

Use case with tmux - add new window based on marks

I'd like share with you guys little script that I use on a daily basis. It spawns new tmux's window based on mark entries. I hope someone find this useful. 👀 🙏 🎉

#!/usr/bin/env bash
# Author: qaraluch - 07.2020 - MIT
# Add new window to the tmux session
# based on fzf-marks entries
readonly _pName=$(basename $0)
IFS=$'\n' readonly fzfMarksEntry=("$(cat ${FZF_MARKS_FILE} | fzf-tmux --exit-0)")
readonly name=$(awk -F':' '{print $1}' <<< "$fzfMarksEntry" | sed 's/[ \t]\?//g')
readonly path=$(awk -F':' '{print $2}' <<< "$fzfMarksEntry" | sed 's/[ \t]\?//g')
if [[ -d "$path" ]]; then
  tmux new-window -d -n "${name}" -c "${path}"
else
  echo "[ ${_pName} ] Destination dir for new tmux window seems does not exist... :o"
fi

The last 3 lines of output are cut off

Issue

I have a script which runs ls every time the directory is changed. When I jump to a mark by pressing <C-g>, the last 3 lines of ls are cut off (as well as a part of the prompt).

Screenshots

fzf_issue1
fzf_issue2
fzf_issue3
Sorry for huge screenshots ¯\_(ツ)_/¯

could not delete multiple marks, and not showing delete marks

Thanks for this handy plugin. I found it is not possible to delete multiple marks, only one is deleted.
And it do not show the deleted paths after the message " The following mark has been deleted ".

I installed the plugin with fisher, fish version v3.4.1, fzf v0.27.2. Thanks!

cd hooks aren't triggered after jumping

For example, if you use direnv you have to hit enter once on the empty prompt before it catches that you changed directory. I'm not sure if I've got something wrong in my setup or if this is just a side effect of how fzf-marks actually changes directories...

readlink -f is not portable

The use of readlink -f at

fname=$(readlink -f "${FZF_MARKS_FILE}")
relies on the GNU implementation and is broken with the BSD readlink available by default on macOS, whose man page says:

     -f format
             Display information using the specified format.  See the FORMATS section for a description of valid formats.

GNU readlink is available but it will probably be named greadlink (as installed via Brew)

Adding bash install instructions

Using bash shell it wasn't obvious on how to get it to work based on current instructions.
I think the readme could benefit from something like along this lines:

git clone https://github.com/urbainvaes/fzf-marks.git
cd fzf-marks
source fzf-marks.plugin.bash # can be put in  
# make it start up on log:
echo "source $PWD/fzf-marks.plugin.bash" >> ~/.profile

Anyway, really nice piece of software!

fzf-marks.plugin.zsh:177 command not found: compdef

Seems that after last month update I keep getting: 😱 🙈

~/.cache/zsh/plugs/fzf-marks/fzf-marks.plugin.zsh:177: command not found: compdef

compdef _fzm fzm

I do not really know what is going on because I've got zsh's completion system activated in my zshrc file:

autoload -Uz compinit
compinit

and after spawning shell the compdef command is available?

btw. I appreciate your work! thx 💚

Dup marks

Can't reproduce but sometimes plugin give dup mark when trying to use its functions.

Provide more CLI commands

Hi, first off, thanks for a fantastic tool, I've used it with great joy for some 1-2 years!

Many many years ago, like 10-13 or so, I started using bashmarks which provide some very neat one-letter shell commands like g mark, s mark etc.

Through the years I've used different shell bookmarks tools like jump & cd-bookmark and I've always kept the original bashmark-style commands by creating similar aliases to the other bookmarking tools, as they are so convenient!

fzf-marks stared a new area of bookmarking, with the interactive menu. When searching for a bookmark I don't know the exact name of, the the fzf interactive menu is perfect! When I know the exact name, it's often easier and faster to use a direct command.

Thus I've used aliases & functions also with fzf-marks, in the style of Bashmarks. This is what I have in my shell startup file:

alias g='fzm'                   # Jump to mark (provided as argument)
alias s='mark'                  # Save current dir as mark (provided as argument)
alias e="vi $FZF_MARKS_FILE"    # Edit bookmarks file. 
alias l="sort $FZF_MARKS_FILE"  # List all bookmarks

_fzf_marks_delete() {
  local mark="$1"
  
  if ! grep -q "^$mark : " $FZF_MARKS_FILE; then
	  echo "$mark is not in the bookmarks" >&2
	  return 2
  fi
  sed -i.bak -e "/^${mark} : /d" "$FZF_MARKS_FILE" && rm "$FZF_MARKS_FILE.bak"
}
alias d='_fzf_marks_delete'  # Delete mark (provided as arg)

_fzf_marks_print() {
  local mark="$1"
  
  if ! grep -q "^$mark : " $FZF_MARKS_FILE; then
	  echo "$mark is not in the bookmarks" >&2
	  return 2
  fi
	  grep "^$mark :" $FZF_MARKS_FILE | awk '{print $3}'
  }
alias p='_fzf_marks_print'  # Print a mark (provided as arg)

This works, but it would be neat if there was built-in support by this tool to do operations like edit, list and delete a mark!

Note: I'm not requesting that this tool should adopt the bashmark-style command names like g, s etc., that I can always create myself, but a requst to provide some switches or subcommands to fzfm or mark to do this. Maybe mark -d abookmark could delete a bookmark named abookmark for example.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.