From 14b17d5d0b0f5bbb3e1d1d60fdd2724cb15bec6f Mon Sep 17 00:00:00 2001 From: fredrikr79 Date: Mon, 29 Sep 2025 11:41:50 +0200 Subject: [PATCH] fix nnn with nushell --- home.nix | 1 + home/config/nushell/config.nu | 39 +++++++++++++++------------- home/config/nushell/quitcd/quitcd.nu | 39 ++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 18 deletions(-) create mode 100644 home/config/nushell/quitcd/quitcd.nu diff --git a/home.nix b/home.nix index 4e7b5af..715375b 100644 --- a/home.nix +++ b/home.nix @@ -112,6 +112,7 @@ in ".config/jj".source = home/config/jj; ".icons/default".source = "${pkgs.vanilla-dmz}/share/icons/Vanilla-DMZ"; ".config/nushell/themes".source = home/config/nushell/themes; + ".config/nushell/quitcd".source = home/config/nushell/quitcd; }; home.sessionVariables = { diff --git a/home/config/nushell/config.nu b/home/config/nushell/config.nu index 535183c..dc3506b 100644 --- a/home/config/nushell/config.nu +++ b/home/config/nushell/config.nu @@ -1,21 +1,21 @@ -# # config.nu -# # -# # Installed by: -# # version = "0.106.1" -# # -# # This file is used to override default Nushell settings, define -# # (or import) custom commands, or run any other startup tasks. -# # See https://www.nushell.sh/book/configuration.html -# # -# # Nushell sets "sensible defaults" for most configuration settings, -# # so your `config.nu` only needs to override these defaults if desired. -# # -# # You can open this file in your default editor using: -# # config nu -# # -# # You can also pretty-print and page through the documentation for configuration -# # options using: -# # config nu --doc | nu-highlight | less -R +# config.nu +# +# Installed by: +# version = "0.106.1" +# +# This file is used to override default Nushell settings, define +# (or import) custom commands, or run any other startup tasks. +# See https://www.nushell.sh/book/configuration.html +# +# Nushell sets "sensible defaults" for most configuration settings, +# so your `config.nu` only needs to override these defaults if desired. +# +# You can open this file in your default editor using: +# config nu +# +# You can also pretty-print and page through the documentation for configuration +# options using: +# config nu --doc | nu-highlight | less -R ### @@ -108,3 +108,6 @@ $env.PATH = ($env.PATH | source ./themes/dracula.nu +source ./quitcd/quitcd.nu +$env.NNN_TMPFILE = $"($env.HOME)/.config/nnn/.lastd" +$env.EDITOR = "nvim" diff --git a/home/config/nushell/quitcd/quitcd.nu b/home/config/nushell/quitcd/quitcd.nu new file mode 100644 index 0000000..faad949 --- /dev/null +++ b/home/config/nushell/quitcd/quitcd.nu @@ -0,0 +1,39 @@ +# Run nnn with dynamic changing directory to the environment. +# +# $env.XDG_CONFIG_HOME sets the home folder for `nnn` folder and its $env.NNN_TMPFILE variable. +# See manual NNN(1) for more information. +# +# Import module using `use quitcd.nu n` to have `n` command in your context. +export def --env n [ + ...args : string # Extra flags to launch nnn with. + --selective = false # Change directory only when exiting via ^G. +]: nothing -> nothing { + + # The behaviour is set to cd on quit (nnn checks if $env.NNN_TMPFILE is set). + # Hard-coded to its respective behaviour in `nnn` source-code. + let nnn_tmpfile = $env + | default '~/.config/' 'XDG_CONFIG_HOME' + | get 'XDG_CONFIG_HOME' + | path join 'nnn/.lastd' + | path expand + + # Launch nnn. Add desired flags after `^nnn`, ex: `^nnn -eda ...$args`, + # or make an alias `alias n = n -eda`. + if $selective { + ^nnn -e ...$args + } else { + NNN_TMPFILE=$nnn_tmpfile ^nnn -e ...$args + } + + if ($nnn_tmpfile | path exists) { + # Remove from the first part of the string and the last single quote <'>. + # Fix post-processing of nnn's given path that escapes its single quotes with POSIX syntax. + let path = open $nnn_tmpfile + | str replace --all --regex `^cd '|'$` `` + | str replace --all `'\''` `'` + + ^rm -- $nnn_tmpfile + + cd $path + } +}