10

I really like and often use the cgn command for replacing something I was currently searching for, but it sort of lacks a (imho) rather intuitive feature: replace the word (or whatever) under the cursor. My current approach is to search for the word in question, but that feels like a hack, plus the cursor immediately moves onwards to the next hit.

Is there any way to use cgn on the current word?

1

2 Answers 2

7

There is no built-in way to do it (as an option), as far as I know.

I have the following mapping:

" change current word (like ciw) but repeatable with dot . for the same next
" word
nnoremap <silent> c<Tab> :let @/=expand('<cword>')<cr>cgn

Basically what it does is:

  1. make current search term == word under cursor (without actual search and jump)
  2. call cgn -- and it will use current search from 1.)

For reference:

  • :h @/
  • :h <cword>
3
  • Instead of :let @/=expand('<cword>')<cr> can't you simply use * or # in normal mode to search for the word under the cursor, and then use cgn? :h star
    – statox
    Feb 13, 2019 at 10:17
  • @statox I can, but the cursor will jump to the next occurrence of the word
    – Maxim Kim
    Feb 13, 2019 at 10:43
  • Oh right I use a mapping to avoid the cursor to jump, I forgot about that.
    – statox
    Feb 13, 2019 at 12:33
8

Make * stay at the cursor position

nnoremap * m`:keepjumps normal! *``<cr>

https://stackoverflow.com/questions/4256697/vim-search-and-highlight-but-do-not-jump

Plugins for *: vim-asterisk, vim-slash, ...

There are quite a few (lightweight) plugins which try to enhance searching in vim with the star command.

Typically, people miss that the star command does not consider a visual selection and secondly, what you want, to not move the cursor when pressing *.

After installation of one of these, you would press *cgn to achieve what you want.

In case of vim-asterisk, you have to override the builtin * yourself in your vimrc. vim-asterisk suggests:

map *   <Plug>(asterisk-*)
map #   <Plug>(asterisk-#)
map g*  <Plug>(asterisk-g*)
map g#  <Plug>(asterisk-g#)
map z*  <Plug>(asterisk-z*)
map gz* <Plug>(asterisk-gz*)
map z#  <Plug>(asterisk-z#)
map gz# <Plug>(asterisk-gz#)

where z* means stay. However, you can make * to stay with

map *  <Plug>(asterisk-z*)
map #  <Plug>(asterisk-z#)
map g* <Plug>(asterisk-gz*)
map g# <Plug>(asterisk-gz#)

vim-slash does this for you. This might be a reason to prefer vim-asterisk because you have control of what gets remapped.

There are more of them. Just to give you an idea I mention a few:

Plugin with new operator: sad.vim

If you like an explicit mapping for this operation, consider https://github.com/hauleth/sad.vim. This remaps s (changes builtin and clashes with a few other plugins, e.g. vim-sneak and vim-sandwich). siw and then press . as often you want to repeat the change for the next search matches.

Your Answer

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

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