3

If I have the folowing file:

XX:YY:ZZ foobar: some text
XX:YY:ZZ foobar: some other text
a text breaking the pattern
XX:YY:ZZ foobar: some more text

And I want to operate on the differents parts XX:YY:ZZ foobar: of the lines. When I am on the first line I can select the text that I want with, for example, v3f:. Now when I am on the second or on the last line how can I select this same text without type once again v3f:?

I insist on the fact that I need to select the texts sequentially and not all the occurences at the same time.

I know the command gv which allows to re-select the last selected area but in my case it will select the 16 first characters of the first line which is not what I want.

To sum it up How can I execute again the last selection command?

(Also I wasn't sure about the tags I should use for this question don't hesitate to edit/suggest the right ones to use)

  • :help visual-search simple example for search selected text: :vmap X y/<C-R>"<CR>" and gn` - Search forward for the last used search pattern, like with n, and start Visual mode to select the match. – Alex Kroll Jun 17 '15 at 9:42
  • @AlexKroll: I had trouble figuring out what you meant (I think there is a problem in the formatting of your comment) that seems like a nice solution, especially the gn part which I didn't know. – statox Jun 17 '15 at 9:56
  • Yes. I've combined two parts and formatting has broken. First part: search pattern selected in visual mode. You can read about it in vimdoc :help viusual-search. it's just simple example. Second part: navigate forwad to the next matched pattern. Like n in normal mode but visual mode have own mapping gn for more explanation :help gn. – Alex Kroll Jun 17 '15 at 10:01
  • 1
    That's a nice way to do it! I think that it could also be done with doing a first search and then removing the <C-R>" part, this way the search will be done on the last search pattern and the user can yank another piece of text without breaking the mapping. Also if I may suggest you to edit your comment for the comfort of future users that would be nice ;) – statox Jun 17 '15 at 10:08
  • But your question about selection movement. In other word: select from cursor to third colon in other line. My comment covers case for selection exactly same text in other line. – Alex Kroll Jun 17 '15 at 10:32
2

You could simply create a quick mapping:

:nnoremap <key> 0v3f:

Or use a macro recording:

qq
0v3f:
q

then:

@q

Here is another method, lifted from the "experimental" part of my config:

function! GetVisualSelection()
  let old_reg = @v
  normal! gv"vy
  let raw_search = @v
  let @v = old_reg
  return substitute(escape(raw_search, '\/.*$^~[]'), "\n", '\\n', "g")
endfunction

nnoremap <key> *``gn<C-g>
inoremap <key> <C-o>gn<C-g>
xnoremap <key> <Esc>:let @/ = GetVisualSelection()<CR>gn<C-g>
  1. Select your text with v3f:.
  2. Press <key> to enter insert mode.
  3. Edit the selection directly.
  4. Press <key> again to jump to the next match.
  5. GOTO 3

--- edit ---

GetVisualSelection() returns a representation of the selected text suitable for use as a search pattern (escaped slashes and so on).

The normal mode mapping jumps to the next occurrence of the word under the cursor (with *), comes back (with ``), selects the last search (with gn, here it is the word under the cursor) and switches to select mode (<C->g) to allow us to type right away.

The insert mode mapping temporarily jumps out of insert mode (with <C-o>) to jump to and select the next occurrence (with gn) and switches to select mode.

The visual mode mapping has the same function as the normal mode mapping but it is implemented differently: it goes out of visual mode (with <Esc>), places a "prepared" representation of the selected text in the search register (with :let @/ = GetVisualSelection()<CR>), jumps to and select the next occurrence (with gn) and switches to select mode (with <C-g>.

--- endedit ---

  • I didn't precise it clearly but I was wondering if there was a built-in solution which would allow not to use macro or mapping, but your 2 first solution works. For the mapping a cool line to add to vimrc is nnoremap <Leader>r :nnoremap <lt>Leader>t which allow to quickly redefine the mapping of <Leader>t, that could be use to create the quick mapping. For your "experimental solution" I'll have to try it and study it a little bit more because I'm not sure I understand clearly how it works ;) – statox Jun 17 '15 at 11:24
  • Now I understand it better, your last solution is really nice actually! Thanks for sharing that! – statox Jun 17 '15 at 11:41
3

I think the 'vim way' does not rely on using the visual mode to do something with the selection, but the other way around: do something on the 3f: - in your case: e.g. d3f:. If you do the latter, you can move on the line you want and simply press . to repeat that operation.

To answer strictly to your question, you can use:

:norm v3f:

or

:norm ^v3f

from the command line, and repeat that on any line you want, with @:.

  • Indeed you're probably right about the vim way to work... I might be able to find an other way to do what I want. But stricly about my question your solution works for me, thank you. – statox Jun 17 '15 at 9:51
  • You're welcome. Bonus: after the 1st @:, you can use @@ (which is easier to type), while you don't use the command-line. – VanLaser Jun 17 '15 at 9:59
  • Yep I knew this one but I tend to use it for macros and forgot that it can be used in this case. Great bonus anyway! – statox Jun 17 '15 at 10:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.