Skip to content

About

Neovim plugin that allows pasting a visual selection at current cursor position

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Latest commit

 

History

7 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 

Repository files navigation

insert-visual-paste.nvim

insert-visual-paste.nvim is a small Neovim plugin for copying a visually selected piece of text and pasting it back at the cursor position where you originally stopped editing.

The main workflow is:

  1. You are in insert mode
  2. You press a key (default v) to leave insert mode
  3. You move elsewhere and visually select some text
  4. You yank it with y
  5. The plugin jumps back to your original cursor position
  6. It pastes the yanked text
  7. It enters insert mode again

This is useful when you are typing something and realize you want to reuse a word, phrase, or small piece of text from somewhere else in the buffer.


Features

  • Leaves insert mode and remembers your original cursor position
  • Waits for the next visual-mode yank
  • Copies the yanked text into a configurable register, default z
  • Jumps back to the original position
  • Pastes the selected text there
  • Returns to insert mode automatically
  • Optional mode to enter visual mode immediately
  • Configurable mapping and register
  • Small and dependency-free

Requirements

  • Neovim 0.8 or newer.

How it works

Starting in insert mode:

The quick brown fox jumps over the lazy |dog.

Press the plugin mapping, for example:

<LocalLeader>v

Neovim leaves insert mode and remembers that cursor position.

Then move elsewhere, visually select some text:

fox

Press:

y

The plugin will paste fox back at the original cursor position and re-enter insert mode.


Installation

lazy.nvim

Using your GitHub repository:

{
  "gpappasunb/insert-visual-paste.nvim",
  config = function()
    require("insert_visual_paste").setup({
      mapping = "<LocalLeader>v",
      register = "z",
      start_visual = false,
      enter_insert = true,
    })
  end,
}

Suggested key mappings

Add these lines to your lua configuration file:

vim.keymap.set({ 'n', 'i' }, '<LocalLeader>yj', '<cmd>InsertVisualPasteFromBelow<CR>', {
  silent = true,
  desc = 'Paste rest of line below at cursor',
})

vim.keymap.set({ 'n', 'i' }, '<LocalLeader>yk', '<cmd>InsertVisualPasteFromAbove<CR>', {
  silent = true,
  desc = 'Paste rest of line above at cursor',
})

Note about <LocalLeader>

The default mapping uses <LocalLeader>.

If you have not configured maplocalleader, Neovim often uses \ as the default.

You may want to set it explicitly near the top of your init.lua, before loading plugins:

vim.g.maplocalleader = ","

Then the default plugin mapping becomes:

,v

Setup

require("insert_visual_paste").setup()

This uses the default configuration.

For custom behavior, pass a table:

require("insert_visual_paste").setup({
  mapping = "<LocalLeader>v",
  modes = { "i" },
  register = "z",
  start_visual = false,
  enter_insert = true,
  create_commands = true,
  silent = true,
  buffer = false,
  desc = "Leave insert mode; next visual yank is pasted at original cursor position",
})

Configuration options

mapping

Type: string | false

Default:

mapping = "<LocalLeader>v"

The key used to start the plugin.

Example:

require("insert_visual_paste").setup({
  mapping = "<LocalLeader>p",
})

Disable the default mapping:

require("insert_visual_paste").setup({
  mapping = false,
})

modes

Type: string[]

Default:

modes = { "i" }

Modes where the mapping should be created.

Usually you only need insert mode:

modes = { "i" }

register

Type: string

Default:

register = "z"

The register used to temporarily store the visually yanked text.

Example:

require("insert_visual_paste").setup({
  register = "q",
})

Note: this plugin copies the visually yanked text into this register. The normal yank operation still happens, so the unnamed register may also be affected depending on your Neovim configuration.


start_visual

Type: boolean

Default:

start_visual = false

If false, pressing the mapping leaves insert mode and puts you in normal mode.

If true, pressing the mapping leaves insert mode and immediately enters visual mode.

Example:

require("insert_visual_paste").setup({
  start_visual = true,
})

enter_insert

Type: boolean

Default:

enter_insert = true

If true, Neovim enters insert mode after pasting.

If false, Neovim stays in normal mode after pasting.

Example:

require("insert_visual_paste").setup({
  enter_insert = false,
})

create_commands

Type: boolean

Default:

create_commands = true

Creates user commands:

:InsertVisualPasteStart
:InsertVisualPasteCancel

To disable them:

require("insert_visual_paste").setup({
  create_commands = false,
})

silent

Type: boolean

Default:

silent = true

Whether the created mapping should be silent.

Example:

require("insert_visual_paste").setup({
  silent = false,
})

buffer

Type: boolean

Default:

buffer = false

If true, the mapping is created for the current buffer only.

Example:

require("insert_visual_paste").setup({
  buffer = true,
})

Usually you want the global mapping, so leave this as false.


desc

Type: string

Default:

desc = "Leave insert mode; next visual yank is pasted at original cursor position"

The description shown by plugins like which-key.nvim.

Example:

require("insert_visual_paste").setup({
  desc = "Paste next visual yank at previous insert position",
})

Usage

Start from insert mode

In insert mode, press your configured mapping:

<LocalLeader>v

Then:

  1. Move to the text you want to copy.
  2. Visually select it using v, V, or Ctrl-v.
  3. Yank it with y.

The plugin will:

  1. Copy the yanked text into the configured register.
  2. Jump back to the original cursor position.
  3. Paste the text.
  4. Enter insert mode.

Start manually from normal mode

You can also start the behavior manually:

:InsertVisualPasteStart

If you run this from normal mode, the plugin saves the current cursor position as the target position.

Then visually select some text and yank it.


Commands

If create_commands is enabled, the following commands are available.

:InsertVisualPasteStart

Starts the plugin manually.

Useful if you disabled the mapping or want to trigger the behavior from normal mode.

:InsertVisualPasteStart

:InsertVisualPasteCancel

Cancels the current waiting state.

:InsertVisualPasteCancel

You can also cancel by entering insert mode again before yanking.


Example configurations

Minimal setup

require("insert_visual_paste").setup()

Enter visual mode immediately

require("insert_visual_paste").setup({
  start_visual = true,
})

Stay in normal mode after pasting

require("insert_visual_paste").setup({
  enter_insert = false,
})

Use a different register

require("insert_visual_paste").setup({
  register = "q",
})

Disable the mapping and use commands only

require("insert_visual_paste").setup({
  mapping = false,
})

Then use:

:InsertVisualPasteStart

Troubleshooting

The mapping does nothing

Check that maplocalleader is set if you are using <LocalLeader>:

vim.g.maplocalleader = ","

Make sure this line runs before the plugin is configured.

You can also use a normal mapping instead:

require("insert_visual_paste").setup({
  mapping = "<leader>vp",
})

The text is not pasted

Make sure you are yanking the visual selection with:

y

This plugin reacts to a visual-mode yank. It does not trigger on visual-mode delete or change.


The plugin pastes from the wrong register

Check the configured register:

require("insert_visual_paste").setup({
  register = "z",
})

Make sure you are not overriding register z elsewhere.


License

MIT

About

Neovim plugin that allows pasting a visual selection at current cursor position

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages