I usually search for words by :/string, but if I have to search entire current line, how to do that.

event: 1  beacon: 0x02              //line 1
event: 19 beacon: 0x02              //line 2
event: 1  beacon: 0x03              //line 3
event: 1  beacon: 0x02              //line 4
event: 8  beacon: 0x01              //line 5
event: 19 beacon: 0x02              //line 6
event: 1  beacon: 0x02              //line 7

For example, if cursor is presently at line 1 how to search similarly as :/ for text containing entire current line. The cursor should jump then to matching line 4, and again to line 7


Here's how I'd do it:

  1. Yank the line (into the unnamed register), without including the newline at the end: 0y$
  2. Initiate a very nomagic search: /\V
  3. Add the yanked line: <c-r>"
  4. Fire off the search: <cr>

As @EvergreenTree points out in a comment, you can of course create a mapping to perform all the steps in one go:

nnoremap <leader>* 0y$/\V<c-r>"<cr>

Relevant help topics:

:help registers
:help /magic
:help c_CTRL-R

N.B. If the line you're searching for contains backslashes, you'll still need to go through and escape them (by adding another backslash), even with a very nomagic search.

  • 4
    Of course, you could also create a mapping for this: nnoremap {key} 0y$/\V<c-r>"<cr> – EvergreenTree Jan 21 '16 at 23:50

Especially for mapping purposes, I find using getline() more elegant than doing the yanking yourself. Calling getline() with a string '.' returns the line under the cursor. There are two good options for using this:

:exec '/' . getline('.')

which parses the strings '/' and what is returned from getline() together and executes that as a vim command.



which uses the expression register @=. The expression following = is evaluated after the first <CR> and placed in the command line.

  • Nice! When the line contains character like / or ' it does not work very well. Anyway, I learn something today! Thank you – Luc M Aug 3 '17 at 16:16

Personally I would get a visual star plugin (there are few out there). There is a nice Vimcast about this: Search for the selected text. This means you can select the line visually and then press *.

If a plugin isn't your thing you can add the following mapping to your vimrc:

xnoremap * :<c-u>let @/=@"<cr>gvy:let [@/,@"]=[@",@/]<cr>/\V<c-r>=substitute(escape(@/,'/\'),'\n','\\n','g')<cr><cr>

Note: Visual-block (<c-v>) is not supported for this mapping.

  • I added the mappings but actually it's jumping to the first matching word and NOT selecting the entire line. – manav m-n Jan 22 '16 at 15:04
  • If it is match a word then I assume you are using * from normal mode or do not have the mapping installed correctly. – Peter Rincker Jan 22 '16 at 15:15

The existing answers all fail if your line contains certain items that are interpreted as a part of a pattern. The \V point gets most of the way there but still messes up with the backslash.

Try this to escape the backslashes...

/\V<C-R>=escape(getline('.'), '\')

And then if you're using it in a mapping you'll need one or two <Enter>s at the end depending on how you want it to work.

  • Excellent answer! Lines often contain both kinds of slash which confuse the other answers. That last argument of escape should include a forward slash too though: escape(…, '/\') – bobbogo Jan 15 at 10:04

With vim-asterisk installed just use V$* (or 0v$*) to select the current line and search for it.

In the second command 0 is not necessary if the cursor is already on the start of the line.

  • 1
    Doesn't * only search for the word under the cursor, and not the current visual selection? – EvergreenTree Jan 22 '16 at 14:06
  • 1
    @EvergreenTree I did forget to mention the plugin. Thanks for notice – adelarsq Jan 22 '16 at 16:17

One more way to do this is using the command-line window: yy/<Ctrl-F>p<Enter>

  • Yank the whole line you need with yy
  • Press / and then Ctrl-F to open command-line window
  • Press p to paste the line
  • Press Enter to start the search
  • 5
    You can also use q/ to get the same effect as /<c-f> – evilsoup Jan 23 '16 at 0:59

http://vim.wikia.com/wiki/Searching_for_expressions_which_include_slashes points out that the search register can be set directly, like this:

:let @/='/abc/def/ghi/'

This allows search on the line and works even when the line includes slashes.

So to make, say, \* map to a line search:

nnoremap <leader>* :set hlsearch<cr>:let @/=getline('.')<cr>

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.