惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

aimingoo的专栏
aimingoo的专栏
I
InfoQ
B
Blog RSS Feed
D
Docker
GbyAI
GbyAI
N
Netflix TechBlog - Medium
Y
Y Combinator Blog
F
Fortinet All Blogs
P
Proofpoint News Feed
Microsoft Azure Blog
Microsoft Azure Blog
人人都是产品经理
人人都是产品经理
Martin Fowler
Martin Fowler
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
M
MIT News - Artificial intelligence
C
Check Point Blog
Vercel News
Vercel News
云风的 BLOG
云风的 BLOG
博客园 - Franky
Google DeepMind News
Google DeepMind News
WordPress大学
WordPress大学
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
V
V2EX
Last Week in AI
Last Week in AI
L
LangChain Blog

jdhao's digital space

Conversion between base64 and OpenCV or PIL Image 腾讯云对象存储博客图床开启 CDN 加速(不需要购买额外域名) Search and Replace in Multiple Files in Vim/Neovim Change Table Column Width in LaTeX Image or Table Side by Side in LaTeX LaTeX 并排显示图像或表格 Firenvim: Neovim inside Your Browser Content inside HTML tags missing in Latest Hugo? Creating Markdown Front Matter with Ultisnips Labelme JSON 标注格式转 voc XML 格式 Nifty Nvim Techniques That Make My Life Easier -- Series 6 macOS 下如何为视频制作字幕 Running Command Asynchronously inside Neovim Resolving Merge Conflict after Git Stash Pop Pylint: command not found? A Hands-on Experience with Neovim's Built-in LSP Support How to Convert PDF to Images with Imagemagick 互联网上常用缩略语集锦 File Backup in Neovim Converting PDF Pages to Images with Poppler Nifty Nvim Techniques That Make My Life Easier -- Series 5 Neovim Configuration for System-wide Use How to sort a list of tuple or list in Python -- lambda or itemgetter? Building A Vim Statusline from Scratch 人类第一颗原子弹爆炸始末 Distributed Training in PyTorch with Horovod Learning Expect Programming Essential Knowledge about SSH Nifty LaTeX Techniques -- Series 1 更改 Adsense 邮寄地址,重新寄送 PIN
You Do Not Need a Plugin for This Feature
2022-08-21 · via jdhao's digital space

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#