Skip to content

Instantly share code, notes, and snippets.

@janoamaral
Last active November 15, 2020 15:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save janoamaral/ed433b6cf24e57d108173bcce9f449da to your computer and use it in GitHub Desktop.
Save janoamaral/ed433b6cf24e57d108173bcce9f449da to your computer and use it in GitHub Desktop.
Save and show bookmarks on Zsh
# bookmark.zsh
# Save and show bookmarks on Zsh
# ==================================================================================
# Dependencies: smenu (https://github.com/p-gen/smenu.git)
# Full article (in spanish) https://logico.ar/blog/2020/11/14/crear-bookmarks-en-zsh
# Usage
# Add this line to .zshrc
# source /path_to/bookmarks.zsh
# MIT License
# Copyright (c) 2020 Alejandro Amaral (PGP 12F8FA3688B6FE48)
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# Bookmark file location
BOOKMARK_FILE=~/dots/bookmarks/bookmarks
# Functions
# Shows the bookmarks menu
function bookmarks() {
RES=$(cut -d "," -f1 "$BOOKMARK_FILE" | smenu -n20 -q -c -b -g -d -N)
if [ ! -z "$RES" ]
then
cd `grep "^$RES" "$BOOKMARK_FILE" | cut -d "," -f2`
fi
zle reset-prompt
}
# Add current directory to the bookmarks file
function bookmark-this() {
echo "`basename $PWD`,$PWD" >> $BOOKMARK_FILE
echo "Bookmark added"
zle reset-prompt
}
# Bindings (atajos de teclado)
# Ctrl+n - Shows menu
zle -N bookmarks
bindkey '^n' bookmarks
# Ctrl+a - Adds current directory to bookmarks
zle -N bookmark-this
bindkey '^a' bookmark-this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment