]> Sergey Matveev's repositories - nnn.git/commitdiff
Fix #133: Add nnn-picker.vim plugin
authorArun Prakash Jana <engineerarun@gmail.com>
Fri, 23 Nov 2018 19:30:02 +0000 (01:00 +0530)
committerArun Prakash Jana <engineerarun@gmail.com>
Fri, 23 Nov 2018 19:33:45 +0000 (01:03 +0530)
README.md
scripts/vim-plugin/nnn-picker.vim [new file with mode: 0644]

index 6c256fea529edaef0192ead53fe25f591eb4a29e..88b1f9225f66661b33ec611ccb7440e97077ff28 100644 (file)
--- a/README.md
+++ b/README.md
@@ -63,6 +63,7 @@ It runs on Linux, OS X, Raspberry Pi, Cygwin, Linux subsystem for Windows and Te
     - [quote paths](#quote-paths)
     - [to clipboard](#to-clipboard)
   - [cd on quit](#cd-on-quit)
+  - [vim plugin](#vim-plugin)
   - [run custom scripts](#run-custom-scripts)
     - [sample scripts](#sample-scripts)
   - [change dir color](#change-dir-color)
@@ -80,6 +81,7 @@ It runs on Linux, OS X, Raspberry Pi, Cygwin, Linux subsystem for Windows and Te
 
 - Modes
   - Basic, detail (default), disk usage analyzer (du)
+  - Vim file picker (as a vim plugin)
 - Navigation
   - Familiar, easy shortcuts (arrows, `~`, `-`, `&`)
   - *Navigate-as-you-type* with auto-select directory
@@ -439,6 +441,10 @@ Pick the appropriate file for your shell from [`scripts/quitcd`](scripts/quitcd)
 
 As you might notice, `nnn` uses the environment variable `NNN_TMPFILE` to write the last visited directory path. You can change it.
 
+#### vim plugin
+
+`nnn` can be used as a file picker/chooser within vim. The instructions are available in the [nnn-picker.vim](https://github.com/jarun/nnn/blob/master/scripts/vim-plugin/nnn-picker.vim) plugin.
+
 #### run custom scripts
 
 `nnn` can invoke custom scripts with the currently selected file name as argument 1.
diff --git a/scripts/vim-plugin/nnn-picker.vim b/scripts/vim-plugin/nnn-picker.vim
new file mode 100644 (file)
index 0000000..3390db0
--- /dev/null
@@ -0,0 +1,45 @@
+" vim plugin to use nnn as a file picker
+" Closely follows and inspired by the vim_file_chooser plugin for ranger.
+"
+" Author: Arun Prakash Jana
+" Email: engineerarun@gmail.com
+" Homepage: https://github.com/jarun/nnn
+" Copyright © 2018 Arun Prakash Jana
+"
+" Usage:
+" Copy this file to the vim plugin directory.
+" To open nnn as a file picker in vim, use the command ":NnnPicker" or ":Np"
+" or the key-binding "<leader>n". Once you select one or more files and quit
+" nnn, vim will open the first selected file and add the remaining files to
+" the arg list/buffer list.
+" If no file is explicitly selected, the last selected entry is picked.
+
+function! NnnPicker()
+    let temp = tempname()
+    if has("gui_running")
+        exec 'silent !xterm -e nnn -p ' . shellescape(temp)
+    else
+        exec 'silent !nnn -p ' . shellescape(temp)
+    endif
+    if !filereadable(temp)
+        redraw!
+        " Nothing to read.
+        return
+    endif
+    let names = readfile(temp)
+    if empty(names)
+        redraw!
+        " Nothing to open.
+        return
+    endif
+    " Edit the first item.
+    exec 'edit ' . fnameescape(names[0])
+    " Add any remaining items to the arg list/buffer list.
+    for name in names[1:]
+        exec 'argadd ' . fnameescape(name)
+    endfor
+    redraw!
+endfunction
+command! -bar NnnPicker call NnnPicker()
+nnoremap <leader>n :<C-U>NnnPicker<CR>
+command! -nargs=* -complete=file Np :call NnnPicker()