开发者

Add support for `ci|` and `da|` (seleting text inside pipes)

I often use commands like ci( and di{ when editing source code.

开发者_高级运维

Parameters in Ruby blocks are contained inside pipe characters, like |a, b|

Is it possible to extend this behavior to add support for |, so that commands like ci|, da| and yi| work properly?


I have the following in my vimrc (I have added the va| and vi| commands for completeness):

nnoremap di\| T\|d,
nnoremap da\| F\|d,
nnoremap ci\| T\|c,
nnoremap ca\| F\|c,
nnoremap yi\| T\|y,
nnoremap ya\| F\|y,
nnoremap vi\| T\|v,
nnoremap va\| F\|v,

The , operator repeats the previous F,f,T or t but in the opposite direction. A very useful key!

These mappings can be easily modified to support other delimiters; I use the $ versions all the time when editing LaTeX.


This is what I would use:

vnoremap <silent> a<bar> :<c-u>silent! normal! vF<bar>of<bar><cr>
vnoremap <silent> i<bar> :<c-u>silent! normal! vT<bar>ot<bar><cr>
onoremap <silent> a<bar> :normal va<bar><cr>
onoremap <silent> i<bar> :normal vi<bar><cr>

Basically setup a operator pending mode (that is the onoremap) which will call the appropriate visual mode mapping. The visual mode mappings will search backwards to find a | with F| then go to the other side of the visual selection via the o command, then search forwards with f| to select the other end of the piped area. The inner mappings are the same but instead of using the F and f commands you use T and t.

Sadly these mappings do not work correctly with the ., redo command as they inherently rely visual mode mappings which means the . command will execute the same command again but only for an area that take up the same amount of space.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜