From 15fba89c8499c35172777dc3a54dc52da0cee25b Mon Sep 17 00:00:00 2001 From: PowerUser64 Date: Sat, 11 Dec 2021 17:56:18 -0800 Subject: [PATCH] zsh: changes to cdt() and cds(), and bug fix for rmswp() when the swapfile isn't mounted --- .zshrc | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/.zshrc b/.zshrc index 959c56e..eb70c2a 100644 --- a/.zshrc +++ b/.zshrc @@ -196,20 +196,32 @@ { # cd search: cd to a directory, given part of its name # (also can take arguments for an `fd` commnd) + # (also can cd to a file if `-t f` is passed) cds() { if ! [ -z "$1" ];then - cd "$(fd --max-results=1 -t d $@)" && pwd + DIR="$(fd --max-results=1 -t d $@)" + if [ -f "$DIR" ];then + cd "$(dirname "$DIR")" && pwd + else + cd "$DIR" && pwd + fi else echo "$0: no arguments provided" return 1 fi } - # cd to: cd to the containing dir of a file or folder, given part of its name + # cd find (interactive): cd to the containing dir of a file, or inside a folder, given part of its name + # Basically, an interactive version of what's above # can take an `fd` command # requires that `fzy` is installed - cdt() { - cd "$(dirname "$(fd $@ | fzy)")" && pwd + cdf() { + DIR="$(fd $@ | fzy)" + if [ -f "$DIR" ];then + cd "$(dirname "$DIR")" && pwd + else + cd "$DIR" && pwd + fi } # cd all: opens a new shell in each of the directories below the one you're in, so @@ -476,7 +488,7 @@ # Deletes the swapfile created by mkswp() rmswp() { - sudo swapoff -v /swapfile && + sudo swapoff -v /swapfile sudo rm /swapfile }