I saw from the repo awesome neovim1 a plugin that helps the user create the intermediate dir when they save a file. I wondered why this feature even needs a whole plugin? You can literally do this with a few lines of code.

Here is a similar Lua snippet from my own config2:

-- Auto-create dir when saving a file, in case some intermediate directory does not exist
local api = vim.api

api.nvim_create_augroup("auto_create_dir", {
clear = true
})

api.nvim_create_autocmd({ "BufWritePre" }, {
pattern = "*",
group = "auto_create_dir",
callback = function()
local fpath = fn.expand('<afile>')
local dir = fn.fnamemodify(fpath, ":p:h")

-- utils.may_create_dir(dir)
local res = fn.isdirectory(dir)
if res == 0 then
fn.mkdir(dir, 'p')
end
end
})


This snippet is simple and do not require sophisticated knowledge to write. You can write it perhaps after reading the nvim lua guide. I do not think this is good for new users to learn how to use Neovim properly. It creates unnecessary dependencies in their config for external plugins. The extreme will be like the JavaScript world where there is dependency hell, you rely on external libraries for even a small feature/function you do not care to write. When that library fails/disappears, your library fails too, which is manifested in the famous left-pad incident.

My philosophy is that if you can create a feature easily without external plugins, try to write it yourself unless it will cost you a lot of effort, e.g., write a fully-featured git client for neovim (vim-fugitive level).

I think as a neovim user, if you are serious about it, you got to learn some basic things. Otherwise, you won’t go long in this road. In this sense, the hype over the pre-configuration is also doubtful. Can the new user really learn something from these pre-configurations, or are they just stuck in these pre-configurations, which may have their own config options? My guess is that these configurations can not really help the new users learn the basics.

Edit: I posted this post on reddit, someone gives a cleaner way to do this:

vim.api.nvim_create_autocmd({ "BufWritePre" }, {
pattern = "*",
group = vim.api.nvim_create_augroup("auto_create_dir", { clear = true }),
callback = function(ctx)
local dir = vim.fn.fnamemodify(ctx.file, ":p:h")
vim.fn.mkdir(dir, "p")
end
})


According to the doc, the callback accepts a table containing the context info. The file path for the buffer where this autocmd is fired is in the file key.

# References

• left-pad incident:

1. Modified a bit to make it self-running, original here↩︎