jrock2004
jrock2004

Reputation: 3501

Neovim Lua isdirectory vim function

I am hoping to set up keymaps for when I am in a git directory and when I am not. Maybe now that I am using Lua that maybe I cannot do this like this. Any help would be appreciated.

if vim.fn.isdirectory('.git') then
  map('n', '<leader>t', '<cmd>lua require(\'telescope.builtin\').git_files({hidden = true})<CR>', options)
else
  map('n', '<leader>e', '<cmd>lua require(\'telescope.builtin\').find_files({hidden = true})<CR>', options)
end

Seems like if always hits and never the else.

Upvotes: 1

Views: 3677

Answers (1)

Piglet
Piglet

Reputation: 28974

:h isdirectory

isdirectory({directory})

isdirectory() The result is a Number, which is non-zero when a directory with the name {directory} exists. If {directory} doesn't exist, or isn't a directory, the result is FALSE. {directory} is any expression, which is used as a String.

:h FALSE

For boolean operators Numbers are used. Zero is FALSE, non-zero is TRUE. You can also use |v:false| and |v:true|. When TRUE is returned from a function it is the Number one, FALSE is the number zero.

Make sure that FALSE is actually false. It is probably 0 which would be a true value in Lua. In Lua any values but false or nil are true.

So ideally check if vim.fn.isdirectory('.git') ~= 0 then

Upvotes: 3

Related Questions