• Skip to primary navigation
  • Skip to main content
Sal Ferrarello
  • About Sal Ferrarello
  • Speaking
  • Connect
    Mastodon GitHub Twitter (inactive)
You are here: Home / Computing / Vim Visual Mode Search for Selection

Vim Visual Mode Search for Selection

Last updated on May 2, 2019 by Sal Ferrarello

Sometimes you want to search for a long string in Vim. When using a GUI interface you may be able to get away with CTRL+c, CTRL+v but when you are in a terminal, it is a little trickier to use Vim Visual Mode search for a selection. Here is how to search in Vim using Visual Mode to select the search string.

  • Select the text in visual mode (v and move cursor to highlight)
  • Yank the text (y)
  • Go into search mode (/)
  • Select registers (CTRL+r)
  • Select the unnamed register, i.e. the last yank or delete (")

You never use visual mode?

Then you aren’t leveraging all the power of Vim.  To be fair, I don’t think I know anyone leveraging all the power of Vim but visual mode is a great tool to add to your utility belt.

Using the * Command

Before jumping into Visual Mode (with v), you may be able to do your search without entering Visual Mode. When you press * (Shift + 8) in Normal mode, Vim will search for the word (i.e. keyword) under the cursor. Assuming the text you want to search is a single word, this is a great way to search.

Sal Ferrarello
Sal Ferrarello (@salcode)
Sal is a PHP developer with a focus on the WordPress platform. He is a conference speaker with a background including Piano Player, Radio DJ, Magician/Juggler, Beach Photographer, and High School Math Teacher. Sal can be found professionally at WebDevStudios, where he works as a senior backend engineer.

Share this post:

Share on TwitterShare on FacebookShare on LinkedInShare on EmailShare on Reddit

Filed Under: Computing Tagged With: vim

Reader Interactions

Comments

  1. Dušan Dimitrić says

    July 10, 2019 at 6:12 am

    Nice, thanks!

    Dušan D. – https://dusandimitric.com

    Reply
  2. Wojciech Wisowaty says

    December 3, 2019 at 11:22 am

    Use # (Shift + 3) to search for the word under the cursor backwards.

    Reply
  3. Iron Rodge says

    August 19, 2021 at 3:54 pm

    Select registers seem to not work anymore for Vim..
    Select registers (CTRL+r)
    Select the unnamed register, i.e. the last yank or delete (“)

    However (Shift+Insert) works for pasting the Yank. It’s a reach for keyboards but it still works.

    Reply
    • Sal Ferrarello says

      August 22, 2021 at 10:19 pm

      That’s interesting, I don’t seem to be having the same experience. What version of Vim are you using?

      I’ve tested on both: Vim 8.2 and Neovim 0.5.0 on my Mac, and in both cases the instructions in this article work for me.

      I would double-check you are hitting / (to start defining a search) before (CTRL + r).

      Reply
  4. yurenchen says

    March 9, 2022 at 8:36 pm

    seems this operation sequence just for `copy from select` & `paste into cmd line`.
    the magic things in text (such as /, .) still need handle (escape)

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2023 · Bootstrap4 Genesis on Genesis Framework · WordPress · Log in