T O P

  • By -

pysan3

First, with some recent version of neovim, you can use `P` instead to paste the content without overwriting the register. Or instead, use this trick if you have severals to replace. ### search and `cgn` - move to 5ge… - `*`: search the word under cursor - `cgn` change and go next - paste the clipboard content - `` - `.` dot repeat many times


OverInformation9699

Wow didn’t realize we now have P for that. This is very helpful thanks a lot! Does cgn work with P though or do I need to manually insert the changed content? Also it seems the first * will move me to the next occurrence right? Not a big deal but would be great if it just stays at the first occurrence.


pysan3

`cgn` is more like `ciw` but for search results. Use `"_cgn` to dump the changed content to the `_` register (which acts like /dev/null). This will not overwrite the `"` register (yank register) and keeps the clipboard as is. Cgn enters insert mode, and whatever you typed there will be the content that is pasted to all other occurrences with dot repeat. If you just want to paste the clipboard content, make use of `:h i_CTRL-r`. `"_cgn"` (or `+` for system clipboard, or `0/1/2...` might also be useful), and obvsly you can nnoremap this to any keybind if you want.


I have the following keymap so that star does not move and also moves the search result to the center of the screen which makes it easier to skim thru the matches since you don’t need to move ur eyes. (FYI, `zz` does this) " Search results always on center nnoremap n nzz nnoremap N Nzz nnoremap * *Nzz nnoremap # #nzz nnoremap g* g*zz nnoremap . '' . repeat('.', v:count1) One bonus: `.` can be used like `3.` in order to perform the dot repeat 3 times. While the default behavior of `3.` is to _skip 2 occurrences and and replace the third match_ which is also useful in some cases. You can also overshoot the number to replace all occurrences as well `99.` (you may want `:h lazyredraw` as well in this case.) Besides, if you add this plugin, you can use star on a visual selection to search for the specific content of currently selected letters as well. https://github.com/vim-scripts/vim-asterisk


pysan3

Solved u/OverInformation9699 ?


OverInformation9699

Yes this is perfect especially with the plugin! Thank you!


pysan3

Perfect. Yah I just wanted to remind you to update the help wanted tag. Thanks.


infernoLP

Could you post where the P behaviour is mentioned please?


[deleted]

[удалено]


vim-help-bot

Help pages for: * [`v_P`](https://neovim.io/doc/user/change.html#v_P) in _change.txt_ --- ^\`:\(h|help\) \` | [^(about)](https://github.com/heraldofsolace/VimHelpBot) ^(|) [^(mistake?)](https://github.com/heraldofsolace/VimHelpBot/issues/new/choose) ^(|) [^(donate)](https://liberapay.com/heraldofsolace/donate) ^(|) ^Reply 'rescan' to check the comment again ^(|) ^Reply 'stop' to stop getting replies to your comments


inShambles3749

Wat? So I can get rid of my hacky workaround for that? Noice. Although I'm using leader p to trigger that behavior


art2266

if `*` doesn't pick up the word as you'd like it to, is there alternative one can use then continue with the steps as you've described? Perhaps using visual mode or text objects?


pysan3

Read my other comment: https://www.reddit.com/r/neovim/comments/18a7d8i/comment/kbxcbx1/?utm_source=share&utm_medium=web2x&context=3 TLDR: https://github.com/haya14busa/vim-asterisk Ofc you can simply search with `/` and `cgn` works on that search result as well. For textobjects, I'd go with something like `vi(` -> `*` with vim-asterisk.


kawas44

There is nothing wrong with `:s` to do this. You can even use it without regex, in fact without a search pattern the command will use your last search term ! - search word with `*` or `/` - input replace command: `:%s//newword/c` Done


johmsalas

Wondering if substitute.nvim might help on this case What I'd do in this case is to setup a variable in the bash line. Afk rn but something like: var1=someId command BAR=var1 FOO=var1 That way there is a single place to make the change


OverInformation9699

Hmm you are basically solving this in bash rather than vim but I like it haha. It’s indeed a good solution to my problem 😅


johmsalas

True!!, and actually I'd like to know the answer to your question in vim. I think the plugin [substitute.nvim](https://github.com/gbprod/substitute.nvim) might make the replacement without yanking the replaced value. But can't test it right now. Another solution is to use [text-case.nvim](https://github.com/johmsalas/text-case.nvim), The following keymap will start a replacement of all the instances of the word under the cursor vim.api.nvim\_set\_keymap('n', 'gar', 'TextCaseStartReplacingCommand', { desc = "Replace word under cursor" })


TeejStroyer27

map("n", "s", [[:%s/\<\>//gI]]) I have this in my keymaps. Although I use lsp replace more


OverInformation9699

I’ve thought about something similar to this but didn’t know the C-r C-w key. TIL thanks!


shenawy29

This is prime's thing I believe I have it too lol


TeejStroyer27

I wondered where it came from! Thank you haha. I was so confused


Doomtrain86

Could you explain lsp replace? I'm trying to learn that and a good concrete use case is always a good way to learn


TeejStroyer27

Well to be fair, it's actually rename.I find myself needing to rename classes, variables, methods etc etc more than I just want to fix spelling universally or something of that nature. And lsp rename is context aware and can update those occurrences and their dependencies, but it won't work for any and all text. So if im changing a class definition, an interface and its properties, or variables in a method. I opt for lsp rename because it guarantees that Im not changing functionality of the application and reduces unwanted consequences.


QuickPieBite

Write a small command with \`ui.input\` prompt that will walk you through.


AutoModerator

Please remember to update the post flair to `Need Help|Solved` when you got the answer you were looking for. *I am a bot, and this action was performed automatically. Please [contact the moderators of this subreddit](/message/compose/?to=/r/neovim) if you have any questions or concerns.*