From 0617c8d90ab49dfcf0d7cefb0a7a6610d742d56e Mon Sep 17 00:00:00 2001 From: Arun Prakash Jana Date: Fri, 4 Jan 2019 22:33:55 +0530 Subject: [PATCH] Sample scripts to convert NUL to newline --- README.md | 76 +++++++++++++++++----------------- scripts/user-scripts/copier.sh | 2 +- scripts/user-scripts/picker.sh | 16 +++---- 3 files changed, 44 insertions(+), 50 deletions(-) diff --git a/README.md b/README.md index 52b9469..2627d38 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,7 @@ We need contributors. Please visit the [ToDo list](https://github.com/jarun/nnn/ - [Leader key](#leader-key) - [Contexts](#contexts) - [Directory color](#directory-color) + - [Selection](#selection) - [Filters](#filters) - [Navigate-as-you-type mode](#navigate-as-you-type-mode) - [File indicators](#file-indicators) @@ -62,7 +63,6 @@ We need contributors. Please visit the [ToDo list](https://github.com/jarun/nnn/ - [How to](#how-to) - [add bookmarks](#add-bookmarks) - [copy file paths](#copy-file-paths) - - [selection](#selection) - [to clipboard](#to-clipboard) - [get selection manually](#get-selection-manually) - [cd on quit](#cd-on-quit) @@ -291,6 +291,29 @@ Each context can have its own color for directories specified: export NNN_CONTEXT_COLORS="1234" colors: 0-black, 1-red, 2-green, 3-yellow, 4-blue (default), 5-magenta, 6-cyan, 7-white +#### Selection + +Use ^K to copy the absolute path of the file under the cursor. + +To copy multiple absolute file paths: + +- press ^Y (or Y) to enter selection mode. In this mode it's possible to + - cherry-pick individual files one by one by pressing ^K on each entry (works across directories and contexts); or, + - navigate to another file in the same directory to select a range of files +- press ^Y (or Y) _again_ to copy the paths and exit the selection mode + +The files in the list can now be copied (P), moved (V) or removed (X). + +To list the file paths copied to memory press y. + +File paths are copied to the temporary file `DIR/.nnncp`, where `DIR` (by priority) is: + + $HOME or, + $TMPDIR or, + /tmp + +The path is shown in the help and configuration screen. + #### Filters Filters support regexes to instantly (search-as-you-type) list the matching entries in the current directory. @@ -384,41 +407,36 @@ NOTE: Bookmark keys should be single-character to use them in combination with t #### copy file paths -##### selection +##### to clipboard -Use ^K to copy the absolute path of the file under the cursor. +Along with default copy, `nnn` can pipe the absolute path of the current file or multiple files to a copier script. For example, you can use `xsel` on Linux or `pbcopy` on macOS. Here's a sample [copier script](https://github.com/jarun/nnn/blob/master/scripts/user-scripts/copier.sh). -To copy multiple absolute file paths: +To inform `nnn` of the executable copier script location: -- press ^Y (or Y) to enter selection mode. In this mode it's possible to - - cherry-pick individual files one by one by pressing ^K on each entry (works across directories and contexts); or, - - navigate to another file in the same directory to select a range of files -- press ^Y (or Y) _again_ to copy the paths and exit the selection mode + export NNN_COPIER="/path/to/copier.sh" -The files in the list can now be copied (P), moved (V) or removed (X). +##### get selection manually -To list the file paths copied to memory press y. +NOTE: In the following examples we assume the copy file is at `~/.nnncp`. -File paths are copied to the temporary file `DIR/.nnncp`, where `DIR` (by priority) is: +The file paths are `NUL`-terminated, so additional processing is required to make them usable. - $HOME or, - $TMPDIR or, - /tmp +To get a space-separated list of the file paths in selection: -The path is shown in the help and configuration screen.. + cat ~/.nnncp | xargs -0 echo -To use the copied paths from the cmdline, use command substitution. For example, if `DIR` above is `/home/user`: +To get a newline-separated list of the file paths in selection: # bash/zsh - ls -ltr `cat /home/user/.nnncp` - ls -ltr $(cat /home/user/.nnncp) + ls -ltr `cat /home/user/.nnncp | tr '\0' '\n'` + ls -ltr $(cat /home/user/.nnncp | tr '\0' '\n') # fish - ls -ltr (cat /home/user/.nnncp) + ls -ltr (cat /home/user/.nnncp | tr '\0' '\n') An alias may be handy, e.g. when you want to copy selection at the _run a command_ prompt: - alias ncp='cat /home/user/.nnncp' + alias ncp="cat /home/user/.nnncp | tr '\0' '\n'" so you can easily handle files together: @@ -429,24 +447,6 @@ so you can easily handle files together: # fish ls -ltr (ncp) -##### to clipboard - -Along with default copy, `nnn` can pipe the absolute path of the current file or multiple files to a copier script. For example, you can use `xsel` on Linux or `pbcopy` on macOS. Here's a sample [copier script](https://github.com/jarun/nnn/blob/master/scripts/user-scripts/copier.sh). - -To inform `nnn` of the executable copier script location: - - export NNN_COPIER="/path/to/copier.sh" - -##### get selection manually - -To get a space-separated list of the file paths in selection, say at the command-prompt: - - cat ~/.nnncp | xargs -0 echo - -Set an easy to remember alias: - - alias ncp="cat ~/.nnncp | xargs -0 echo" - To get the list in a file: ncp > out.txt diff --git a/scripts/user-scripts/copier.sh b/scripts/user-scripts/copier.sh index 3d8ccb5..391770c 100755 --- a/scripts/user-scripts/copier.sh +++ b/scripts/user-scripts/copier.sh @@ -1,4 +1,4 @@ -#!/bin/sh +#!/usr/bin/env sh # Linux cat ~/.nnncp | xargs -0 | xsel -bi diff --git a/scripts/user-scripts/picker.sh b/scripts/user-scripts/picker.sh index 1968c96..8885ed6 100644 --- a/scripts/user-scripts/picker.sh +++ b/scripts/user-scripts/picker.sh @@ -1,8 +1,8 @@ -#!/bin/bash +#!/usr/bin/env sh # Description: Pick files and pipe the line-separated list to another utility # -# Shell: bash +# Shell: sh # Author: Arun Prakash Jana # # Usage: @@ -16,12 +16,6 @@ # # NOTE: This use case is limited to picking files, other functionality may not work as expected. -nnn -p /tmp/pickerout -> /tmp/picked -while read -d $'\0' line ; do - echo $line >> /tmp/picked -done < /tmp/pickerout -echo $line >> /tmp/picked -cat /tmp/picked - -rm /tmp/pickerout /tmp/picked +nnn -p /tmp/picked +cat /tmp/picked | tr '\0' '\n' +rm /tmp/picked