Fork me on GitHub
#vim
<
2023-05-30
>
practicalli-johnny09:05:34

I have multiple occurrences of the word prefix in a file and would like to rename them all, e.g. to octo-prefix However, there is one occurrence of 'prefix` that I do not want to change. Most occurrences are on consecutive lines, but not all of them. What are my options? (I'm using neovim with AstroNvim if it makes a difference) In Emacs I would use iedit to select all the occurences and then unselect those that I didnt want to change. Or narrow to a region and change all occurrences in that region. I seem to do this quite a lot, so something relatively simple is preferred, Thank you

lispyclouds09:05:57

For the region replacement you can select the region and then do the :s/prefix/octo-prefix/g in nvim too

sheluchin10:05:56

You could use a negative lookbehind to exclude that one prefix based on what precedes it:

def
abcdefghij
xdef
klm
def
abc
xyzabc
:%s/\v(xyz)@<!abc/FOO/g
def
FOOdefghij
xdef
klm
def
FOO
xyzabc

Martynas Maciulevičius10:05:31

> in a file Do you work with a text file or with a file where you have LSP server? If you have LSP server for this file then you could invoke that instead of replacing strings. For instance I do this when I use clojure-lsp.

tomd10:05:02

If you have a small enough number of matches, you could review each interactively: :%s/prefix/octo-prefix/gc the c flag allowing for you to hit y to replace, n to skip, and a to replace all remaining (once you've skipped your single occurrence you don't' want to change)

12
Martynas Maciulevičius10:05:58

Or you could select a region where you have this match and then rename it into something else. And then use string replace in whole file. Then you'd rename the thing back to what you renamed it from.

👍 2
Noah Bogart11:05:08

If there’s a single item i don’t want to change, i would manually edit it to by something unique, then use :%s//g to change all other instances, then manually edit the single occurrence back to the original word. Getting clever with it would be annoying and harder to perform on the spot.

👍 2
practicalli-johnny13:05:59

There are over 30 matches although its all in one file. Its for key bindings so there are a lot of repeats. I also need something similar when updating library versions in aliases in the Practicalli Clojure CLI config, although there are not as many occurrences. I find iedit useful when editing similar occurrences in search results across projects with a large number of files. I'm a bit surprised there isnt something similar to iedit for neovim (considering how useful it is in Emacs, especially with narrowing). I used LSP rename in the end as I'd added a Lua server, it worked well enough, with a little post fix update. I should learn :s/prefix/octo-prefix/g but this is the sort of thing I easily forget. I'll add it to the neovim book (assuming I didnt already and forgot). Thanks for the all ideas though

lispyclouds13:05:18

nvim with its search replace preview when you do :s/foo/bar… should get you quite close to iedit like behaviour on the same file.

practicalli-johnny13:05:20

What command is the s an alias for? I assume its substitute?

Noah Bogart13:05:01

4.2 Substitute						*:substitute*
							*:s* *:su*
:[range]s[ubstitute]/{pattern}/{string}/[flags] [count]
			For each line in [range] replace a match of {pattern}
			with {string}.
			For the {pattern} see |pattern|.
			{string} can be a literal string, or something
			special; see |sub-replace-special|.
			When [range] and [count] are omitted, replace in the
			current line only.  When [count] is given, replace in
			[count] lines, starting with the last line in [range].
			When [range] is omitted start in the current line.

practicalli-johnny13:05:16

Strange, I undid the change and tried :s/prefix/octo_prefix/g but it only changed the first match. I'll take a look at the help and see if I can figure out why. The first occurrence is highlighted and changes as I type in the new string to use, but that is the only occurrence that changes. All the occurrences of prefix are highlighted, and I can use n to jump to the next ones, but they haven't changed. Maybe some plugin is interfering...

sheluchin14:05:01

You need specify the whole file by doing :%s/x/y/g, and note that you need gc if you want to get prompted for confirmations.

Martynas Maciulevičius14:05:48

I think this is not productive. Could you open a video instead? This is a very basic question and we're beating around the bush.

practicalli-johnny14:05:54

Ah, it was the % character that represents the current file that was missing form the command, thanks @UPWHQK562 I found the c flag via the readme, this is very useful. The g flag is not of use in this specific example, as there is only one occurrence on each line (I assumed at first that g was global on the file, but its just for the line - amazing what you learn reading the docs 😄 ). g will be useful for other tasks. It still feels way easier to do this in Emacs (`#` to select all occurrences, call iedit, type new string) 😞 but I appreciate all the effort to help me. Thanks.

sheluchin14:05:36

You can also select all occurrences by putting the cursor over one of them and hitting * or # (depending which direction you want to search for, but barely relevant here) and after that :%s//foo/c.

practicalli-johnny15:05:23

Ah, that's a nice addition, thanks @UPWHQK562 . I'll practice some more over the coming weeks and see if I can get used to it. Thanks all.

lispyclouds15:05:56

In general I’ve found the vim docs as equally complete, enlightening and beginner friendly! Highly recommend to read even if you’re not facing an issue 😄

dave17:05:18

Has anyone else had issues with Neovim hanging when editing Clojure files? I've noticed it only in one particular project, and I'm not sure why. This started happening recently after I updated my config to use a bunch of modern, Lua-based plugins, so it could be related to a number of different things, unfortunately :thinking_face: I did notice, however, that the problem goes away when I remove the lspconfig.clojure_lsp.setup {} part of my config, i.e. disable clojure-lsp. So I'm thinking this might be clojure-lsp related.

dave17:05:19

Hey! 👋 That's great news! I'll try updating Neovim and see if that fixes it.

👍 2
ericdallo17:05:32

but AFAIK that was affecting only wsl2, LMK if works for you

dave17:05:45

Nope, still hangs for me after updating Neovim 😞

😔 2
dave17:05:39

I'm running the unstable version of Neovim (v0.10.0-dev) which ought to have those fixes, I would think

ericdallo17:05:29

indeed, downgrading is always a good way to test things :)

ericdallo17:05:01

https://github.com/neovim/neovim/issues/23725#issuecomment-1561364086 are a headache indeed, and depending on the client implementation it may be slow, emacs-lsp had similar issues in the past.

dave17:05:28

I just found that workaround myself -- giving it a try now

dave17:05:55

Nice, that fixed it!

ericdallo17:05:28

cool, it's a nice feature, but totally optional, the client basically watches for code chances in the project that are done outside the editor, like git branches switch, and then send to server to keep analysis updated, without that you may face outdated reference lens, or diagnostics, but nothing that a LSP restart wouldn't solve

dave17:05:11

Ah, I see. That would be nice to have, but I can live without it for now

👍 2