How I use Vim to write everywhere, sort of

So I was explaining how I use Vim to write anywhere and it raised some interest with people asking for the config on reddit so I figured I’d make a short post about it.

Here’s the humble trick:

I’ve mapped a key combination to open vim in insert mode and another to yank everything and exit, then I just paste it wherever I want.

That’s the most suckless thing I was able to come up with, as it does not depend on anything, will never break, and you can use it everywhere. You also get all the perks of your local, full fledged Vim install such as autocomplete.

It still has some overhead though as it requires some more key presses than just typing the message, so I really only use it for emails or longer messages.

The receip

I first added a custom shortcut in my system settings which runs the following command when I press meta+v: (alacritty is the terminal I use)

alacritty -t "Scratchpad" -e vim -c "startinsert"

And in my vimrc

" yank everything
nmap <leader>ya gg0vG$"+y
" yank everything and force quit
nmap <leader>yq gg0vG$"+y:q!<CR>

I use 0vG$ and not VG because the latter adds an unwanted linebreak at the end.

Bonus: temporary backup

Actually, you really want to use this line instead so that you don’t lose the precious time you spent typing your email on a bad manipulation. You may change the path to whatever you like.

nmap <leader>yq :w! /tmp/last_scratchpad.txt<CR>gg0vG$"+y:q!<CR>

Common issue and the fix

For some people exiting vim cleared their clipboard. Adding the following line

autocmd VimLeave * call system("xsel -ib", getreg('+'))

to your vimrc as described here can fix it.


Vimtips

281 Words

2020-07-04 11:15 +0200