Search code examples
vimvim-plugin

How to bind key-mapping for saving selected text to file in vim?


In vim, I select a text in visual mode, and type :w! ~/saved.txt, actually vim will execute :'<,'>w! ~/saved.txt. like this:

enter image description here

The command will save the text into ~/saved.txt. In another vim instance, I can use :r ~/saved.txt to read from ~/saved.txt to the other vim.

This implement a global copy-paste on the same machine. I want to make this a vim-command/key-mapping, while I don't know how to implement it.


Solution

  • A simple key-mapping:

    vnoremap ,w :w! ~/saved.txt<cr>
    nnoremap ,r :r ~/saved.txt<cr>
    

    Replace ,w, ,r with your favour.