Crysthamus/nvim-file-operations

github github
lsp
stars 7
issues 1
subscribers 0
forks 1
CREATED

UPDATED


nvim-file-operations

nvim-file-operations is a Neovim plugin that adds support for workspace file operations using built-in LSPs. This plugin serves as a modern, maintained, and direct drop-in replacement for nvim-lsp-file-operations. It works by subscribing to events emitted by file managers like nvim-tree, neo-tree, and triptych.

Features

It also has faster startup times and better performance than nvim-lsp-file-operations

Installation

Using lazy.nvim

return {
  {
    "Crysthamus/nvim-file-operations",
    dependencies = {
      -- Uncomment whichever supported plugin(s) you use
      -- "nvim-tree/nvim-tree.lua",
      -- "nvim-neo-tree/neo-tree.nvim",
      -- "simonmclean/triptych.nvim"
    },
    config = function()
      require("nvim-file-operations").setup()
    end,
  },
}

Please use branch = "compat", if you are using versions older than 0.11

Setup

Initialize the plugin with the default configuration:

require("nvim-file-operations").setup()

To override the defaults, pass an options table:

require("nvim-file-operations").setup({
  -- Select which file operations to enable
  operations = {
    willRenameFiles = true,
    didRenameFiles = true,
    willCreateFiles = true,
    didCreateFiles = true,
    willDeleteFiles = true,
    didDeleteFiles = true,
  },
  -- How long to wait (in milliseconds) for LSP responses before cancelling
  timeout_ms = 10000,
  -- Saves modifies files after renames, moves, etc.
  auto_save = false
})

Some LSP servers also expect to be informed about the extended client capabilities:

vim.lsp.config("*", {
  capabilities = require("nvim-file-operations.config").default_capabilities(),
})

API

rename(opts)

Renames a file on disk, updates matching Neovim buffers to the new path, and notifies LSP clients.

require("nvim-file-operations").rename({
  old_name = "path/to/old_file.lua", -- Optional. Defaults to active buffer.
  new_name = "path/to/new_file.lua", -- Required.
})

create(opts)

Creates a new file on disk, ensures parent directories exist, notifies LSP clients, and opens the file.

require("nvim-file-operations").create({
  fname = "path/to/new_file.lua", -- Required.
})

delete(opts)

require("nvim-file-operations").delete({
  fname = "path/to/target.lua", -- Optional. Defaults to active buffer.
})