Skip to content

Commit 0e5d03c

Browse files
committed
Add visual paste
Closes #128
1 parent 709c97f commit 0e5d03c

File tree

8 files changed

+47
-7
lines changed

8 files changed

+47
-7
lines changed

README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ require('neoclip').setup({
156156
select = '<cr>',
157157
paste = '<c-p>',
158158
paste_behind = '<c-k>',
159+
paste_visual = '<c-v>',
159160
replay = '<c-q>', -- replay a macro
160161
delete = '<c-d>', -- delete an entry
161162
edit = '<c-e>', -- edit an entry
@@ -167,6 +168,7 @@ require('neoclip').setup({
167168
--- It is possible to map to more than one key.
168169
-- paste = { 'p', '<c-p>' },
169170
paste_behind = 'P',
171+
paste_visual = 'v',
170172
replay = 'q',
171173
delete = 'd',
172174
edit = 'e',
@@ -177,6 +179,7 @@ require('neoclip').setup({
177179
select = 'default',
178180
paste = 'ctrl-p',
179181
paste_behind = 'ctrl-k',
182+
paste_visual = 'ctrl-v',
180183
custom = {},
181184
},
182185
},
@@ -332,6 +335,16 @@ if using `telescope` or
332335
```
333336
if using `fzf-lua`.
334337

338+
### Visual mode
339+
340+
If you want to select some text and replace it with a Neoclip selection, you need the following keymap:
341+
342+
```vim
343+
<ESC>:Telescope neoclip<CR>
344+
```
345+
346+
The `<ESC>` is necessary to "save" the visual selection before opening a foating buffer. If not, the visual selection is lost. If we don't do this, there will be an error `E481: No range allowed` because Telescope does not support ranges (it seems). If the mapping were `<CMD>Telescope neoclip<CR>`, the visual range would be dropped before opening Telescope without being saved so the visual paste later on would be wrong.
347+
335348
### Macros
336349
If `enable_macro_history` is set to `true` (default) in the [`setup`](#configuration) then any recorded macro will be stored and can later be accessed using:
337350
```vim
@@ -381,7 +394,7 @@ You can edit the contents of an entry using the keybinds for `edit`. It'll open
381394
end
382395
return true
383396
end
384-
397+
385398
require('neoclip').setup{
386399
...
387400
filter = function(data)

doc/neoclip.txt

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ The following are the defaults and the keys are explained below:
140140
select = '<cr>',
141141
paste = '<c-p>',
142142
paste_behind = '<c-k>',
143+
paste_visual = '<c-v>',
143144
replay = '<c-q>', -- replay a macro
144145
delete = '<c-d>', -- delete an entry
145146
edit = '<c-e>', -- edit an entry
@@ -151,6 +152,7 @@ The following are the defaults and the keys are explained below:
151152
--- It is possible to map to more than one key.
152153
-- paste = { 'p', '<c-p>' },
153154
paste_behind = 'P',
155+
paste_visual = 'v',
154156
replay = 'q',
155157
delete = 'd',
156158
edit = 'e',
@@ -161,6 +163,7 @@ The following are the defaults and the keys are explained below:
161163
select = 'default',
162164
paste = 'ctrl-p',
163165
paste_behind = 'ctrl-k',
166+
paste_visual = 'ctrl-v',
164167
custom = {},
165168
},
166169
},
@@ -424,7 +427,7 @@ TIPS *neoclip-nvim-neoclip.lua-tips*
424427
local function is_whitespace(line)
425428
return vim.fn.match(line, [[^\s*$]]) ~= -1
426429
end
427-
430+
428431
local function all(tbl, check)
429432
for _, entry in ipairs(tbl) do
430433
if not check(entry) then
@@ -433,7 +436,7 @@ TIPS *neoclip-nvim-neoclip.lua-tips*
433436
end
434437
return true
435438
end
436-
439+
437440
require('neoclip').setup{
438441
...
439442
filter = function(data)
@@ -484,10 +487,10 @@ PREVIEW = FALSE AND CONTENT_SPEC_COLUMN = FALSE ~
484487
2. Links *neoclip-links*
485488

486489
1. *neoclip*: https://user-images.githubusercontent.com/23341710/140090515-83a08f0f-85f9-4278-bcbe-48e4d8442ace.png
487-
2. *@cdown*:
488-
3. *@fdschmidt93*:
489-
4. *@ibhagwan*:
490-
5. *@kkharji*:
490+
2. *@cdown*:
491+
3. *@fdschmidt93*:
492+
4. *@ibhagwan*:
493+
5. *@kkharji*:
491494
6. *preview*: https://user-images.githubusercontent.com/23341710/140090515-83a08f0f-85f9-4278-bcbe-48e4d8442ace.png
492495
7. *content_spec_column*: https://user-images.githubusercontent.com/23341710/140090472-3271affa-7efd-40bd-9d20-562b2074b261.png
493496
8. *clean*: https://user-images.githubusercontent.com/23341710/140090327-30bfff28-83ff-4695-82b8-8d4abfd68546.png

lua/neoclip/fzf.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ local function make_actions(register_names)
5959
[keys.select] = get_set_register_handler(register_names),
6060
[keys.paste] = get_paste_handler(register_names, 'p'),
6161
[keys.paste_behind] = get_paste_handler(register_names, 'P'),
62+
[keys.paste_visual] = get_paste_handler(register_names, 'v'),
6263
}
6364
if keys.custom ~= nil then
6465
for key, action in pairs(keys.custom) do

lua/neoclip/handlers.lua

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,24 @@ end
9191

9292
-- TODO can this be done without setting the register?
9393
M.paste = function(entry, op)
94+
if op == "v" then
95+
return M.paste_visual(entry)
96+
end
97+
9498
temporary_reg_usage(entry, function(register_name)
9599
vim.cmd(string.format('normal! "%s%s', register_name, op))
96100
end)
97101
end
98102

103+
M.paste_visual = function(entry)
104+
temporary_reg_usage(entry, function(register_name)
105+
-- `gv` is needed to reselect the last visual selection.
106+
-- NOTE: This only works if the last visual selection was saved by
107+
-- returing back to normal mode before opening neoclip.
108+
vim.api.nvim_feedkeys('gv"'..register_name..'p', "n", false)
109+
end)
110+
end
111+
99112
-- TODO can this be done without setting the register?
100113
M.replay = function(entry)
101114
temporary_reg_usage(entry, function(register_name)

lua/neoclip/settings.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ local settings = {
3636
select = '<cr>',
3737
paste = '<c-p>',
3838
paste_behind = '<c-k>',
39+
paste_visual = '<c-v>',
3940
replay = '<c-q>',
4041
delete = '<c-d>',
4142
edit = '<c-e>',
@@ -45,6 +46,7 @@ local settings = {
4546
select = '<cr>',
4647
paste = 'p',
4748
paste_behind = 'P',
49+
paste_visual = 'v',
4850
replay = 'q',
4951
delete = 'd',
5052
edit = 'e',
@@ -55,6 +57,7 @@ local settings = {
5557
select = 'default',
5658
paste = 'ctrl-p',
5759
paste_behind = 'ctrl-k',
60+
paste_visual = 'ctrl-v',
5861
custom = {},
5962
},
6063
},

lua/neoclip/telescope.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@ local function get_export(register_names, typ)
299299
map_if_set(map, mode, keys.select, 'select', get_set_register_handler(register_names, typ))
300300
map_if_set(map, mode, keys.paste, 'paste', get_paste_handler(register_names, typ, 'p', current_buffer))
301301
map_if_set(map, mode, keys.paste_behind, 'paste_behind', get_paste_handler(register_names, typ, 'P', current_buffer))
302+
map_if_set(map, mode, keys.paste_visual, 'paste_visual', get_paste_handler(register_names, typ, 'v', current_buffer))
302303
map_if_set(map, mode, keys.replay, 'replay', get_replay_recording_handler(register_names, typ, current_buffer))
303304
map_if_set(map, mode, keys.delete, 'delete', get_delete_handler(typ))
304305
map_if_set(map, mode, keys.edit, 'edit', get_edit_handler(typ, opts))

setup.lua.example

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ return {
2121
select = "<cr>",
2222
paste = "<c-p>",
2323
paste_behind = "<c-k>",
24+
paste_visual = "<c-v>",
2425
replay = "<c-q>", -- replay a macro
2526
delete = "<c-d>", -- delete an entry
2627
edit = "<c-e>", -- edit an entry
@@ -32,6 +33,7 @@ return {
3233
--- It is possible to map to more than one key.
3334
-- paste = { 'p', '<c-p>' },
3435
paste_behind = "P",
36+
paste_visual = "v",
3537
replay = "q",
3638
delete = "d",
3739
edit = "e",
@@ -42,6 +44,7 @@ return {
4244
select = "default",
4345
paste = "ctrl-p",
4446
paste_behind = "ctrl-k",
47+
paste_visual = "ctrl-v",
4548
custom = {},
4649
},
4750
},

tests/plenary/neoclip_spec.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,7 @@ some line]],
600600
select = '<c-a>',
601601
paste = '<c-b>',
602602
paste_behind = '<c-c>',
603+
paste_visual = '<c-v>',
603604
replay = '<c-d>',
604605
delete = '<c-e>',
605606
edit = '<c-e>',
@@ -613,6 +614,7 @@ some line]],
613614
select = 'a',
614615
paste = 'b',
615616
paste_behind = 'c',
617+
paste_visual = 'v',
616618
replay = 'd',
617619
delete = 'e',
618620
edit = 'e',
@@ -627,6 +629,7 @@ some line]],
627629
select = '<c-a>',
628630
paste = '<c-b>',
629631
paste_behind = '<c-c>',
632+
paste_visual = '<c-v>',
630633
custom = {
631634
['<c-e>'] = function(opts)
632635
return opts

0 commit comments

Comments
 (0)