From c39e41bc7f1443de462730f58b8aa27937c92b2e Mon Sep 17 00:00:00 2001 From: Gardouille Date: Tue, 1 Aug 2023 14:16:00 +0200 Subject: [PATCH] Change to dir only if not already in the dir --- zshrc | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/zshrc b/zshrc index 01c601c..c30f9c3 100644 --- a/zshrc +++ b/zshrc @@ -2194,7 +2194,7 @@ cf() { # Display the 50 first lines of the selected file with bat (batcat) # Move to the directory of the selected file # Open the selected file with vi -v() { +function v() { local files local dir local file @@ -2213,7 +2213,8 @@ v() { if [[ -n "${files}" ]] then dir=$(dirname "${files}") - cd -- "${dir}" + ### Change directory only if not already in the expected dir + test $(realpath "${dir}") != $(realpath .) && cd -- "${dir}" file=$(basename "${files}") vi -- "${file}" fi @@ -2246,7 +2247,8 @@ pdf() { if [[ -n $files ]] then dir=$(dirname "${files}") - cd -- "${dir}" + ### Change directory only if not already in the expected dir + test $(realpath "${dir}") != $(realpath .) && cd -- "${dir}" file=$(basename "${files}") "${PDF_VIEWER}" -- "${file}" fi @@ -2357,7 +2359,8 @@ odt() { if [[ -n $files ]] then dir=$(dirname "${files}") - cd -- "${dir}" + ### Change directory only if not already in the expected dir + test $(realpath "${dir}") != $(realpath .) && cd -- "${dir}" file=$(basename "${files}") command libreoffice --writer "${file}" & fi @@ -2387,7 +2390,8 @@ ods() { if [[ -n $files ]] then dir=$(dirname "${files}") - cd -- "${dir}" + ### Change directory only if not already in the expected dir + test $(realpath "${dir}") != $(realpath .) && cd -- "${dir}" file=$(basename "${files}") command libreoffice --calc "${file}" & fi @@ -2417,7 +2421,8 @@ odp() { if [[ -n $files ]] then dir=$(dirname "${files}") - cd -- "${dir}" + ### Change directory only if not already in the expected dir + test $(realpath "${dir}") != $(realpath .) && cd -- "${dir}" file=$(basename "${files}") command libreoffice --impress "${file}" & fi @@ -2448,7 +2453,8 @@ fopen() { if [[ -n $files ]] then dir=$(dirname "${files}") - cd -- "${dir}" + ### Change directory only if not already in the expected dir + test $(realpath "${dir}") != $(realpath .) && cd -- "${dir}" file=$(basename "${files}") command xdg-open "${file}" & fi