From 3555e9dc0afc349fb9061117703ef59dc77a538d Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Sun, 10 Nov 2024 23:53:36 +0100 Subject: [PATCH] CD to git root user cmd --- lua/autocmd.lua | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lua/autocmd.lua b/lua/autocmd.lua index ef6d522..58256e3 100644 --- a/lua/autocmd.lua +++ b/lua/autocmd.lua @@ -25,4 +25,17 @@ vim.api.nvim_create_autocmd('TextYankPost', { end, }) +vim.api.nvim_create_user_command('CDToGitRoot', function() + local handle = io.popen 'git rev-parse --show-toplevel 2>/dev/null' + local git_root = handle:read('*a'):gsub('\n', '') + handle:close() + + if git_root == '' then + print 'Not in a Git repository' + else + vim.cmd('cd ' .. git_root) + print('Changed directory to Git root: ' .. git_root) + end +end, { desc = 'Change directory to the current Git repository root' }) + -- vim: ts=2 sts=2 sw=2 et