Created
April 7, 2023 10:10
-
-
Save Piotr1215/51b5fd6e6cdea3fd96baf4abea632fcb to your computer and use it in GitHub Desktop.
Quickly copy search results in neovim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function _G.yank_matching_lines() | |
local search_pattern = vim.fn.getreg('/') | |
if search_pattern ~= '' then | |
local matching_lines = {} | |
for line_number = 1, vim.fn.line('$') do | |
local line = vim.fn.getline(line_number) | |
if vim.fn.match(line, search_pattern) ~= -1 then | |
table.insert(matching_lines, line) | |
end | |
end | |
if #matching_lines > 0 then | |
local original_filetype = vim.bo.filetype | |
vim.fn.setreg('+', table.concat(matching_lines, '\n')) | |
vim.cmd('new') | |
vim.cmd('0put +') | |
vim.bo.filetype = original_filetype | |
else | |
print("No matches found") | |
end | |
end | |
end | |
vim.api.nvim_set_keymap('n', '<Leader>ya', ':lua _G.yank_matching_lines()<CR>', { noremap = true, silent = true }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment