keymaps.lua

~/.config/nvim/lua/config/keymaps.lua
-- https://github.com/ThePrimeagen/init.lua/blob/master/lua/theprimeagen/remap.lua
-- When you paste over selected text, it won't overwrite your paste register
-- The deleted text is discarded instead of replacing what's in your paste buffer
-- Allows you to paste the same content multiple times over different selections
vim.keymap.set("x", "<leader>p", '"_dP')

vim.keymap.set("v", "J", ":m '>+1<CR>gv=gv")
vim.keymap.set("v", "K", ":m '<-2<CR>gv=gv")

-- C-c to copy to system clipboard
vim.keymap.set("v", "<C-c>", '"+y')
-- [[ Highlight on yank ]]
-- See `:help vim.highlight.on_yank()`
local highlight_group = vim.api.nvim_create_augroup("YankHighlight", { clear = true })
vim.api.nvim_create_autocmd("TextYankPost", {
  callback = function()
    vim.highlight.on_yank()
  end,
  group = highlight_group,
  pattern = "*",
})

-- Copy the path to current file so I can cd into it from another terminal
vim.keymap.set("n", "<leader>cd", function()
  local cwd = vim.fn.expand("%:p:h")
  vim.fn.setreg("+", cwd)
  print("Copied " .. cwd .. " to clipboard")
end, { desc = "Copy the directory containing file" })

-- H,L to switch tabs in normal mode
vim.keymap.set("n", "H", vim.cmd.tabp, { desc = "Switch to previous tab" })
vim.keymap.set("n", "L", vim.cmd.tabn, { desc = "Switch to next tab" })

-- Disables all automatic fold opening
vim.opt.foldopen = ""
vim.opt.foldmethod = "manual"

local fold_node = function()
  local ts_utils = require("nvim-treesitter.ts_utils")
  local cn = ts_utils.get_node_at_cursor(0)
  while cn ~= nil do
    local node_start_row, _, node_end_row, _ = cn:range()
    if node_end_row == node_start_row then
      cn = cn:parent()
    else
      vim.cmd(node_start_row + 1 .. "," .. node_end_row + 1 .. "fold")
      break
    end
  end
end

vim.opt.fillchars = { eob = "-", fold = " " }
vim.opt.foldtext = "v:lua.MyFoldText()"
function MyFoldText()
  local start_line = vim.fn.getline(vim.v.foldstart)
  local end_line = vim.fn.getline(vim.v.foldend)
  end_line = end_line:gsub("^%s+", "")
  return start_line .. " ... " .. end_line
end

vim.keymap.set("n", "<C-f>", function()
  local fold_level = vim.fn.foldlevel(".")
  local fold_closed = vim.fn.foldclosed(".")

  if fold_level > 0 then
    if fold_closed == -1 then
      -- fold_named_arg_func()
      fold_node()
    else
      vim.cmd("foldopen")
    end
  else
    -- fold_named_arg_func()
    fold_node()
  end
end)

vim.keymap.set("n", "<C-h>", "<C-w><")
vim.keymap.set("n", "<C-j>", "<C-w>-")
vim.keymap.set("n", "<C-k>", "<C-w>+")
vim.keymap.set("n", "<C-l>", "<C-w>>")

-- Todo: Make this better.
vim.keymap.set({ "i" }, "<C-p>", function()
  local dirpath = vim.fn.expand("%:p:h") .. "/img"
  local imgname = os.time() .. ".png"

  -- A shell script to paste image from clipboard into subdir img
  local shell_script = string.format(
    [[
  DIRPATH=%s
  IMGFNAME=%s
  if ! [ -d $DIRPATH ]
  then
    mkdir $DIRPATH
  fi
  wl-paste -t image/png > $DIRPATH/$IMGFNAME
  ]],
    dirpath,
    imgname
  )
  vim.fn.system(shell_script)

  -- All this is just to insert markdown image link
  local row, col = unpack(vim.api.nvim_win_get_cursor(0))
  local lines = vim.api.nvim_buf_get_lines(0, row - 1, row, false)
  local markdown_link = string.format("![%s](img/%s)", imgname, imgname)
  -- Check if the 'lines' table is empty
  local line = ""
  if #lines ~= 0 then
    line = lines[1]:gsub("\n", " ")
  end
  local new_line = line .. markdown_link
  vim.api.nvim_buf_set_lines(0, row - 1, row, false, { new_line })
  vim.api.nvim_win_set_cursor(0, { row, col + #markdown_link })
end, { desc = "Paste screenshot" })

vim.keymap.set('n', '<C-n>', function()
  if vim.api.nvim_get_option_value('filetype', { buf = 0 }) ~= "oil"
  then
    -- Get the current window
    local current_win = vim.api.nvim_get_current_win()

    -- Get the window to the left of the current window
    local cur_win_nr = vim.fn.winnr()
    local left_win_nr = vim.fn.winnr('h')
    local left_win_id = vim.fn.win_getid(left_win_nr)

    -- Check if there is a window to the left
    if left_win_nr ~= cur_win_nr then
      -- Get the buffer of the left window
      local left_buf = vim.api.nvim_win_get_buf(left_win_id)

      -- Get the file type of the buffer
      local filetype = vim.api.nvim_get_option_value('filetype', { buf = left_buf })

      -- Check if the file type is 'oil'
      if filetype == 'oil' then
        -- Close
        vim.cmd(left_win_nr .. 'hide')
      else
        vim.cmd('30vsplit')
        vim.cmd('Oil')
        vim.api.nvim_command("set winfixwidth")  -- Lock the width
        vim.api.nvim_command("set winfixheight") -- Lock the height

        --- Open
      end
    else
      vim.cmd('30vsplit')
      vim.cmd('Oil')
      vim.api.nvim_command("set winfixwidth")  -- Lock the width
      vim.api.nvim_command("set winfixheight") -- Lock the height
      -- Open
    end
  else
    vim.cmd("hide")
  end
end)

vim.keymap.set('n', "<Leader><CR>", function()
  vim.cmd('vsplit')
  vim.fn.feedkeys('<CR>')
end, { desc = "Enter after a vsplit" })

Comments