[vim/vim] Configure star command not to advance to next match (Issue #11050)

82 views
Skip to first unread message

jzinn

unread,
Sep 3, 2022, 4:08:33 PM9/3/22
to vim/vim, Subscribed

Why does the star command, *, advance to the next match, rather than to the beginning of the current match (the word under the cursor when star is invoked)?

So often do I immediately back up (using N) to the word my cursor was over when I invoked the star command that I wonder if I can make this the default behavior. Is it possible? Is it misguided to want this?

I briefly grepped the vim source tree but was unable to find if anything for this exists.


Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/11050@github.com>

Nick Jensen

unread,
Sep 3, 2022, 4:46:53 PM9/3/22
to vim/vim, Subscribed

Why would you want this? When I use * I want to find other usages of the word under the cursor. If it's about highlighting then you can easily add nnoremap * *N to your personal config.


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/11050/1236196140@github.com>

jzinn

unread,
Sep 3, 2022, 6:42:50 PM9/3/22
to vim/vim, Subscribed

I want it for example for doing a search and replace in a visually selected range of lines where the I make the selection by using *, V, and N.

For example, typing something like *VNN:s/C-R//replacement. If my cursor is over the word I want to replace, let's call it oldword, I can type * to select oldword as the search term, V to start a visual highlight, NN to select the range of lines that include two more instances of the search term, and type : to go into command mode (which prepopulates :'<,'>), and then do a search and replace by starting with s/, then typing CTRL-R / to paste \<oldword\>, and then complete the command :'<,'>s/\<oldword\>/newword/g.

Usually I want to replace the instance of oldword that my cursor was on when I typed *. So I type * and then immediately back up using N.

Why would you want the current behavior?

Thank you for the nnoremap suggestion. I'll try it.


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/11050/1236208799@github.com>

lacygoill

unread,
Sep 3, 2022, 7:14:50 PM9/3/22
to vim/vim, Subscribed

How about these mappings:

# bounded search
nnoremap c* <Cmd>call expand('<cword>')->printf('\<%s\>')->setreg('/')<CR>cgn
# UNbounded search
nnoremap cg* <Cmd>call expand('<cword>')->setreg('/')<CR>cgn

Then:

  • press c* (or cg*) while on the word you want to change
  • insert the new word
  • press Escape to get back to normal mode
  • press . as many times as necessary to replace the next occurrences of the old word


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/11050/1236211784@github.com>

jzinn

unread,
Sep 3, 2022, 7:49:50 PM9/3/22
to vim/vim, Subscribed

Wow, nice! I'll play with that.

I'll close this issue, though I would still like to know what the motivation is for star to automatically advance to the next match.

I presume it's to make some repeated use nicer, but I just can't think of an example to demonstrate why.


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/11050/1236214622@github.com>

jzinn

unread,
Sep 3, 2022, 7:49:50 PM9/3/22
to vim/vim, Subscribed

Closed #11050 as completed.


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issue/11050/issue_event/7317669537@github.com>

Nick Jensen

unread,
Sep 3, 2022, 8:06:15 PM9/3/22
to vim/vim, Subscribed

@jzinn as I said, I use it (and I assume this is the original purpose) to find other usages of the word under the cursor. So I'm not interested in staying on the original match, I want to skip to other parts of the document where that word is used. For replacing the word, I use variations on the mappings @lacygoill suggested.


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/11050/1236217081@github.com>

Bram Moolenaar

unread,
Sep 4, 2022, 7:28:05 AM9/4/22
to vim/vim, Subscribed


> Wow, nice! I'll play with that.
>
> I'll close this issue, though I would still like to know what the
> motivation is for star to automatically advance to the next match.
>
> I presume it's to make some repeated use nicer, but I just can't think
> of an example to demonstrate why.

I nearly always use "*" to find the next instance of a word. And with
'hlsearch' on I can see all instances of it. Usually a variable or
function name.

--
Imagine a world without hypothetical situations.

/// Bram Moolenaar -- ***@***.*** -- http://www.Moolenaar.net \\\
/// \\\
\\\ sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/11050/1236316413@github.com>

Christian Brabandt

unread,
Sep 4, 2022, 7:47:01 AM9/4/22
to vim/vim, Subscribed

If i use the * family of commands, I always want the cursor move to the next/previous instance of the match.


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/11050/1236319584@github.com>

Reply all
Reply to author
Forward
0 new messages