From e085a5cc2369c566d29aa099ced54cd4f7e89764 Mon Sep 17 00:00:00 2001 From: Jonas Hahn Date: Fri, 29 Aug 2025 13:34:49 +0200 Subject: [PATCH] Cleaned up the keymaps such that the keymaps are where the plugins are. This is for easy sharing of configurations --- lua/config/keymaps.lua | 135 +++++++++++++++++++------------------- lua/custom/typst.lua | 40 ++++------- lua/plugins/custom.lua | 13 ++-- lua/plugins/git.lua | 2 +- lua/plugins/lsp.lua | 2 +- lua/plugins/misc.lua | 83 ++++++++++++++++++----- lua/plugins/nvimtree.lua | 6 ++ lua/plugins/searching.lua | 62 +++++++++++++++++ lua/plugins/typstar.lua | 8 +++ lua/plugins/ui.lua | 8 ++- lua/utils/functions.lua | 90 ------------------------- 11 files changed, 235 insertions(+), 214 deletions(-) diff --git a/lua/config/keymaps.lua b/lua/config/keymaps.lua index 2389894..bbbc03a 100644 --- a/lua/config/keymaps.lua +++ b/lua/config/keymaps.lua @@ -3,12 +3,11 @@ require("utils.functions") require("custom.uni") -local season = "S2" +-- TODO: move this to a config file +local season = "S3" local links = require("utils.linker") local user = vim.fn.system('whoami'):gsub('\n', '') -local api = require("nvim-tree.api") -local builtin = require('telescope.builtin') local function open_cal() local current_date = os.date("%Y-%m-%d") @@ -37,66 +36,67 @@ vim.keymap.set("n", "w", "w") --------------------- NORMAL ------------------------- -vim.keymap.set("n", "L", ":BufferNext", { silent = true }) -vim.keymap.set("n", "n", "nzz", { silent = true }) -vim.keymap.set("n", "N", "Nzz", { silent = true }) -vim.keymap.set("n", "H", ":BufferPrevious", { silent = true }) -vim.keymap.set("n", "", "zz", { silent = true }) -vim.keymap.set("n", "", "zz", { silent = true }) -vim.keymap.set("n", "", "zz", { silent = true }) -vim.keymap.set("n", "", "zz", { silent = true }) -vim.keymap.set('n', 'a', 'm9ggVG"+y`9') -vim.keymap.set('n', 'va', 'ggVG') - --- Launch panel if nothing is typed after z -vim.keymap.set("n", "z", "Telekasten panel") - --- Most used functions -vim.keymap.set("n", "zf", "Telekasten find_notes") -vim.keymap.set("n", "zg", "Telekasten search_notes") vim.keymap.set('n', 'zq', ':e ~/synced/brainstore/zettelkasten/input.txt`.zz') -vim.keymap.set("n", "zd", "Telekasten goto_today") -vim.keymap.set("n", "zr", "Telekasten rename_note") -vim.keymap.set("n", "zz", "Telekasten follow_link") -vim.keymap.set("n", "zn", "Telekasten new_note") -vim.keymap.set("n", "zb", "Telekasten show_backlinks") -vim.keymap.set("n", "zw", "Telekasten find_weekly_notes") -vim.keymap.set("n", "zI", "Telekasten insert_img_link") + +-- Buildin vim vim.keymap.set("n", "me", ":mes") -vim.keymap.set("n", "", ":ToggleTerm") -vim.keymap.set("t", "", ":ToggleTerm") vim.keymap.set("n", "snt", "set nu") vim.keymap.set("n", "snf", "set nonu") --- Call insert link automatically when we start typing a link -vim.keymap.set("n", "il", "Telekasten insert_link") - vim.keymap.set("n", "nv", function() select_course_directory() end, { desc = "Open UniCourse menu" }) +vim.keymap.set("n", "n", "nzz", { silent = true }) +vim.keymap.set("n", "N", "Nzz", { silent = true }) +vim.keymap.set("n", "", "zz", { silent = true }) +vim.keymap.set("n", "", "zz", { silent = true }) +vim.keymap.set("n", "", "zz", { silent = true }) +vim.keymap.set("n", "", "zz", { silent = true }) +vim.keymap.set('n', 'a', 'm9ggVG"+y`9') +vim.keymap.set('n', 'va', 'ggVG') -vim.keymap.set('n', 'ca', 'ggVGd') -vim.keymap.set("n", "bd", ":BufferDelete", { silent = true }) --- Typstar stuff -vim.keymap.set("n", "ti", ":TypstarInsertRnote", { silent = true }) -vim.keymap.set("n", "to", ":TypstarOpenDrawing", { silent = true }) -- Get a ready to use terminal vim.keymap.set('n', 'tr', ':tabnew:termi') -vim.keymap.set("n", "tt", ":Telescope", { desc = "Follow Link" }) -- This needs to be refined for quick access to a new file or a recently edited one -vim.keymap.set('n', 'ov', open_vorlesung) +vim.keymap.set('n', 'ov', + function() + local uni_dir = vim.fn.expand("~/projects/university/" .. season) + + require('telescope.builtin').find_files { + prompt_title = "Select Vorlesung in " .. season, + cwd = uni_dir, + find_command = { + "eza", "-1", "-D" + }, + } + end +) -- new quick note file -- TODO: make this smarter vim.keymap.set("n", "nn", ":e ~/synced/brainstore/zettelkasten/quick", { silent = true }) -vim.keymap.set("n", "r", set_root) +vim.keymap.set("n", "r", + function() + local current_file = vim.fn.expand('%:p:h') -- get directory of current file + local cmd = 'git -C ' .. vim.fn.fnameescape(current_file) .. ' status' + vim.fn.system(cmd) + if vim.v.shell_error == 0 then + local git_root = vim.fn.systemlist('git -C ' .. + vim.fn.fnameescape(current_file) .. ' rev-parse --show-toplevel') + [1] + vim.cmd('cd ' .. vim.fn.fnameescape(git_root)) + else + vim.cmd('cd ' .. vim.fn.fnameescape(current_file)) + end + end +) -- Quickly open some buffers vim.keymap.set('n', 'occ', ':e ~/.config/nvim/init.lua`.zz') @@ -106,7 +106,7 @@ vim.keymap.set('n', 'ocd', ':e ~/.config/nvim/lua/config/autocmds.luaoco', ':e ~/.config/nvim/lua/config/options.lua`.zz') vim.keymap.set('n', 'ocl', ':e ~/.config/nvim/lua/config/lazy.lua`.zz') vim.keymap.set('n', 'oczl', ':e ~/.config/nvim/lua/config/lsp.lua`.zz') -vim.keymap.set('n', 'ocp', ':e ~/.config/nvim/lua/plugins/main.lua`.zz') +vim.keymap.set('n', 'ocp', ':e ~/.config/nvim/lua/plugins/misc.lua`.zz') vim.keymap.set('n', 'ocf', ':e ~/.config/nvim/lua/utils/functions.lua`.zz') vim.keymap.set('n', 'oca', ':e ~/.config/nvim/lua/utils/after.lua`.zz') vim.keymap.set('n', 'oq', ':e ~/synced/brainstore/input.txt`.zz') @@ -122,7 +122,27 @@ vim.keymap.set('n', 'od', ':e ~/synced/brainstore/todos/done.txt`.zz vim.keymap.set('n', 'ou', ':e ~/projects/university/' .. season .. '/input.txt`.zz') vim.keymap.set('n', 'oz', ':e ~/.zshrc`.zz') vim.keymap.set('n', 'oaa', ':e ~/.common_shell`.zz') -vim.keymap.set("n", "or", "lua open_last_file()", { noremap = true, silent = true }) +vim.keymap.set("n", "or", + function() + local last_file_path = vim.fn.stdpath('data') .. "/lastfile.txt" + local file = io.open(last_file_path, "r") + if file then + local last_file = file:read("*line") + file:close() + if last_file and vim.fn.filereadable(last_file) == 1 then + vim.cmd("edit " .. last_file) + pcall(function() + vim.cmd('normal! `.') -- Go to the last edit position + vim.cmd('normal! zz') -- Center the cursor on the screen + end) + else + print("Last file does not exist or is not readable") + end + else + print("No last file found") + end + end + , { noremap = true, silent = true }) vim.keymap.set('n', 'ok', open_cal) @@ -135,12 +155,10 @@ vim.keymap.set("n", "lp", links.insert_project_link, { desc = "Link Proj vim.keymap.set("n", "lc", links.insert_contact_link, { desc = "Link Contact" }) vim.keymap.set("n", "ld", links.insert_date_link, { desc = "Link Contact" }) --- Nvim tree -vim.keymap.set("n", "e", function() - api.tree.toggle({ find_file = true, update_root = true, focus = true, }) -end, { silent = true }) -- also update the root with the bang +-- Indent all will be replaced by the formatting of lsp where the lsp is installed vim.keymap.set('n', 'ia', 'gg=Gzz') + vim.keymap.set('n', 'ya', 'ggVG"+y') vim.keymap.set('n', 'ss', ':wa') @@ -151,6 +169,8 @@ vim.keymap.set('n', 'sw', function() vim.cmd(string.format("%%s/\\<%s\\>/%s/gI", vim.fn.escape(word, '\\/'), vim.fn.escape(replacement, '\\/'))) end end, { desc = "Substitute word under cursor (prompt)" }) + +-- Substitution in visual mode vim.keymap.set('v', 'sv', function() -- Save the current selection local save_reg = vim.fn.getreg('"') @@ -181,29 +201,6 @@ vim.keymap.set('v', 'p', function() vim.cmd('normal! "+p') end, { desc = 'Yank to clipboard and keep the selection' }) -vim.keymap.set('n', 'fr', function() - require('telescope.builtin').oldfiles({ - disable_devicons = true, - }) -end, { noremap = true, silent = true }) -vim.keymap.set('n', 'fb', builtin.buffers, { desc = 'Telescope buffers' }) -vim.keymap.set('n', 'fh', builtin.help_tags, { desc = 'Telescope help tags' }) -vim.keymap.set("n", "fl", links.follow_link, { desc = "Follow Link" }) - - -vim.keymap.set('n', 'g', function() - require('telescope.builtin').live_grep({ - disable_devicons = true, - cwd = vim.fn.getcwd(), -- set the starting directory - additional_args = function() - return { '--hidden', '--glob', '!.git/*' } -- include hidden files but exclude .git - end, - }) -end, { noremap = true, silent = true }) - -vim.keymap.set('n', '', find_eff, - { desc = 'Telescope find files (with dotfiles and folders but excluding .git, .cache, .local, and large files)' }) - ------------------------ VISUAL ------------------ vim.keymap.set('v', 'p', function() diff --git a/lua/custom/typst.lua b/lua/custom/typst.lua index 19a7cf3..268edb9 100644 --- a/lua/custom/typst.lua +++ b/lua/custom/typst.lua @@ -1,41 +1,33 @@ +local M = {} + local functions = require("utils.functions") + local watch_job_id = nil local watch_buf_id = nil +local watch_tab_id = nil -local function watch_and_open() +function M.Watch_and_open() + -- Parse the current file and check for typst + vim.notify("INIT", vim.log.levels.WARN) local input = vim.fn.expand("%:p") if not input:match("%.typ$") then vim.notify("Not a Typst file", vim.log.levels.WARN) return end - local dir = vim.fn.fnamemodify(input, ":h") -- directory of the Typst file local filename = vim.fn.expand("%:t:r") .. ".pdf" -- filename without extension + .pdf - -- Get the underlying unicourse dir - local one_up = vim.fn.fnamemodify(dir, ":h") - - print(one_up) - local pdf_dir = nil - if vim.fn.filereadable(one_up .. "/.unicourse") == 1 then - pdf_dir = one_up .. "/../../pdfs" - else - pdf_dir = dir - end - - vim.fn.mkdir(pdf_dir, "p") - local output = pdf_dir .. "/" .. filename - -- Check if a watcher is already running for this file if watch_job_id then vim.notify("Typst watcher already running - please close zathura", vim.log.levels.INFO) return end - + local output = dir .. "/" .. filename -- Start typst watch local cwd = vim.fn.getcwd() -- set the starting directory + -- TODO: root setting does not work local watch_cmd = { "typst", "watch", "--root", cwd, input, output } watch_job_id = vim.fn.jobstart(watch_cmd, { @@ -44,7 +36,7 @@ local function watch_and_open() on_stderr = function(_, data) if data then if not watch_tab_id then - pre_tab = vim.api.nvim_get_current_tabpage() -- Get the current tab ID + local pre_tab = vim.api.nvim_get_current_tabpage() -- Get the current tab ID vim.cmd('tabnew') -- Open a new tab watch_tab_id = vim.api.nvim_get_current_tabpage() -- Get the current tab ID watch_buf_id = vim.api.nvim_get_current_buf() -- Get the buffer ID of the new tab @@ -63,12 +55,6 @@ local function watch_and_open() end end, on_exit = function(_, exit_code) - -- Ensure to close the tab that holds the logs - --if watch_tab_id then - -- -- Switch to the tab holding the log buffer and close it - -- vim.api.nvim_set_current_tabpage(watch_tab_id) - -- vim.cmd('tabclose') -- Close the tab holding the log buffer - --end if exit_code == 0 then vim.notify("Typst watch stopped successfully", vim.log.levels.INFO) else @@ -79,8 +65,6 @@ local function watch_and_open() }) vim.notify("Started Typst watch", vim.log.levels.INFO) - -- Start sioyek with the --new-window flag and stop watch when it exits - -- ensure that there is no sioyek vim.fn.system("killall .zathura-wrapped") functions.Sleep(0.5) vim.fn.jobstart({ "zathura", output }, { @@ -93,6 +77,4 @@ local function watch_and_open() }) end -return { - Watch_and_open = watch_and_open -} +return M diff --git a/lua/plugins/custom.lua b/lua/plugins/custom.lua index 95da755..ccacec0 100644 --- a/lua/plugins/custom.lua +++ b/lua/plugins/custom.lua @@ -1,14 +1,15 @@ --- Custom modules - ---- @param name string +--- @param name? string --- @return string -local function get_dir(name) +local function get_custom_dir(name) + if name == nil then + return vim.fn.stdpath("config") .. "/lua/custom" + end return vim.fn.stdpath("config") .. "/lua/custom/" .. name end return { { - dir = get_dir("todo"), + dir = get_custom_dir("todo"), name = "todo", dependencies = { "nvim-lua/plenary.nvim", "nvim-telescope/telescope.nvim" }, config = function() @@ -16,7 +17,7 @@ return { end, }, { - dir = vim.fn.stdpath("config") .. "/lua/custom", -- folder containing typst.lua + dir = get_custom_dir(), name = "typst", dependencies = { "nvim-lua/plenary.nvim", "nvim-telescope/telescope.nvim" }, keys = { diff --git a/lua/plugins/git.lua b/lua/plugins/git.lua index d191e72..a0f82b1 100644 --- a/lua/plugins/git.lua +++ b/lua/plugins/git.lua @@ -80,7 +80,7 @@ return { -- Toggles map('n', 'tb', gitsigns.toggle_current_line_blame) - map('n', 'tw', gitsigns.toggle_word_diff) + -- map('n', 'tw', gitsigns.toggle_word_diff) -- Text object map({ 'o', 'x' }, 'ih', gitsigns.select_hunk) diff --git a/lua/plugins/lsp.lua b/lua/plugins/lsp.lua index 4430cc9..b860572 100644 --- a/lua/plugins/lsp.lua +++ b/lua/plugins/lsp.lua @@ -1,5 +1,5 @@ return { - { "stevearc/aerial.nvim", opts = {} }, + { "stevearc/aerial.nvim", config = true }, { "nvimdev/lspsaga.nvim", opts = { diff --git a/lua/plugins/misc.lua b/lua/plugins/misc.lua index 28bf47c..c344a40 100644 --- a/lua/plugins/misc.lua +++ b/lua/plugins/misc.lua @@ -1,26 +1,77 @@ local base_zet = "~/synced/brainstore/zettelkasten" return { - { "ThePrimeagen/harpoon" }, - { "akinsho/toggleterm.nvim", config = true }, + { + "ThePrimeagen/harpoon", + config = function() + vim.keymap.set("n", "af", function() + require("harpoon.mark").add_file() + end) + vim.keymap.set("n", "hf", function() + require("harpoon.ui").toggle_quick_menu() + end) + vim.keymap.set("n", "Hn", function() + require("harpoon.ui").nav_next() + end) + vim.keymap.set("n", "Hp", function() + require("harpoon.ui").nav_prev() + end) + end + }, + { + "akinsho/toggleterm.nvim", + config = function() + -- Cannot setup with opts here need to call before using the command for the plugin + require("toggleterm").setup({ + size = 20, + direction = "horizontal", + }) + + local first_open = false + local function toggle_term() + vim.cmd("ToggleTerm") + if first_open == true then + vim.cmd("startinsert") + end + end + + vim.keymap.set("n", "", toggle_term, { noremap = true, silent = true }) + vim.keymap.set("t", "", toggle_term, { noremap = true, silent = true }) + end + }, { 'Ascyii/telekasten.nvim', dev = true, - opts = { - home = vim.fn.expand(base_zet), - dailies = vim.fn.expand(base_zet .. "/daily"), + config = function() + -- Again can only use opts when not using config + require("telekasten").setup({ + home = vim.fn.expand(base_zet), + dailies = vim.fn.expand(base_zet .. "/daily"), - image_subdir = vim.fn.expand(base_zet .. "/media"), - weeklies = vim.fn.expand(base_zet .. "/weekly"), - templates = vim.fn.expand(base_zet .. "/templates"), - template_new_note = vim.fn.expand(base_zet .. "/templates/note.md"), - template_new_daily = vim.fn.expand(base_zet .. "/templates/daily.md"), - template_new_weekly = vim.fn.expand(base_zet .. "/templates/weekly.md"), + image_subdir = vim.fn.expand(base_zet .. "/media"), + weeklies = vim.fn.expand(base_zet .. "/weekly"), + templates = vim.fn.expand(base_zet .. "/templates"), + template_new_note = vim.fn.expand(base_zet .. "/templates/note.md"), + template_new_daily = vim.fn.expand(base_zet .. "/templates/daily.md"), + template_new_weekly = vim.fn.expand(base_zet .. "/templates/weekly.md"), - filename_format = "%Y%m%d%H%M-%title%", - new_note_filename = "uuid-title", - uuid_type = "%Y%m%d%H%M", - uuid_separator = "-", - } + filename_format = "%Y%m%d%H%M-%title%", + new_note_filename = "uuid-title", + uuid_type = "%Y%m%d%H%M", + uuid_separator = "-", + }) + + vim.keymap.set("n", "z", "Telekasten panel") + vim.keymap.set("n", "zf", "Telekasten find_notes") + vim.keymap.set("n", "zg", "Telekasten search_notes") + vim.keymap.set("n", "zd", "Telekasten goto_today") + vim.keymap.set("n", "zr", "Telekasten rename_note") + vim.keymap.set("n", "zz", "Telekasten follow_link") + vim.keymap.set("n", "zn", "Telekasten new_note") + vim.keymap.set("n", "zb", "Telekasten show_backlinks") + vim.keymap.set("n", "zw", "Telekasten find_weekly_notes") + vim.keymap.set("n", "il", "Telekasten insert_link") + vim.keymap.set("n", "zI", "Telekasten insert_img_link") + end }, }; diff --git a/lua/plugins/nvimtree.lua b/lua/plugins/nvimtree.lua index d6f91e4..dd55bbd 100644 --- a/lua/plugins/nvimtree.lua +++ b/lua/plugins/nvimtree.lua @@ -1,5 +1,11 @@ return { "nvim-tree/nvim-tree.lua", + keys = { + { "e", function() + require("nvim-tree.api").tree.toggle({ find_file = true, update_root = true, focus = true, }) + end + } + }, opts = { sort_by = "case_sensitive", diff --git a/lua/plugins/searching.lua b/lua/plugins/searching.lua index 3f52a83..d248c92 100644 --- a/lua/plugins/searching.lua +++ b/lua/plugins/searching.lua @@ -7,5 +7,67 @@ return { { "nvim-telescope/telescope.nvim", dependencies = { "nvim-lua/plenary.nvim" }, + lazy = true, + keys = { + { + "tt", + ":Telescope", + desc = "Telescope menu", + }, + { + "", + function() + require("telescope.builtin").buffers() + end, + desc = "Fzf current buffers" + }, + { + "fr", + function() + require('telescope.builtin').oldfiles({ + disable_devicons = true, + }) + end, + desc = "Open last files" + }, + + { + "g", + function() + require('telescope.builtin').live_grep({ + disable_devicons = true, + cwd = vim.fn.getcwd(), + additional_args = function() + return { '--hidden', '--glob', '!.git/*' } + end, + }) + end + }, + { + "fh", + function() + require('telescope.builtin').help_tags() + end, + }, + { + "", + function() + require('telescope.builtin').find_files({ + hidden = true, + no_ignore = true, -- Also show files in gitignore + follow = true, + disable_devicons = true, + prompt_title = "Find Files - custom", + find_command = { + "rg", "--files", + "--glob", "!**/.git/*", + "--glob", "!**/build/*", + "--glob", "!**/*.{jpg,png,gif,mp4,mkv,tar,zip,iso}" + } + }) + end + } + }, + config = true, }, } diff --git a/lua/plugins/typstar.lua b/lua/plugins/typstar.lua index 1c213f0..e875698 100644 --- a/lua/plugins/typstar.lua +++ b/lua/plugins/typstar.lua @@ -23,6 +23,14 @@ return { "TypstarSmartJumpBack", mode = { "s", "i" }, }, + { + "ti", + ":TypstarInsertRnote", + }, + { + "to", + ":TypstarOpenDrawing", + }, }, config = function() local typstar = require("typstar") diff --git a/lua/plugins/ui.lua b/lua/plugins/ui.lua index bdb0878..507b7bd 100644 --- a/lua/plugins/ui.lua +++ b/lua/plugins/ui.lua @@ -63,8 +63,6 @@ return { 'lewis6991/gitsigns.nvim', 'nvim-tree/nvim-web-devicons', }, - init = function() vim.g.barbar_auto_setup = false end, - cmd = { "BufferNext", "BufferPrevious" }, opts = { clickable = false, tabpages = false, @@ -72,5 +70,11 @@ return { icons = { filetype = { enabled = false } } }, version = '^1.0.0', -- only update when a new 1.x version is released + config = function() + vim.keymap.set("n", "L", ":BufferNext", { silent = true }) + vim.keymap.set("n", "H", ":BufferPrevious", { silent = true }) + + vim.keymap.set("n", "bd", ":BufferDelete") + end }, } diff --git a/lua/utils/functions.lua b/lua/utils/functions.lua index 4869abe..ebaa554 100644 --- a/lua/utils/functions.lua +++ b/lua/utils/functions.lua @@ -5,94 +5,4 @@ function M.Sleep(n) os.execute("sleep " .. tonumber(n)) end -function open_vorlesung() - local mapss = require("config.keymaps") - -- get globals - set_obs() - local uni_dir = vim.fn.expand("~/projects/university/" .. _G.season) - - require('telescope.builtin').find_files { - prompt_title = "Select Vorlesung in " .. _G.season, - cwd = uni_dir, - find_command = { - "eza", "-1", "-D" - }, - } -end - - - -function find_eff() - require('telescope.builtin').find_files({ - hidden = true, -- show hidden files (dotfiles) - no_ignore = true, -- respect .gitignore (ignore files listed in .gitignore) - follow = true, -- follow symlinks - disable_devicons = true, - - -- Additional filtering to exclude .git, .cache, .local, and large files - prompt_title = "Find Files - custom", - find_command = { - "rg", "--files", - "--glob", "!**/.git/*", - "--glob", "!**/.cache/*", - "--glob", "!**/.local/*", - "--glob", "!**/bigfiles/*", -- exclude large files folder - "--glob", "!**/*.{jpg,png,gif,mp4,mkv,tar,zip,iso}" -- exclude some large file types - } - }) -end - --- TODO: implement this ---function search_brain_links() --- local fzf = require('fzf') --- local cmd = "grep -oP '\\[\\[.*:' " .. vim.fn.expand('%') -- grep pattern to search for [[.*: --- fzf.fzf(cmd, { --- preview = "bat --style=numbers --color=always --line-range :500", -- preview with bat (optional) --- sink = function(selected) --- if selected and #selected > 0 then --- local line = vim.fn.search(selected[1], 'n') -- jump to the match --- if line > 0 then --- vim.api.nvim_win_set_cursor(0, {line, 0}) --- end --- end --- end --- }) ---end - - -function set_root() - local current_file = vim.fn.expand('%:p:h') -- get directory of current file - local cmd = 'git -C ' .. vim.fn.fnameescape(current_file) .. ' status' - vim.fn.system(cmd) - if vim.v.shell_error == 0 then - local git_root = vim.fn.systemlist('git -C ' .. vim.fn.fnameescape(current_file) .. ' rev-parse --show-toplevel') - [1] - vim.cmd('cd ' .. vim.fn.fnameescape(git_root)) - else - vim.cmd('cd ' .. vim.fn.fnameescape(current_file)) - end -end - --- Function to open the last file -function open_last_file() - local last_file_path = vim.fn.stdpath('data') .. "/lastfile.txt" - local file = io.open(last_file_path, "r") - if file then - local last_file = file:read("*line") - file:close() - if last_file and vim.fn.filereadable(last_file) == 1 then - vim.cmd("edit " .. last_file) - pcall(function() - vim.cmd('normal! `.') -- Go to the last edit position - vim.cmd('normal! zz') -- Center the cursor on the screen - end) - else - print("Last file does not exist or is not readable") - end - else - print("No last file found") - end -end - - return M