How to get current buftype? I want to check whether the terminal is opened

As title. I want to use this info to check whether the cursor is on the floating terminal. If you could provide an example, it will be very helpful. Does vim.o.buftype exist?

My current trying:

function _G.__change_cwd_to_grandparent()
  if (not string.find("dashboard NvimTree", vim.o.filetype) and string.find(vim.fn.expand("%:p:h"), "/")) then
    vim.api.nvim_set_current_dir(vim.fn.expand("%:p:h"))
  end
end

That should be vim.bo.buftype.

1 Like

How to print the vim.bo.buftype when the floating terminal is open?

I’m using this one but it didn’t work

vim.cmd'au BufEnter * call v:lua.__change_cwd_to_grandparent()'
function _G.__change_cwd_to_grandparent()
  if (not string.find("dashboard NvimTree", vim.o.filetype)
    and not string.find("terminal", vim.bo.buftype)
    and string.find(vim.fn.expand("%:p:h"), "/")) then
    vim.api.nvim_set_current_dir(vim.fn.expand("%:p:h"))
    print(vim.fn.expand("%:p:h"))
  end
end

It turns out to be somehow complex. The following code works:

vim.cmd'au CursorHold * pwd'
vim.cmd'au BufEnter * call v:lua.__change_cwd_to_grandparent()'
function _G.__change_cwd_to_grandparent()
  if (
    vim.bo.buftype ~= "terminal"
    and vim.o.filetype ~= "dashboard"
    and vim.o.filetype ~= "NvimTree"
  ) then
    local test_path = "%:p:h"
    local count = 0
    while (
      count+1 <= 2
      and string.len(vim.fn.expand(test_path .. ":h")) >= string.len(vim.fn.expand("$HOME"))
    ) do
      local _, last = string.find(vim.fn.expand(test_path .. ":h"), "GitHub")
      if last == string.len(vim.fn.expand(test_path .. ":h")) then
        break
      end
      test_path = test_path .. ":h"
      count = count + 1
    end
    vim.api.nvim_set_current_dir(vim.fn.expand(test_path))
  end
end

My final version. If you know how to optimize it please tag me:

vim.cmd'au CursorHold * pwd'
vim.cmd'au BufEnter * call v:lua.change_cwd_to_grandparent()'
vim.api.nvim_set_keymap('n', '<Leader>prr', '<cmd>lua _G.reset_project_root(); print("Project Root Has Been Reset"); _G.change_cwd_to_grandparent()<CR>', NOREF_NOERR_TRUNC)
_G.__PROJECT_ROOT_CONST = {
  "GitHub",
  "GitHub%-2",
}
function _G.reset_project_root()
  for k in pairs(_G.__PROJECT_ROOT) do
    local found = false
    for kk in pairs(_G.__PROJECT_ROOT_CONST) do
      if k == kk then
        found = true
        break
      end
    end
    if not found then
      _G.__PROJECT_ROOT[k] = nil
    end
  end
end
vim.api.nvim_set_keymap('n', '<Leader>pra', '<cmd>lua table.insert(_G.__PROJECT_ROOT, vim.fn.input("Extend Project Root: ")); _G.change_cwd_to_grandparent()<CR>', NOREF_NOERR_TRUNC)
_G.__PROJECT_ROOT = {
  "GitHub",
  "GitHub%-2",
}
function _G.change_cwd_to_grandparent()
  if (
    vim.bo.buftype ~= "terminal"
    and vim.o.filetype ~= "dashboard"
    and vim.o.filetype ~= "NvimTree"
  ) then
    local test_path = "%:p:h"
    local count = 0
    while (
      count+1 <= 2
      and string.len(vim.fn.expand(test_path .. ":h")) >= string.len(vim.fn.expand("$HOME"))
    ) do
      local found_root = false
      for idx = 1, #_G.__PROJECT_ROOT do
        local _, last = string.find(vim.fn.expand(test_path .. ":h"), _G.__PROJECT_ROOT[idx])
        if string.len(vim.fn.expand(test_path .. ":h")) == last then
            found_root = true
            break
        end
      end
      if found_root then break end
      test_path = test_path .. ":h"
      count = count + 1
    end
    vim.api.nvim_set_current_dir(vim.fn.expand(test_path))
  end
end

There is also nvim specific functions

if vim.api.nvim_buf_get_option(buf, 'buftype') == 'terminal' then 
    -- buffer is a terminal
end

Where buf is the buffer id returned from something such as nvim_win_get_buf(window)

So what should I change?