开发者

how do I cancel visual mode programmatically in vim

I'm running a bit of code in Vim and having a problem (I think) with not being in the correct mode. I'm trying to get the text between either '< >' or '[ ]' into a register. At point code starts below I know the cursor is between a pair of brackets, but I don't know what kind the brackets are:

    " put text in x reg if I'm in <> brackets
    normal! vi<"xy
    " put in x reg if I'm in [] brackets
    if len(@x) < 7 
           normal! vi["xy
    endif
   [. .. . more code follows making use of
           value in x register]

If I'm clicking between <> brackets the rest of the code works okay, alhough I may not be in normal mode. If I'm clicking between the [] brackets then it doesn't work at all, and the literal text ["xy gets inserted at cursor position.

I've tried a couple different workarounds, e.g., trying to put into the keystrings (or a separate normal command) or using 'visual!' command after each visual part. Can't get things going with and the Visual! command avoids the character inserts on second part but but also seems to screw up getting the visual match into the x register.

I'm a little confused and not sure what mode I'm in ex开发者_开发百科actly at all parts of script: normal, visual, or insert. I run through things manually and it seems to work fine but if I do it programmatically it behaves unexpectedly.

Is there some simple thing I'm missing? I could do what I want another way, but this method (at first look) seemed like easiest and most straightforward. Is it?

Thanks for any help.


Okay, I figured out the problem. The mode was left in visual mode when first inner bracket command found no match. Needed to have an extra 'v' to bring back to normal mode before pressing the keys again. Like this:

    " put text in x reg if I'm in <> brackets
    normal! vi<"xy
    " put in x reg if I'm in [] brackets
    if len(@x) < 7 
           "  EXTRA V IN LINE BELOW
           normal! vvi["xy
    endif
   [. .. . more code follows making use of
           value in x register]
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜