changeset 724:e442bbd36b87

feat: copy current file path relative to repo root and line nr to clipboard
author zegervdv <zegervdv@me.com>
date Fri, 09 Dec 2022 17:59:37 +0100
parents bbcf4f5b005c
children 624d8036f5ae
files .chezmoitemplates/init.lua
diffstat 1 files changed, 12 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/.chezmoitemplates/init.lua	Tue Dec 06 21:20:31 2022 +0100
+++ b/.chezmoitemplates/init.lua	Fri Dec 09 17:59:37 2022 +0100
@@ -1093,6 +1093,18 @@
   if ls.choice_active() then ls.change_choice() end
 end)
 
+-- Copy the current file and line number
+-- inspired by https://github.com/diegoulloao/nvim-file-location
+vim.keymap.set('n', '<leader>cp', function()
+  local Path = require 'plenary.path'
+  local current_file = Path:new(vim.fn.expand '%')
+  local root =
+    vim.fs.dirname(vim.fs.find({ '.hg', '.git' }, { path = tostring(current_file:parent()), upward = true })[1])
+  local current_line = vim.fn.line '.'
+  require('osc52').copy(current_file:make_relative(root) .. ':' .. current_line)
+  vim.notify 'Copied file path and line number'
+end, { desc = 'Yank current path and line number' })
+
 -- LSP config
 local lsp = require 'lspconfig'
 local null_ls = require 'null-ls'