Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork6.5k
One-file LastFile plugin#36956
-
One-file LastFile plugin
There are already some great native capabilities like 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"}) |
BetaWas this translation helpful?Give feedback.
All reactions
Replies: 1 comment 2 replies
-
"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 |
BetaWas this translation helpful?Give feedback.
All reactions
-
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 right |
BetaWas this translation helpful?Give feedback.
All reactions
-
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. |
BetaWas this translation helpful?Give feedback.
All reactions
👎 1