Note that there are some explanatory texts on larger screens.

plurals
  1. POAllowing custom motion in a vim map?
    text
    copied!<p>I have the following mapping that allows to paste over a word from the yank buffer. (cpw = change paste word)</p> <p><code>nmap &lt;silent&gt; cpw "_cw&lt;C-R&gt;"&lt;Esc&gt;</code></p> <p>What I would like to do is allow commands such as the following</p> <p>cpiw (change paste in word -> like the 'iw' motion)</p> <p>cpaw (change paste a word -> like the 'aw' motion) </p> <p>for any motion {m} cp{m}</p> <p>is this possible to allow in a mapping, so I don't have to write the nmap for each motion that I want to work with it?</p> <p>Thanks in advance.</p> <p>EDIT: typo fixes. My solution below</p> <p>After diligently looking into the map-operator I was successful in making a function that did exactly as I wanted. For any who are interested it is as follows: </p> <pre><code>"This allows for change paste motion cp{motion} nmap &lt;silent&gt; cp :set opfunc=ChangePaste&lt;CR&gt;g@ function! ChangePaste(type, ...) silent exe "normal! `[v`]\"_c" silent exe "normal! p" endfunction </code></pre> <p>EDIT - new version that might be better.</p> <pre><code>"This allows for change paste motion cp{motion} nmap &lt;silent&gt; cp :set opfunc=ChangePaste&lt;CR&gt;g@ function! ChangePaste(type, ...) if a:0 " Invoked from Visual mode, use '&lt; and '&gt; marks. silent exe "normal! `&lt;" . a:type . "`&gt;\"_c" . @" elseif a:type == 'line' silent exe "normal! '[V']\"_c" . @" elseif a:type == 'block' silent exe "normal! `[\&lt;C-V&gt;`]\"_c" . @" else silent exe "normal! `[v`]\"_c" . @" endif endfunction </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload