gitignore.nvim
A neovim plugin for generating .gitignore files in seconds, by allowing you to select from a huge number of different technologies.
This plugin is functionally identical to the service offered by
gitignore.io, but capable of
generating .gitignore
files offline, and directly from within neovim.
Installation & Dependencies
gitignore.nvim
optionally depends on
telescope.nvim to provide
multi-selection. Without installing
telescope you
will not be able to select multiple technologies.
After installing telescope.nvim
, you can install gitignore.nvim
like this:
Using vim-plug:
Plug 'wintermute-cell/gitignore.nvim'
Using dein:
call dein#add("wintermute-cell/gitignore.nvim")
Using packer.nvim:
use({
"wintermute-cell/gitignore.nvim",
requires = {
"nvim-telescope/telescope.nvim" -- optional: for multi-select
}
})
Usage
This plugin ships with only one command which when run,
it will create a buffer with the .gitignore
contents:
:Gitignore [path]
If an existing .gitignore
is found, the generated contents will be appended
to the existing lines. The buffer will not save automatically, so there is no
risk of overwriting an existing .gitignore
.
You can optionally pass a path
argument to point the command to a
specific directory (for example if you have nested .gitignore
files).
Alternatively, you can use the corresponding lua
function directly, for
example to create a keymap:
local gitignore = require("gitignore")
vim.keymap.set("n", "<leader>gi", gitignore.generate)
Or with a path:
local gitignore = require("gitignore")
local my_path = "./some/path"
vim.keymap.set("n", "<leader>gi",
function ()
gitignore.generate(my_path)
end
)
Selecting multiple items
Without telescope, gitignore.nvim
does not allow you to select multiple
technologies for your .gitignore
, since the fallback picker, vim.ui.select()
,
can only select one item.
Therefore, if you want to be able to select multiple technologies, you must
either install
telescope.nvim
(you may find an example using packer.nvim
in the
Installation section), or override the provided
generate
method with your own implementation (see section
below).
gitignore.nvim
will detect if telescope.nvim
is installed and use it
automatically, there is no further configuration required.
Selecting multiple items with telescope.nvim installed
gitignore.nvim
makes use of telescope.nvim
's multi-selection keybinds.
This means that by default, you can (de-)select multiple keywords with <Tab>
,
and confirm your selection with <CR>
(Enter).
In case of multiple selected keywords, the keyword highlighted you press <CR>
on will not be added to the selection!
For convenience, when no multi-selection is made before pressing <CR>
,
<CR>
will actually add the highlighted item to the selection, and create
a .gitignore
file for the single keyword.
Configuration
If you want the :Gitignore
command to overwrite your current .gitignore
instead of appending to it, you can set:
vim.g.gitignore_nvim_overwrite = true
If this variable is set to false
, or not set at all, :Gitignore
will take
into account an existing .gitignore
.
Custom Picker
Instead of using telescope.nvim
or the native vim.ui.select()
, you may
implement your own solution according to the following contract:
gitignore.nvim
provides list of templateNames and two methodsgenerate
andcreateGititnoreBuffer
.- As its first parameter, the
generate
method will receive anopts
table, containing the target path for the.gitignore
inopts.args
. - One must pass on
opts.args
, and a list of selected templateNames tocreateGitignoreBuffer
.
Here's an example implementation using fzf-lua:
local gitignore = require("gitignore")
local fzf = require("fzf-lua")
gitignore.generate = function(opts)
local picker_opts = {
-- the content of opts.args may also be displayed here for example.
prompt = "Select templates for gitignore file> ",
winopts = {
width = 0.4,
height = 0.3,
},
actions = {
default = function(selected, _)
-- as stated in point (3) of the contract above, opts.args and
-- a list of selected templateNames are passed.
gitignore.createGitignoreBuffer(opts.args, selected)
end,
},
}
fzf.fzf_exec(function(fzf_cb)
for _, prefix in ipairs(gitignore.templateNames) do
fzf_cb(prefix)
end
fzf_cb()
end, picker_opts)
end
Note
Note that the above will not overwrite the :Gitignore
command.
To do that, recreate the command after defining your generate function as
follows:
vim.api.nvim_create_user_command("Gitignore", gitignore.generate, { nargs = "?", complete = "file" })
Demo
Credits
Thanks to Toptal for providing a huge list of ignore-templates!