Skip to content

Commit

Permalink
quitcd: make nnn env variable local
Browse files Browse the repository at this point in the history
  • Loading branch information
BeyondMagic committed Feb 2, 2024
1 parent 43a44d5 commit 4aa9fda
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions misc/quitcd/quitcd.nu
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
export-env {
# Run nnn with dynamic changing directory to the environment.
export def --env n [
...flags : string # Extra flags to launch nnn with.
] -> nothing {

let config_home = if ($env.XDG_CONFIG_HOME | is-empty) {
$'($env.HOME)/.config/'
} else {
$env.XDG_CONFIG_HOME
}

# The behaviour is set to cd on quit (nnn checks if NNN_TMPFILE is set).
$env.NNN_TMPFILE = ($config_home + '/nnn/.lastd')
}

# Run nnn with dynamic changing directory to the environment.
export def --env n [
...flags : string # Extra flags to launch nnn with.
] -> nothing {
let nnn_tmpfile = $config_home + '/nnn/.lastd'

# Launch nnn. Add desired flags after `^nnn`, ex: `^nnn -eda ($x | str join)`
^nnn ...$flags
NNN_TMPFILE=$nnn_tmpfile ^nnn ...$flags

if ($env.NNN_TMPFILE | path exists) {
if ($nnn_tmpfile | path exists) {
# Remove <cd '> from the first part of the string and the last single quote <'>.
# Fix post-processing of nnn with path, escaping single quotes with POSIX syntax.
let path = (open $env.NNN_TMPFILE | str substring 4..-1 | str replace `'\''` `'`)
let path = (open $nnn_tmpfile | str substring 4..-1 | str replace --all `'\''` `'`)

rm $env.NNN_TMPFILE
rm $nnn_tmpfile

cd $path
}
Expand Down

0 comments on commit 4aa9fda

Please sign in to comment.