From a3691db9e58b8f1c4b0b9ed76098246299dee913 Mon Sep 17 00:00:00 2001 From: smjonas Date: Sun, 22 Jan 2023 16:30:16 +0100 Subject: [PATCH] feat: respect v:count in line-wise mapping --- README.md | 8 ++++++-- lua/duplicate/init.lua | 9 ++++++--- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index ccc0070..d39fe0b 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,10 @@ # duplicate.nvim -A tiny Neovim plugin used to duplicate a textobject (works in both normal and visual mode). -E.g. use `ydip` to duplicate the current paragraph or `ydaw` to duplicate the current word (including whitespace). +A Neovim plugin used to duplicate a textobject. +E.g. use `ydaw` to duplicate the current word (including whitespace) or `3ydd` to duplicate the next three lines. + +## Features +- mappings for duplication in normal mode (line-wise or using textobject) + visual mode +- duplication in normal mode respects `v:count` and is dot-repeatable Requires Neovim ≥ 0.6. diff --git a/lua/duplicate/init.lua b/lua/duplicate/init.lua index 650c73e..3649125 100644 --- a/lua/duplicate/init.lua +++ b/lua/duplicate/init.lua @@ -37,11 +37,14 @@ end local duplicate_cur_line = function() local line_nr, _ = unpack(vim.api.nvim_win_get_cursor(0)) - local line = vim.api.nvim_get_current_line() + -- Respect v:count + local line_count = math.max(1, vim.v.count) + local lines = vim.api.nvim_buf_get_lines(0, line_nr - 1, line_nr + line_count - 1, false) if config.transform then - line = config.transform({ line })[1] + lines = config.transform(lines) end - vim.api.nvim_buf_set_lines(0, line_nr, line_nr, false, { line }) + local last_line = line_nr + line_count - 1 + vim.api.nvim_buf_set_lines(0, last_line, last_line, false, lines) end -- Line indices are 1-based, columns are 0-based