nvim delete current file

This one is one that I've been using quite often, I did't have a hotkey for it, I just used the shell command.

This one is one that I’ve been using quite often, I did’t have a hotkey for it, I just used the rm shell command.

!!rm %<TAB><CR>

When you type !! from normal mode it will automatically put you in command mode with .! pre-filled, then you just type rm and <TAB> to auto-complete the current file name, and <CR> to execute the command.

:.!rm %<TAB><CR>

Making it better #

The one quirk that I don’t like about this is that the buffer remains open after deleting, and sometimes I forget to close it and end up re-creating it by mistake when running :wall or :xall.

Create a DeleteFile command with vim command.

:command! DeleteFile execute "!rm %" | bdelete!

Create a DeleteFile command with lua.

vim.api.nvim_create_user_command(
  'DeleteFile',
  function()
    -- Delete the current file from disk
    vim.cmd('!rm %')
    -- Close the buffer without saving
    vim.cmd('bdelete!')
  end,
  {}
)

Connections

Related tags and posts connected to this entry.