Vim-startify配置从Vimscript迁移至Lua:Git Modified与Untracked列表实现方法问询
Migrating Vim-startify Git Modified/Untracked Lists to Lua
Got it, let's get those git-specific lists working in your Lua config. Here's how to replicate your original Vimscript functionality perfectly:
Full Working Lua Config
-- Helper function to get git modified files local function git_modified() -- Run git command and capture output as a list local files = vim.fn.systemlist('git ls-files -m 2>/dev/null') -- Filter out empty lines (avoids empty sections when not in a git repo) local non_empty_files = vim.tbl_filter(function(file) return file ~= '' end, files) -- Map each file to the structure startify expects return vim.tbl_map(function(file) return { line = file, path = file } end, non_empty_files) end -- Helper function to get git untracked files local function git_untracked() local files = vim.fn.systemlist('git ls-files -o --exclude-standard 2>/dev/null') local non_empty_files = vim.tbl_filter(function(file) return file ~= '' end, files) return vim.tbl_map(function(file) return { line = file, path = file } end, non_empty_files) end -- Updated startify lists with git sections vim.g.startify_lists = { { type = "dir", header = { " MRU " .. vim.fn.getcwd() } }, { type = "sessions", header = { " Sessions" } }, { type = "bookmarks", header = { " Bookmarks" } }, { type = git_modified, header = { " git modified" } }, { type = git_untracked, header = { " git untracked" } }, { type = "commands", header = { " Commands" } }, }
Breakdown of Key Changes
- Replacing Vimscript Functions: We use Lua's
local functionsyntax instead of Vimscript'sfunction!. The core git command stays the same—we just call it viavim.fn.systemlist(Lua's equivalent of Vimscript'ssystemlist()). - Handling Empty Results: Added
vim.tbl_filterto strip out empty lines. This prevents startify from showing an empty git section when you're not in a Git repository. - Mapping to Startify's Expected Structure:
vim.tbl_mapdoes the same job as Vimscript'smap(), converting each filename into the{line = ..., path = ...}table that startify uses to render list items. - Adding to startify_lists: Just like in Vimscript, we reference the Lua functions directly in the
typefield of each list entry.
内容的提问来源于stack exchange,提问作者abidibo
相关产品推荐
相关产品推荐

