Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The title of the question might cause misunderstanding. What you want to do is to define a new operator like <code>y</code>, <code>d</code> and <code>c</code>, neither motions nor text objects, isn't it? <a href="http://vimdoc.sourceforge.net/htmldoc/map.html#:map-operator" rel="noreferrer"><code>:help :map-operator</code></a> describes how to define a new operator. To take a parameter like the surround plugin, use <code>getchar()</code> in your <code>'operatorfunc'</code>.</p> <p>Though <code>:help :map-operator</code> describes the basics, it's a bit troublesome to deal with arguments passed to <code>'operatorfunc'</code>. You can use <a href="https://github.com/kana/vim-operator-user" rel="noreferrer">vim-operator-user</a> to simplify the handling of arguments. With this plugin, surround-like operator can be written as follows:</p> <pre><code>function! OperatorSurround(motion_wise) let _c = getchar() let c = type(_c) == type(0) ? nr2char(_c) : _c if c ==# "\&lt;Esc&gt;" || c == "\&lt;C-c&gt;" return endif let bp = getpos("'[") let ep = getpos("']") if a:motion_wise ==# 'char' call setpos('.', ep) execute "normal! \"=c\&lt;Return&gt;p" call setpos('.', bp) execute "normal! \"=c\&lt;Return&gt;P" elseif a:motion_wise ==# 'line' let indent = matchstr(getline('.'), '^\s*') call append(ep[1], indent . c) call append(bp[1] - 1, indent . c) elseif a:motion_wise ==# 'block' execute bp[1].','.ep[1].'substitute/\%'.ep[2].'c.\zs/\=c/' execute bp[1].','.ep[1].'substitute/\%'.bp[2].'c\zs/\=c/' call setpos('.', bp) else endif endfunction call operator#user#define('surround', 'OperatorSurround') map s &lt;Plug&gt;(operator-surround) </code></pre> <p>If you really want to define your own text objects, please consider <a href="https://github.com/kana/vim-textobj-user" rel="noreferrer">vim-textobj-user</a>.</p>
 

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