Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

One-file LastFile plugin#36956

shushtain started this conversation inShow and tell
Dec 14, 2025· 1 comments· 2 replies
Discussion options

One-file LastFile plugin

Neovim 0.11.5

There are already some great native capabilities like:mksession and great plugins likermagatti/auto-session. But I couldn't make it work for me. Either something breaks because a naughty plugin doesn't clean unrecoverable buffers onVimLeave, or I open a huge Rust project and wonder why everything is slow while having basically every buffer open from all the previous sessions.

Making a plugin that opens the last file (or several last files) you were working on is a leaner, make-it-and-forget-it kind of setup. And it is also a great exercise for working with cached files that improved my other plugins.

---Grouping autocmds is a good practice.---And we will be able to disable caching this waylocalgroup=vim.api.nvim_create_augroup("uLastFile", {clear=false })---Cache locationlocalstate=vim.fn.stdpath("state").."/lastfile/"---Hashing function, since we want OS-friendly filenameslocalfunctionhash()returnstate..vim.fn.sha256(vim.fn.getcwd())..".data"end---Restore last filevim.api.nvim_create_autocmd("VimEnter", {group=group,once=true,callback=function(env)---Ignore temporary, util filesifvim.bo[env.buf].buftype==""andvim.bo[env.buf].filetype==""andvim.api.nvim_buf_get_name(env.buf)==""andvim.api.nvim_buf_line_count(env.buf)==1thenlocalpath=hash()---Check if cache exists for this directory---"1" means file exists. I always second-guess it,---since "1" is also a shell error return codeifvim.fn.filereadable(path)==1thenlocalfile=vim.fn.readfile(path)localfilename=file[1]---Check if cached file still existsifvim.fn.filereadable(filename)==1thenlocalcurpos= {file[2],file[3] }---Schedule it after Neovim is fully loaded.---Wrap in pcalls, not to break occasional---swap file notifications and their restorationvim.schedule(function()-- open filepcall(vim.cmd,"e"..filename)-- restore cursor positionpcall(vim.fn.cursor,curpos)-- center cursor line in viewpcall(vim.cmd,"normal! zz")end)endreturnend---[[ Optional ]]---What's cool about custom plugins is that you can make things like---open the main Rust file if I didn't work on this repo before.path=vim.fn.getcwd().."/src/"ifvim.fn.filereadable(path.."main.rs")==1thenvim.schedule(function()vim.cmd("e"..path.."main.rs")end)elseifvim.fn.filereadable(path.."lib.rs")==1thenvim.schedule(function()vim.cmd("e"..path.."lib.rs")end)endendend,})vim.api.nvim_create_autocmd("BufWinLeave", {group=group,callback=function(env)localfiletype=vim.bo[env.buf].filetypelocalbufname=vim.api.nvim_buf_get_name(env.buf)localbuftype=vim.bo[env.buf].buftypelocalcwd=vim.fn.getcwd()---Ignore temp files, ignore $HOME directoryifbufname~=""andbuftype==""andfiletype~="gitcommit"andcwd~=vim.fn.getenv("HOME")then---Make cache directory if it doesn't existifvim.fn.isdirectory(state)==0thenvim.fn.mkdir(state,"p")endlocalpath=hash()localcurpos=vim.fn.getcurpos()localtbl= {bufname,curpos[2],curpos[3] }vim.fn.writefile(tbl,path)endend,})---Disable caching for this sessionvim.keymap.set("n","<Leader>ze",function()vim.api.nvim_del_augroup_by_id(group)vim.fn.delete(hash())vim.notify("LastFile is off")end, {desc="Purge : LastFile"})---Purge cache for all directoriesvim.keymap.set("n","<Leader>zo",function()vim.fn.delete(state,"rf")vim.notify("Purged LastFile cache")end, {desc="Purge : LastFile All"})
You must be logged in to vote

Replies: 1 comment 2 replies

Comment options

"or I open a huge Rust project and wonder why everything is slow while having basically every buffer open from all the previous sessions"

This can be customized with:h sessionoptions

You must be logged in to vote
2 replies
@shushtain
Comment options

It would still require writing a small plugin to handle sessions. And no single option reliably shows which files to store. Unfortunately, utility buffers that come from plugins don't necessarily set the rightunlisted,buftype and other parameters, so I had to DIY it.

@SebasF1349
Comment options

I meant you can only save buffers currently shown, not hidden ones. Even plugin buffers are not saved (if not shown). Afaik the other functionality you added can be added with just an option, so your code it needed for your usecase.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Labels
None yet
2 participants
@shushtain@SebasF1349

[8]ページ先頭

©2009-2025 Movatter.jp