1

Title says it all really. Sorry for noob question.

I've noticed that if 'hidden' is set, :e! will do the same thing as :e (that is, put the buffer you're currently working on in the background and open the file you specify in a new one).

If 'hidden' is not set, :e! will discard the current buffer, which I want, but :e will warn and do nothing if it hasn't been saved.

Often, while testing out new features of the editor as I learn about them, I open a new buffer, play with it for a bit, and then want to discard it to edit an actual file. I can't close the buffer without quitting the whole editor unless I have another text file open already besides the help file I just learned the command from (which I often don't).

What I often end up doing is :e! the file I want to edit and then forgetting that I had that scratch buffer open until I go to quit, at which point Vim helpfully reminds me that I haven't saved it.

I could simply turn 'hidden' off, but I'd rather not write off the buffer feature altogether.

Is there a way, maybe through a plugin, to tell Neovim which action I want it to do on a per-command basis, or do I have to open the file, then switch to the buffer I was just editing and close it?

That seems awfully tedious and like the sort of thing neovim would have a solution for.

3
  • 1
    Probably an XY problem. Have you tried :bd or :bw?
    – r_31415
    Aug 17, 2022 at 22:55
  • 1
    :h 'bufhidden' to set action individually per buffer
    – Matt
    Aug 18, 2022 at 2:11
  • 1
    Let's be specific: are you running the command :edit or :edit {filename} (with/without bang)? Because :edit! by itself loads the existing file from disk, which is not quite what you've described. You might also want to read :help windows
    – D. Ben Knoble
    Aug 18, 2022 at 13:15

3 Answers 3

1

Possibly I’m misunderstanding you, but I’m going to ignore most of your question and focus on this part which I think describes your main goal?

I open a new buffer, play with it for a bit, and then want to discard it to edit an actual file. I can't close the buffer without quitting the whole editor

You can use the command :bd! to discard your scratch buffer, and can then edit your file and subsequently quit Vim without any further warnings.

1

There's fundamental misunderstanding here: in Vim (the most of) options are consulted by specific events, not by specific commands that may trigger such events. That is, say, "hidden" is checked whenever a buffer becomes hidden (no matter why!) and Vim is unsure if it should be unloaded or not. Thence, it is the commands who rule the options (e.g. "quit!" overrules "hidden", or "hide edit" overrules "nohidden", etc.) but not vice versa.

In your case you should probably do set buftype=nofile, so that Vim knows that it is a temporary buffer and never tries to write it on disk. See also :h scratch-buffer.

0

I am not aware of a native method for doing this with :e/:e!, but it is doable with a new user command.

Since you are working in Neovim, I'm going to assume you are okay with lua to solve this. It is possible with strictly vimscript, but it will be more annoying to create.

First, we need to create a function which opens a new file, and if the ! is used delete the previous buffer. The arguments to this function will be formatted according to nvim_create_user_command. But the important bits from that help doc are the "args" entry, which is where we will be getting the filename from, and the "bang" entry, which is how we know if the ! was used.

local fancy_edit = function(args)
  local bang = args["bang"]
  local filename = args["args"]
  local buf_num = vim.api.nvim_get_current_buf() -- Save current buffer number
  vim.cmd("e " .. filename) -- Open the file (or blank filename is fine too)
  -- If a filename is provided with the ! option, delete the previous buffer
  if bang and filename ~= "" then
    vim.api.nvim_buf_delete(buf_num, {force = true})
  end
end

Now that we have a function to do what we need, we just need to create the actual user command.

vim.api.nvim_create_user_command("FancyEdit", fancy_edit, {nargs = "?", complete = "file", bang = true})

The nargs=? specifies the command takes either 0 or 1 arguments, i.e. can take a filename or not. The complete option isn't strictly necessary, but it tells neovim what completion source to use for the arguments; in this case, it should complete based on filenames. Finally, the bang=true argument tells neovim that it should allow an ! to be used to modify the commands behavior.

Now to actually use this, you can either just chuck it in init.lua, or put it in some other file that you require. Then instead of using :e/:e! you can use :FancyEdit/:FancyEdit! (or whatever you want to rename it, "E" might be a good option)

3
  • 4
    None of this code would be “annoying to create” in vimscript
    – D. Ben Knoble
    Aug 18, 2022 at 20:45
  • 2
    In fact, the opposite is true. It is more verbose and convoluted in Lua.
    – r_31415
    Aug 19, 2022 at 19:01
  • @D.BenKnoble most people just follow the bandwagon. I do, too. But fads die down. Eventually
    – 3N4N
    Sep 18, 2022 at 14:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.