1

I make a lot of use of till and forward when I'm moving around. When I'm creating ASCII diagrams I find myself wanting a similar version for up and down.

Using this usenet logical map featured on wikipedia as an example,

----------------------------------------------------------------------


  UUCP/USENET Logical Map  -   June 1, 1981 / mods by S. McGeady 11/19/81

            (ucbvax)
+=+===================================+==+
| |                                   |  |
| |                wivax              |  |
| |                  |                |  |
| |         microsoft| uiucdcs        |  |
| |  genradbo      | | |  |           |  |           (Tektronix)
| |     |          | | |  | purdue    |  |
| decvax+===+=+====+=+=+  | |         |  |
|       |   | |      |    | | pur-phy |  |                        tekmdp
|       |   | |      |    | |     |   |  |                           |
+@@@@@@cca  | |      |    | |     |   |  |                           |
|       |   | |  +=pur-ee=+=+=====+===+  |                           |
|    csin   | |  |   |                   |                           |
|           | +==o===+===================+==+========+=======+====teklabs=+
|           |    |                                                        |
|           |    |                    pdp phs   grumpy  wolfvax           |
|           |    |                     |   |      |        |              |
|           | cincy                unc=+===+======+========+              |
|           |   |        bio       |                                      |
|           |   |  (Misc) |        |        (Misc)                        |
|           |   | sii  reed        |    dukgeri duke34  utzoo             |
|           |   |  |    |          |         |   |       |                |
|      +====+=+=+==+====++======+==++===duke=+===+=======+==+=========+   |
|      |      |    |     |      |   |                       |         |   | u1100s
|    bmd70  ucf-cs ucf   | andiron  |                       |         |   |   |
|                        |          |                       |         |   |   |
|                  red   |          |                       |         |   | pyuxh
|                   |    |          |     zeppo             |         |   |   |
|       psupdp---psuvax  |          |       |               |         |   |   |
|                   |    |          | alice |   whuxlb      | utah-cs |   | houxf
|                allegra |          | |     |     |         |   |     |   |   |
|                     |  |          | |     |     |         |   |  +--chico---+
|                 +===+=mhtsa====research   |   /=+=======harpo=+==+     |    |
|                 |   |  |  |               |  /            |            |    |
|               hocsr |  |  +=+=============+=/           cbosg---+      |    |
|    ucbopt           |  |    |                             |     |   esquire |
|       :             |  |    |                           cbosgd  |           |
|       :             |  |    |                                   |           |
|    ucbcory          |  | eagle==+=====+=====+=====+=====+       |           |
|       :             |  |  |     |     |     |     |     |       |  +-uwvax--+
|       :             |  |  |   mhuxa mhuxh mhuxj mhuxm mhuxv     |  |
|       :             |  |  |                                     |  |
|       :             |  |  |        +----------------------------o--+
|       :             |  |  |        |                            |
|    ucbcad           |  |  |      ihpss    mh135a                |
|       :             |  |  |        |         |                  |
|       :             \--o--o------ihnss----vax135----cornell     |
|       :                |  |        |         |                  |
+=+==ucbvax==========+===+==+=+======+=======+=+========+=========+
  (UCB) :            |        |              |          | (Silicon Valley)
     ucbarpa      cmevax      |              |        menlo70--hao
        :                     |              |        |    |
     ucbonyx                  |              |        |   sri-unix
                              |           ucsfcgl     |
                              |              |        |
Legend:                       |              |      sytek====+========+
-------                       |              |               |        |
- | / \ + = Uucp           sdcsvax=+=======+=+======+     intelqa   zehntel
=           "Bus"                  |       |        |
o           jumps               sdcarl  phonlab  sdcattb
:           Berknet
@           Arpanet

----------------------------------------------------------------------------

S. McGeady

-----------------------------------------------------------------
 gopher://quux.org/ conversion by John Goerzen <[email protected]>
 of http://communication.ucsd.edu/A-News/

Say I was at teklabs and wanted to make the line going down to chico; it seems like there should be some way to say 'select downwards to the next "o" in this column'.

Is there a way that I could add something like ^V do for <visual-block><leader><down-till><o>?

2 Answers 2

2

Expanding the visual selection to the next o in the same column is relatively easy to do manually with :help /\%c:

<C-v>/\%.co

but there is no dedicated built-in motion. One can create custom motions, though, so let's get to work.

Custom motions are essentially visual mode mappings and, ideally, their operator-pending mode companions. It would typically look like this:

" definition
xnoremap gfo /\%.co<CR>

" usage
<C-v>gfo

But this is a special case where handling all the possible target characters would mean creating many mappings.

That's where vimscript comes into play. Instead of defining hundreds of mapping, you can define a single one (well four for f, F, t, and T) that waits for the user to provide a target character:

" definition
" equivalent of f<char> but downward
xnoremap <expr> gf '/\%.c' .. getcharstr(-1, {"cursor":"keep"}) .. "<CR>"

" equivalent of F<char> but upward
xnoremap <expr> gF '?\%.c' .. getcharstr(-1, {"cursor":"keep"}) .. "<CR>"

" equivalent of t<char> but downward
xnoremap <expr> gt '/\%.c' .. getcharstr(-1, {"cursor":"keep"}) .. "<CR>k"

" equivalent of T<char> but upward
xnoremap <expr> gT '?\%.c' .. getcharstr(-1, {"cursor":"keep"}) .. "<CR>j"

" usage
<C-v>gfo
<C-v>gTy
etc.

The <expr> modifier tells Vim to evaluate the right-hand side of the mapping when the mapping is executed instead of when it is defined, which allows us to handle user interaction.

See :help getcharstr() (which links to :help getchar()) and :help <expr>.

NOTE: I used g as "leader" for gf, gF, gt and gT because those combinations are, AFAIK, free to use in visual mode. If you want to use something else, make sure you are not overriding an existing command. See :help index.

1

@romainl already answered very well, but I also want to add that there exists a couple of plugins that provide this feature:

1
  • 1
    There's a plugin for that. Commented 12 hours ago

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.