rnvimr

repository·main·Indexed 21 days ago

https://github.com/kevinhwang91/rnvimr

A Neovim plugin that integrates the Ranger file manager into a floating window using RPC. It supports custom layouts, keybindings for opening files in tabs or splits, and synchronization of the current working directory between Ranger and Neovim. Requires Neovim 0.5+, Python 3.6+, Pynvim, and Ranger v1.9.3+.

Tokens
2.2K
Snippets
8
Records
11
Agent score
24%

What's inside rnvimr

  1. Install Rnvimr and its dependencies

    main

    Rnvimr requires Neovim (0.5+), Python 3.6+, Pynvim, and Ranger (v1.9.3+).

    ArchLinux

    Use yay to install all requirements at once:

    yay -S ranger python-pynvim ueberzug

    Note: ueberzug can be replaced with ueberzugpp.

    Other Unix Distributions (via pip)

    Install the core dependencies using pip3:

    pip3 install ranger-fm pynvim

    macOS Specifics:

    • Install Ranger via pip instead of brew: pip3 ranger-fm.
    • ueberzug and ueberzugpp are not supported on macOS because they depend on X11.

    Plugin Manager (Vim-plug)

    Add the following to your Neovim configuration:

    Plug 'kevinhwang91/rnvimr'
    # ArchLinux
    yay -S ranger python-pynvim ueberzug
    
    # Other Unix
    pip3 install ranger-fm pynvim
  2. Verify Rnvimr installation with CheckHealth

    main

    To ensure all dependencies and the RPC connection are working correctly, run the following command within Neovim:

    :checkhealth rnvimr

    Successful installation should show OK for OS, Ranger, Python, Pynvim, and RPC. Ueberzug is optional but will be listed if detected.

  3. Use Rnvimr commands and keybindings

    main

    Rnvimr provides several commands and interactive behaviors to manage the Ranger floating window:

    Commands

    • :RnvimrToggle: Creates a Ranger process or toggles the visibility of the floating window.
    • :RnvimrResize: Cycles through preset floating window layouts.

    Ranger Interactions

    • Enter or l: Opens the selected file in Neovim.
    • q: Hides the floating window (Ranger will automatically attach the current buffer's file on the next toggle).
    • H: Goes back to the last history directory in Ranger.
    • yw: Emits Ranger's current working directory (cwd) to Neovim.
    • gw: Jumps to Neovim's current working directory.
    • ctrl-t / ctrl-x / ctrl-v: Opens the selected file in a new tab, horizontal split, or vertical split respectively (behavior depends on g:rnvimr_action configuration).
  4. Configure Rnvimr settings

    main

    Rnvimr is highly customizable via global variables (g:rnvimr_*).

    Common Configuration Options

    • g:rnvimr_enable_ex: Set to 1 to make Ranger replace Netrw as the default file explorer.
    • g:rnvimr_enable_picker: Set to 1 to hide the window automatically after picking a file.
    • g:rnvimr_hide_gitignore: Set to 1 to hide files included in .gitignore.
    • g:rnvimr_enable_bw: Set to 1 to automatically wipe Neovim buffers corresponding to files deleted in Ranger.
    • g:rnvimr_draw_border: Set to 0 to disable the floating window border.
    • g:rnvimr_border_attr: Define border colors (e.g., {'fg': 14, 'bg': -1}).
    • g:rnvimr_vanilla: Set to 1 to use default Ranger configuration.

    Layout and Preset Customization

    • g:rnvimr_layout: A dictionary defining the initial window size, position, and style.
    • g:rnvimr_presets: A list of dictionaries defining multiple preset window layouts that :RnvimrResize cycles through.
    • g:rnvimr_ranger_views: A list of dictionaries to help Ranger adapt its view to the floating window size (using minwidth, maxwidth, and ratio).

    Action Mapping

    Use g:rnvimr_action to map Ranger keys to Neovim commands. This is useful for fixing macOS split-window issues.

    let g:rnvimr_action = {
                
                	'<C-t>': 'NvimEdit tabedit',
                	'<C-x>': 'NvimEdit split',
                	'<C-v>': 'NvimEdit vsplit',
                	'gw': 'JumpNvimCwd',
                	'yw': 'EmitRangerCwd'
                	}
    " Example: Enable Ranger as default explorer and hide gitignored files
    let g:rnvimr_enable_ex = 1
    let g:rnvimr_hide_gitignore = 1
    
    " Example: Customizing the initial layout
    let g:rnvimr_layout = {
                
                	'relative': 'editor',
                	'width': float2nr(round(0.7 * &columns)),
                	'height': float2nr(round(0.7 * &lines)),
                	'col': float2nr(round(0.15 * &columns)),
                	'row': float2nr(round(0.15 * &lines)),
                	'style': 'minimal'
                	}
  5. Troubleshoot Rnvimr issues

    main

    Files not opening correctly

    If Enter or l fails to open certain file types, check your Ranger rifle.conf. Ensure that ${VISUAL:-$EDITOR} -- "$@" is a valid candidate for those file types. You may need to add your file extension to the mime type rule in rifle.conf.

    RPC Timeout in CheckHealth

    If checkhealth reports an RPC timeout, ensure the NVIM_LISTEN_ADDRESS environment variable is set. You can verify this by running :echo $NVIM_LISTEN_ADDRESS in Neovim. It should return a path like /tmp/nvim... or an address:port format.

    macOS Vertical Split Issue

    On macOS, <ctrl-v> might require pressing it twice. To fix this, use the g:rnvimr_action configuration to explicitly remap <C-v> to NvimEdit vsplit.

  6. Use the Client class for Neovim RPC communication

    main

    The Client class provides the primary interface for Ranger to communicate with a Neovim instance via RPC. To use it, you must first instantiate the client and then call attach_nvim() to connect to the Neovim session using the socket path defined in the NVIM_LISTEN_ADDRESS environment variable.

    from ranger.plugins import client
    
    # Initialize and connect
    nvim_client = client.Client()
    nvim_client.attach_nvim()
    
    # Example: Get current working directory
    cwd = nvim_client.get_cwd()
    print(f"Neovim CWD: {cwd}")
  7. Save and move buffers with Client

    main

    Use these methods to manage buffer persistence between Ranger and Neovim:

    • do_saveas(bufnr, target_name): Saves a specific Neovim buffer (bufnr) to an absolute path (target_name). The target path must exist before calling this.
    • move_buf(src, dst): Moves buffers from a source path to a destination path. If dst is a directory, it attempts to map the subpaths of src to the new location in dst using do_saveas.
    # Save buffer 5 to a specific path
    client.do_saveas(5, '/absolute/path/to/file.txt')
    
    # Move a directory of buffers to a new location
    client.move_buf('/old/path', '/new/path')
  8. Edit files in Neovim via rpc_edit

    main

    The rpc_edit method allows you to open one or more files in Neovim using RPC. You can specify a custom edit command or use the method as a picker.

    Parameters:

    • files (list): A list of file paths to edit.
    • edit (str, optional): The Neovim command to use (defaults to 'edit').
    • picker (bool, optional): If provided, enables picker mode.
    # Edit multiple files
    client.rpc_edit(['/path/to/file1.txt', '/path/to/file2.txt'])
    
    # Use a custom command
    client.rpc_edit(['file.txt'], edit='vsplit')
  9. Manage Neovim windows and highlights with Client

    main

    The Client class allows controlling the Neovim floating window used by the plugin:

    • get_window_info(): Returns information about the current floating window.
    • hide_window(): Closes the floating window.
    • set_winhl(winhl): Sets the floating window highlight using a variable name from the Ranger buffer.
    # Set highlight and then hide the window
    client.set_winhl('my_highlight_var')
    client.hide_window()
  10. Synchronize Neovim directory and buffers

    main

    The Client class provides methods to keep Neovim's state in sync with Ranger:

    • get_cwd(): Returns the current working directory of Neovim.
    • set_cwd(path, noautocmd=False): Sets Neovim's current working directory to the specified absolute path. If noautocmd is True, it uses the noautocmd command to prevent triggering autocommands.
    • list_buf_name_nr(): Returns a dictionary where keys are buffer names and values are buffer numbers.
    • get_cb(): Returns the absolute path of the current buffer in Neovim.