Opening Windows And File Buffers With The Neovim API And Commands

July - 2021
  • Creating a window requires associating it with a buffer
  • I'm making an empty buffer and then a window with this:
buffer1 = vim.api.nvim_create_buf(false, true)
window1 = vim.api.nvim_open_win(buffer1, true, {
        style="minimal", relative='editor',
        row=3, col=3, width=30, height=9, border='single'
    }
)
  • The false and true settings prevent the buffer from being pulled into the main list and identify it as a scratch buffer respectively. (Identifying it as scratch means you can add text to it and close it without getting the warning that you didn't save it)

  • This adds one window and one buffer to the respective counts provided by #vim.api.nvim_list_wins() and #vim.api.nvim_list_bufs()

  • Opening a file in the window is done with this while you're in the window:

vim.api.nvim_command(':e file-name.md')
  • When you do that, it doesn't add a new buffer. It appears to replace the initial empty buffer in the window

  • If you open a new file for editing with:

vim.api.nvim_command(':e file-2.md')

that will open the new buffer in the window and add one to the buffer count. The previous file buffer goes out of view but it's still open. You can switch back to it with something like:

:bn