Ranger is a really nice file explorer for the command line. If you have tried it once, you will never wanna miss it again. The coolest thing about ranger is its integrated file preview, which works similar to the OSX Finder. But for working CLI only, its most outstanding feature is the terminal picture preview. With that in mind you will at least need to give a try to use ranger as a file explorer in vim.
Before you are going to integrate ranger in vim, you might want to have a look at ranger itself and see it in action to decide if it will suite your vim experience. The ranger file explorer is amazingly fast and can be controlled with hotkeys similar to vim. There is nothing you miss so far. It has file previews with syntax highlighting. It can even preview PDF’s and give information about multimedia files.
- UTF-8 Support
- Multi-column display
- Preview of the selected file/directory
- Common file operations (create/chmod/copy/delete/…)
- VIM-like console and hotkeys
- Renaming multiple files at once
- Automatically determine file types and run them with correct programs
- Change the directory of your shell after exiting ranger
- Tabs, Bookmarks, Mouse support
- True Color Image previews
- Video thumbnails
Source: ranger homepage
So you also want it to open files from within vim? Here is a very simple version of how it can be done:
function RangerExplorer() exec "silent !ranger --choosefile=/tmp/vim_ranger_current_file " . expand("%:p:h") if filereadable('/tmp/vim_ranger_current_file') exec 'edit ' . system('cat /tmp/vim_ranger_current_file') call system('rm /tmp/vim_ranger_current_file') endif redraw! endfun map <Leader>x :call RangerExplorer()<CR>
The vim function is pretty basic and can definitely gain some improvements. The important part however is
map <Leader>x :call RangerExplorer()<CR>. This is where you map the call to the Ranger function. Map it to whatever command you desire or just pick the above one.
To get it working straight out of the box, just add the above snippet to your
~/.vimrc, reload vim and hit Leader + X.
If you want to know how to setup ranger on iTerm2 properly including image preview, read the following step by step guide Ranger image preview on OSX with iTerm2
Pingback: Ranger image preview on OSX with iTerm2