aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Arun Prakash Jana <engineerarun@gmail.com>2017-04-19 18:22:30 +0530
committerGravatar Arun Prakash Jana <engineerarun@gmail.com>2017-04-20 17:33:54 +0530
commit01c1ef6e58c195821ff9ccb9489bd99a9101b0ec (patch)
tree32b5350310cd42b1f7256f4b3654ddeb01aa3cd1
parentafa05409348fd3aa50ea9f491300ca00efb35c8d (diff)
downloadnnn-01c1ef6e58c195821ff9ccb9489bd99a9101b0ec.tar.gz
Support cd on quit
-rw-r--r--README.md11
-rw-r--r--config.def.h1
-rw-r--r--misc/quitcd/quitcd.bash10
-rw-r--r--misc/quitcd/quitcd.fish9
-rw-r--r--misc/quitcd/quitcd.zsh10
-rw-r--r--nnn.111
-rw-r--r--nnn.c17
7 files changed, 64 insertions, 5 deletions
diff --git a/README.md b/README.md
index 25117b3..f471d0f 100644
--- a/README.md
+++ b/README.md
@@ -33,6 +33,7 @@ Noice is Not Noice, a noicer fork...
- [File type abbreviations](#file-type-abbreviations)
- [Help](#help)
- [How to](#how-to)
+ - [cd on quit](#cd-on-quit)
- [Copy current file path to clipboard](#copy-current-file-path-to-clipboard)
- [Change file associations](#change-file-associations)
- [Developers](#developers)
@@ -90,6 +91,7 @@ I chose to fork because:
- Removed navigation restriction with relative paths (and let permissions handle it)
- Sort entries by file size (largest to smallest)
- Shortcut to invoke file name copier (set using environment variable `NNN_COPIER`)
+ - Change to last visited directory on quit
#### File association
- 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.
-- If you are using Homebrew, run:
+- If you are using **Homebrew**, run:
brew install jarun/nnn/nnn
- 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 |
| `?` | Toggle help screen |
| `q` | Quit |
+| `Q` | Quit and change directory |
#### Filters
@@ -251,6 +254,12 @@ To lookup keyboard shortcuts at runtime, press `?`.
### 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
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.
diff --git a/config.def.h b/config.def.h
index 2acdae9..e2812e2 100644
--- a/config.def.h
+++ b/config.def.h
@@ -25,6 +25,7 @@ struct assoc assocs[] = {
struct key bindings[] = {
/* Quit */
{ 'q', SEL_QUIT, "", "" },
+ { 'Q', SEL_CDQUIT, "", "" },
/* Back */
{ KEY_BACKSPACE, SEL_BACK, "", "" },
{ KEY_LEFT, SEL_BACK, "", "" },
diff --git a/misc/quitcd/quitcd.bash b/misc/quitcd/quitcd.bash
new file mode 100644
index 0000000..94accd6
--- /dev/null
+++ b/misc/quitcd/quitcd.bash
@@ -0,0 +1,10 @@
+export NNN_TMPFILE="/tmp/nnn"
+
+n()
+{
+ nnn -d
+ if [ -f $NNN_TMPFILE ]; then
+ . $NNN_TMPFILE
+ rm $NNN_TMPFILE
+ fi
+}
diff --git a/misc/quitcd/quitcd.fish b/misc/quitcd/quitcd.fish
new file mode 100644
index 0000000..35551c8
--- /dev/null
+++ b/misc/quitcd/quitcd.fish
@@ -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
diff --git a/misc/quitcd/quitcd.zsh b/misc/quitcd/quitcd.zsh
new file mode 100644
index 0000000..94accd6
--- /dev/null
+++ b/misc/quitcd/quitcd.zsh
@@ -0,0 +1,10 @@
+export NNN_TMPFILE="/tmp/nnn"
+
+n()
+{
+ nnn -d
+ if [ -f $NNN_TMPFILE ]; then
+ . $NNN_TMPFILE
+ rm $NNN_TMPFILE
+ fi
+}
diff --git a/nnn.1 b/nnn.1
index a3028d2..2161876 100644
--- a/nnn.1
+++ b/nnn.1
@@ -79,6 +79,8 @@ Force a redraw
Toggle help screen
.It Ic q
Quit
+.It Ic Q
+Quit and change directory
.El
.Pp
Backing up one directory level will set the cursor position at the
@@ -105,6 +107,12 @@ is configured by modifying
and recompiling the code.
.Pp
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
Filters support regexes to display only the matched
entries in the current directory view. This effectively allows
@@ -117,8 +125,7 @@ An empty filter expression resets the filter.
.Pp
If
.Nm
-is invoked as root the default filter will also match hidden
-files.
+is invoked as root the default filter will also match hidden files.
.Sh ENVIRONMENT
The SHELL, EDITOR and PAGER environment variables take precedence
when dealing with the !, e and p commands respectively.
diff --git a/nnn.c b/nnn.c
index e10161c..7513705 100644
--- a/nnn.c
+++ b/nnn.c
@@ -72,6 +72,7 @@ struct assoc {
/* Supported actions */
enum action {
SEL_QUIT = 1,
+ SEL_CDQUIT,
SEL_BACK,
SEL_GOIN,
SEL_FLTR,
@@ -129,7 +130,7 @@ static off_t blk_size;
static size_t fs_free;
static int open_max;
static const double div_2_pow_10 = 1.0 / 1024.0;
-static const char* size_units[] = {"B", "K", "M", "G", "T", "P", "E", "Z", "Y"};
+static const char *size_units[] = {"B", "K", "M", "G", "T", "P", "E", "Z", "Y"};
/*
* Layout:
@@ -937,7 +938,8 @@ show_help(void)
^K Invoke file name copier\n\
^L Force a redraw\n\
? Toggle help screen\n\
- q Quit\n");
+ q Quit\n\
+ Q Quit and change directory\n");
/* Show exit keys */
printw("\n\n << (?/q)");
@@ -1242,6 +1244,17 @@ begin:
nochange:
sel = nextsel(&run, &env);
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:
dentfree(dents);
return;