Nnn: Feature request: Pin CWD with option

Created on 31 May 2020  Â·  17Comments  Â·  Source: jarun/nnn

For starting nnn with a script, it would be nice to be able to set a CWD with an option.

e.g. NNN_CWD=${PWD}

enhancement

All 17 comments

Just:

nnn "$PWD"

or, save whatever the path is in a var and use it:

dir="$PWD"
do something
nnn "$dir"

I saw your other request also. I need to understand what you mean by _using nnn in scripts_. Even if you run nnn from a script, you'll have to press keys to operate. What exactly are you trying?

For example, wrt. your current request, pressing @ takes you to your start dir.

my idea is to have a script which opens a pre-configured session in my terminal emulator (iTerm / kitty / tmux / whatever).
The session consists of panes with editor, nnn, terminal, and the like.

The session is being called in or on a directory I'm working in (e.g. git project).

In case of nnn: When I move around in the filetree I would like to be able to quickly return to the session's PWD.
And since I might forget setting that on first use it would be nice to set it with the script to be sure it's safed.

Would it be possible to set it with an env variable that's being evaluated on startup?
like (in config.fish) set -ax NNN_BMS ";,:$PWD"?

my idea is to have a script which opens a pre-configured session

You can save the dir in which you want to start in the session. nnn does a chdir() on every dir change from v3.2 (based on user request). So the setting NNN_CWD is not relevant anymore (in literal terms CWD = current working directory). The only constant is the start dir (@).

When I move around in the filetree I would like to be able to quickly return to the session's PWD

Got it. The problem is, each context can have a start dir so it would increase the mem usage by 16K which is significant.

I think we have established that a session is a well-defined snapshot with a fixed start dir. So the most appropriate solution here is to have 4 bookmarks (which is just another config var) with keys 1 to 4:

export NNN_BMS='1:start_dir1;2:start_dir2;3:start_dir3;4:start_dir4;...'

BTW, there's a also a bookmarks plugin you may find interesting. Check it out!

In any case, your bookmarks have to be in sync with your session start dirs and you are done!

So approaching this would be best done with a session?

Open nnn in Project Dir, move around, leave nnn, open nnn again, go back to Project dir with one (or two) Keypress?

Or maybe I set an environment variable per Project with a script.

my idea is to have a script which opens a pre-configured session in my terminal emulator (iTerm / kitty / tmux / whatever).

So approaching this would be best done with a session?

I thought you wanted a session to begin with.

Open nnn in Project Dir, move around, leave nnn, open nnn again, go back to Project dir with one (or two) Keypress?

No.

  1. Open nnn. Go to the dir you want to visit, set sort order, color whatever you want. Have multiple contexts if you need. Save the session.
  2. Save the start dir in each context as a bookmark.

Now when you open the session you saved in step 1 you will always start at the directories you set in 1.

If you navigate away, use the respective bookmark to go back to the session start dir.

BTW, to really harness the power of nnn, go through the project Wiki thoroughly. We have spent a lot of time in writing it and it's a valuable source of information.

my idea is to have a script which opens a pre-configured session in my terminal emulator (iTerm / kitty / tmux / whatever).

So approaching this would be best done with a session?

I thought you wanted a session to begin with.

I think my wording wasn’t very precise.
I want to setup a Project with a script.
Something that’s reproducable for any directory.
And nnn belongs to the Setup.
Since the Project would be tied to a directory it would be great to tie nnn within that terminal session to that folder.

Without any later configuration within nnn.

Without any later configuration within nnn.

Use nnn -S in that case. The default session @ is what you want.

Thanks.

But this would probably only work as long as I have just one nnn instance?

I was just thinking about setting what you would set by pressing , in nnn during startup.

But this would probably only work as long as I have just one nnn instance?

Why?

If I have several Terminal windows with different projects this _might_ become difficult.

Your session is fixed. So all your nnn instances open exactly the same way. Your bookmarks are fixed to start dir of each context, so they always take you to the same location. This is repetitive no matter how many instances of nnn you open.

Your session is fixed. So all your nnn instances open exactly the same way. Your bookmarks are fixed to start dir of each context, so they always take you to the same location. This is repetitive no matter how many instances of nnn you open.

Are you saying that the behavior I have in mind might not be possible with nnn?
Or only with custom sessions?

(Having different Start-dirs or pinned CWD in different opened nnn instances)

I probably don't understand completely what you're trying to achieve, but if you are launching nnn from a script, why don't you just call (cd $dir && nnn) ?

I‘m doing something similar to this:

https://github.com/kovidgoyal/kitty/issues/2422

I‘m using a script to open a Terminal tab with different panes for a project.

nnn would be to Kakoune what nerdtree is to vim.

I want to have a shortcut within nnn to come back to the project's root dir quickly, even after I exit and reenter nnn.

And this should still work even if there are other projects in other terminal windows opened with different instances of nnn.

I thought it would be easiest to use the predefined „pin CWD“ Hotkey (,) but probably the best option for my usecase is to add a session variable within the script to add a bookmark (like described in my post here)

@basbebe you could alternatively write a plugin, e.g.:
~~~sh

!/usr/bin/env sh

printf "%s" "0c$PROJECT_DIR" > "$NNN_PIPE"
~~~

Then you can come back to $PROJECT_DIR with Alt+.

Was this page helpful?
0 / 5 - 0 ratings