Skip to content
This repository has been archived by the owner on Sep 18, 2023. It is now read-only.

Commit

Permalink
feat: respect v:count in line-wise mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
smjonas committed Jan 22, 2023
1 parent d46c892 commit a3691db
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.

Expand Down
9 changes: 6 additions & 3 deletions lua/duplicate/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a3691db

Please sign in to comment.