quitcd.nu (1288B)
1 # Run nnn with dynamic changing directory to the environment. 2 # 3 # $env.XDG_CONFIG_HOME sets the home folder for `nnn` folder and its $env.NNN_TMPFILE variable. 4 # See manual NNN(1) for more information. 5 # 6 # Import module using `use quitcd.nu n` to have `n` command in your context. 7 export def --env n [ 8 ...args : string # Extra flags to launch nnn with. 9 --selective = false # Change directory only when exiting via ^G. 10 ]: nothing -> nothing { 11 12 # The behaviour is set to cd on quit (nnn checks if $env.NNN_TMPFILE is set). 13 # Hard-coded to its respective behaviour in `nnn` source-code. 14 let nnn_tmpfile = $env 15 | default '~/.config/' 'XDG_CONFIG_HOME' 16 | get 'XDG_CONFIG_HOME' 17 | path join 'nnn/.lastd' 18 | path expand 19 20 # Launch nnn. Add desired flags after `^nnn`, ex: `^nnn -eda ...$args`, 21 # or make an alias `alias n = n -eda`. 22 if $selective { 23 ^nnn ...$args 24 } else { 25 NNN_TMPFILE=$nnn_tmpfile ^nnn ...$args 26 } 27 28 if ($nnn_tmpfile | path exists) { 29 # Remove <cd '> from the first part of the string and the last single quote <'>. 30 # Fix post-processing of nnn's given path that escapes its single quotes with POSIX syntax. 31 let path = open $nnn_tmpfile 32 | str replace --all --regex `^cd '|'$` `` 33 | str replace --all `'\''` `'` 34 35 ^rm -- $nnn_tmpfile 36 37 cd $path 38 } 39 }