DEV Community

Sérgio Araújo
Sérgio Araújo

Posted on • Edited on

Primary Selection as a Plus

Why Primary Selection?

Most people I see on the Internet, nvim users, usually give up "primary selection" by setting their clipboard to "unnamedplus". Those who use Linux since its beginning like selecting some text and hitting middle mouse button to have the text pasted somewhere else.

If you can have two independent clipboards in your system why do not use it? Because in vim you can just type:

:reg *,+
Enter fullscreen mode Exit fullscreen mode

The primary selection is represented by * and the clipboard by +.

Fix primary selection paste on firefox:

On my awesomeWM I have this code (line 05 to 09):

01 clientbuttons = gears.table.join(
02   awful.button({ }, 1, function (c)
03     c:emit_signal("request::activate", "mouse_click", {raise = true})
04   end),
05   -- paste primary selection Ctrl + button1
06   awful.button({ "Control" }, 1, function (c)
07     c:emit_signal("request::activate", "mouse_click", {raise = true})
08     awful.spawn.with_shell("xdotool click --delay 0 --clearmodifiers 2")
09   end),
10   awful.button({ modkey }, 1, function (c)
11     c:emit_signal("request::activate", "mouse_click", {raise = true})
12     awful.mouse.client.move(c)
13   end),
14   awful.button({ modkey }, 3, function (c)
15     c:emit_signal("request::activate", "mouse_click", {raise = true})
16     awful.mouse.client.resize(c)
17   end)
18 )
Enter fullscreen mode Exit fullscreen mode

Even though I can not middle click on firefox to paste my primary selection I can now hold Ctrl + button1 to have it working again.

Primary Selection on neovim:

local function map(mode, lhs, rhs, opts)
    local options = { noremap = true, silent = true }
    if opts then
        if opts.desc then
            opts.desc = "init.lua: " .. opts.desc
        end
        options = vim.tbl_extend('force', options, opts)
        end
    vim.keymap.set(mode, lhs, rhs, options)
end


-- avoid clipboard hacking security issue
-- http://thejh.net/misc/website-terminal-copy-paste
-- inoremap <C-R>+ <C-r><C-o>+
map("i", "<C-r>+", "<C-r><C-o>+", { desc = 'fix terminal copy paste hack issue' })
map("i", "<S-Insert>", "<C-r><C-o>*", { desc = 'fix terminal copy paste hack issue' })

-- copy to the primary selection on mouse release
map("v", "<LeftRelease>", '"*y' , {silent = true, desc = "Copy selection to primary selection"})
Enter fullscreen mode Exit fullscreen mode

Paste primary selection on bspwm

# %%hotkey: paste primary selection %%
ctrl + alt + ~button1
    xdotool click --delay 0 --clearmodifiers 2

# %%hotkey: paste primary selection using keyboard %%
shift + insert
    xdotool click --delay 0 --clearmodifiers 2
Enter fullscreen mode Exit fullscreen mode

Top comments (0)