Oil.nvim

Published Wed Feb 07 2024 Updated Thu Feb 22 2024

Oil.nvim is a nice plugin I’ve recently started using instead of a file drawer. It is a Neovim plugin inspired by vim-vinegar. Oil’s benefit is making the files modifiable through vim motions. I can ct. to change a file name up to the extension, or dd a file and put it into a new location. This is my the primary mapping I have for Oil:

:keys [
  {
    :mode "n"
    1 "<C-n>"
    2 (fn []
        (let [oil (require :oil)]
          (oil.toggle_float)
        )
      )
    :desc "Oil - parent directory"
  }
]

I have this bound to <C-n> out of habit, my Nerdtree keymap was <C-n>. Using oil.toggle_float opens oil as a floating window in the center of my screen. Editing files as a buffer doesn’t feel that awesome at first, but once comfortable I can’t edit my files any other way now.

Useful Keys

Oil has some great default keybinds as well. You can see them by using g? to get help from within an Oil buffer. Here are a couple I use most:

  • - go to parent directory
  • g. show hidden files
  • <Cr> select hovered file
  • <C-s> select and vsplit file

With Telescope I use <C-v> to vsplit a file so I have remapped vsplit in oil to <C-v> for comfort. This does unmap visual block mode, but I haven’t had the need for this in Oil for now.

:keys [
  {
    :mode "n"
    :ft "oil"
    1 "<C-v>"
    2 (fn []
      (let [oil (require :oil)]
        (oil.select { :vertical true })))
    :desc "Oil - select vsplit"
  }
]

I added the mapping through Lazy’s keys table. The option :ft "oil" ensures the mapping is applied to Oil buffers only.

Extra

Oil comes with some hidden super powers too, such as LSP file path updates when you save edits to your oil buffer! This behavior is enabled by default, but you do have to save the files. The lsp_rename_autosave option allows Oil to save updated paths for you if you would like.