Use Ranger as a file explorer in vim

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.

Ranger

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.

ranger

Features

  • 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

Vim

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.

Further Readings

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

_

Tweet about this on TwitterShare on RedditShare on FacebookShare on Google+Pin on PinterestShare on LinkedIn

9 comments on “Use Ranger as a file explorer in vim”

  1. Alan Reply

    I would suggest one slight improvement: add a return key to the mapped command, i.e.

    map x :call RangerExplorer()

      • James Baumgarten Reply

        I think he means that right now, your map won’t actually run the function, the user still has to hit the return key. What you probably want is:

        map x :call RangerExplorer()

        • James Baumgarten Reply

          Huh, looks like the , without the spaces, gets interpreted as html. lets try this again:

          “map x :call RangerExplorer()”

          • cytopia Reply

            It is fixed above. Thanks you!

            Still trying to find a good markdown plugin that also works on comments, so code can be pasted here as well.

    • cytopia Reply

      Hi Alan, thanks for the info. Lost some formatting by switching to markdown syntax. I have re-added the CR above.

  2. Pingback: Ranger image preview on OSX with iTerm2

  3. lasertest Reply

    What’s up to all, the contents present at this website are really awesome for people knowledge, well, keep up

    the good work fellows.

  4. Quexint Reply

    In the vimscript, I think we need to replace spaces in the path with escaped.

    My code is following:

    function RangerExplorer()
    exec "silent !ranger --choosefile=/tmp/vim_ranger_current_file " . system('echo "' . expand("%:p:h") . '" | sed -E "s/\ /\\\ /g"')
    if filereadable('/tmp/vim_ranger_current_file')
    exec 'edit ' . system('cat /tmp/vim_ranger_current_file | sed -E "s/\ /\\\ /g"')
    call system('rm /tmp/vim_ranger_current_file')
    endif
    redraw!
    endfun

Leave a Reply

Your email address will not be published. Required fields are marked *