Support cd on quit

This commit is contained in:
Arun Prakash Jana 2017-04-19 18:22:30 +05:30
parent afa0540934
commit 01c1ef6e58
No known key found for this signature in database
GPG key ID: A75979F35C080412
7 changed files with 64 additions and 5 deletions

View file

@ -33,6 +33,7 @@ Noice is Not Noice, a noicer fork...
- [File type abbreviations](#file-type-abbreviations) - [File type abbreviations](#file-type-abbreviations)
- [Help](#help) - [Help](#help)
- [How to](#how-to) - [How to](#how-to)
- [cd on quit](#cd-on-quit)
- [Copy current file path to clipboard](#copy-current-file-path-to-clipboard) - [Copy current file path to clipboard](#copy-current-file-path-to-clipboard)
- [Change file associations](#change-file-associations) - [Change file associations](#change-file-associations)
- [Developers](#developers) - [Developers](#developers)
@ -90,6 +91,7 @@ I chose to fork because:
- Removed navigation restriction with relative paths (and let permissions handle it) - Removed navigation restriction with relative paths (and let permissions handle it)
- Sort entries by file size (largest to smallest) - Sort entries by file size (largest to smallest)
- Shortcut to invoke file name copier (set using environment variable `NNN_COPIER`) - Shortcut to invoke file name copier (set using environment variable `NNN_COPIER`)
- Change to last visited directory on quit
#### File association #### File association
- Set `NNN_OPENER` to let a desktop opener handle it all. E.g.: - Set `NNN_OPENER` to let a desktop opener handle it all. E.g.:
@ -141,7 +143,7 @@ nnn vs. ranger memory usage while viewing a directory with 10,178 files, sorted
nnn needs libncursesw on Linux (or ncurses on OS X) and standard libc. nnn needs libncursesw on Linux (or ncurses on OS X) and standard libc.
- If you are using Homebrew, run: - If you are using **Homebrew**, run:
brew install jarun/nnn/nnn brew install jarun/nnn/nnn
- Packages are available on - Packages are available on
@ -219,6 +221,7 @@ Add the following to your shell's rc file for the best experience:
| `^L` | Force a redraw | | `^L` | Force a redraw |
| `?` | Toggle help screen | | `?` | Toggle help screen |
| `q` | Quit | | `q` | Quit |
| `Q` | Quit and change directory |
#### Filters #### Filters
@ -251,6 +254,12 @@ To lookup keyboard shortcuts at runtime, press `?`.
### How to ### How to
#### cd on quit
Pick the appropriate file for your shell from [misc/quitcd](https://github.com/jarun/nnn/tree/master/misc/quitcd) and add the contents to your shell's rc file. You'll need to spawn a new shell for the change to take effect. You should start nnn as `n` (or modify the function name to something else).
As you might notice, nnn uses the environment variable `NNN_TMPFILE` to write the last visited directory path. You can change it.
#### Copy current file path to clipboard #### Copy current file path to clipboard
nnn can pipe the absolute path of the current file to a copier script. For example, you can use `xsel` on Linux or `pbcopy` on OS X. nnn can pipe the absolute path of the current file to a copier script. For example, you can use `xsel` on Linux or `pbcopy` on OS X.

View file

@ -25,6 +25,7 @@ struct assoc assocs[] = {
struct key bindings[] = { struct key bindings[] = {
/* Quit */ /* Quit */
{ 'q', SEL_QUIT, "", "" }, { 'q', SEL_QUIT, "", "" },
{ 'Q', SEL_CDQUIT, "", "" },
/* Back */ /* Back */
{ KEY_BACKSPACE, SEL_BACK, "", "" }, { KEY_BACKSPACE, SEL_BACK, "", "" },
{ KEY_LEFT, SEL_BACK, "", "" }, { KEY_LEFT, SEL_BACK, "", "" },

10
misc/quitcd/quitcd.bash Normal file
View file

@ -0,0 +1,10 @@
export NNN_TMPFILE="/tmp/nnn"
n()
{
nnn -d
if [ -f $NNN_TMPFILE ]; then
. $NNN_TMPFILE
rm $NNN_TMPFILE
fi
}

9
misc/quitcd/quitcd.fish Normal file
View file

@ -0,0 +1,9 @@
export NNN_TMPFILE="/tmp/nnn"
function n --description 'support nnn quit and change directory'
nnn -d
if test -e $NNN_TMPFILE
. $NNN_TMPFILE
rm $NNN_TMPFILE
end
end

10
misc/quitcd/quitcd.zsh Normal file
View file

@ -0,0 +1,10 @@
export NNN_TMPFILE="/tmp/nnn"
n()
{
nnn -d
if [ -f $NNN_TMPFILE ]; then
. $NNN_TMPFILE
rm $NNN_TMPFILE
fi
}

11
nnn.1
View file

@ -79,6 +79,8 @@ Force a redraw
Toggle help screen Toggle help screen
.It Ic q .It Ic q
Quit Quit
.It Ic Q
Quit and change directory
.El .El
.Pp .Pp
Backing up one directory level will set the cursor position at the Backing up one directory level will set the cursor position at the
@ -105,6 +107,12 @@ is configured by modifying
and recompiling the code. and recompiling the code.
.Pp .Pp
See the environment and examples sections below for more options and information. See the environment and examples sections below for more options and information.
.Pp
Configuring
.Nm
to change to the last visited directory on quit requires shell integration in a
few easy steps. Please visit the project page (linked below) for the
instructions.
.Sh FILTERS .Sh FILTERS
Filters support regexes to display only the matched Filters support regexes to display only the matched
entries in the current directory view. This effectively allows entries in the current directory view. This effectively allows
@ -117,8 +125,7 @@ An empty filter expression resets the filter.
.Pp .Pp
If If
.Nm .Nm
is invoked as root the default filter will also match hidden is invoked as root the default filter will also match hidden files.
files.
.Sh ENVIRONMENT .Sh ENVIRONMENT
The SHELL, EDITOR and PAGER environment variables take precedence The SHELL, EDITOR and PAGER environment variables take precedence
when dealing with the !, e and p commands respectively. when dealing with the !, e and p commands respectively.

15
nnn.c
View file

@ -72,6 +72,7 @@ struct assoc {
/* Supported actions */ /* Supported actions */
enum action { enum action {
SEL_QUIT = 1, SEL_QUIT = 1,
SEL_CDQUIT,
SEL_BACK, SEL_BACK,
SEL_GOIN, SEL_GOIN,
SEL_FLTR, SEL_FLTR,
@ -937,7 +938,8 @@ show_help(void)
^K Invoke file name copier\n\ ^K Invoke file name copier\n\
^L Force a redraw\n\ ^L Force a redraw\n\
? Toggle help screen\n\ ? Toggle help screen\n\
q Quit\n"); q Quit\n\
Q Quit and change directory\n");
/* Show exit keys */ /* Show exit keys */
printw("\n\n << (?/q)"); printw("\n\n << (?/q)");
@ -1242,6 +1244,17 @@ begin:
nochange: nochange:
sel = nextsel(&run, &env); sel = nextsel(&run, &env);
switch (sel) { switch (sel) {
case SEL_CDQUIT:
{
char *tmpfile = getenv("NNN_TMPFILE");
if (tmpfile) {
FILE *fp = fopen(tmpfile, "w");
if (fp) {
fprintf(fp, "cd \"%s\"", path);
fclose(fp);
}
}
}
case SEL_QUIT: case SEL_QUIT:
dentfree(dents); dentfree(dents);
return; return;