0

This script creates a 'nofile' buffer and loads it so it can be edited:

let n = bufadd('foo')
setbufvar(n, '&bt', 'nofile')
bufload(n)

But it has a big problem: The call to bufload will read a file named foo if it exists. This is a nofile buffer that should not touch any files on disk.

Then I tried to tackle this issue in a different way: Create a no-name buffer then set its name. But the only set-name function/command I can find is :file [name] which only works on the current buffer. This is a command not a function, so I get no return values. And it involves too much complexity such as BufEnter and WinEnter autocommands.

My questions are:

  1. Is there a way to ask bufload not to load the file on disk?

  2. Can we rename a buffer using a function bufrename({buf}), with the buffer number as an argument?

2
  • 1
    what happens when you don't give the buf a name e.g., bufadd('')?
    – Mass
    Aug 25, 2022 at 3:32
  • @Mass That buffer is loaded with nothing (after bufload), but without a name.
    – Cyker
    Aug 25, 2022 at 4:30

1 Answer 1

1

Set custom BufReadCmd

let bnr = bufadd("foo")
call setbufvar(bnr, "&buftype", "nofile")
call printf("au BufReadCmd <buffer=%d> :", bnr)->execute()
call bufload(bnr)
2
  • This should work. I was merely looking for a function and didn't think about autocommands. I still prefer functions though, but there seems to be none?
    – Cyker
    Aug 25, 2022 at 4:29
  • @Cyker No, and you don't need it for this case.
    – Matt
    Aug 25, 2022 at 4:46

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.