vim

practicalli-johnny 2023-05-30T09:46:34.204509Z

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

lispyclouds 2023-05-30T09:53:57.025859Z

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

sheluchin 2023-05-30T10:07:56.167949Z

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čius 2023-05-30T10:26:31.980179Z

> 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.

tomd 2023-05-30T10:30:02.222309Z

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)

➕ 6
Martynas Maciulevičius 2023-05-30T10:31:58.603299Z

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.

👍 1
2023-05-30T11:05:08.854209Z

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.

👍 1
practicalli-johnny 2023-05-30T13:32:59.209359Z

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

lispyclouds 2023-05-30T13:39:18.544109Z

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-johnny 2023-05-30T13:49:20.002789Z

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

sheluchin 2023-05-30T13:49:37.037389Z

Yep

2023-05-30T13:49:49.208509Z

:help :s

2023-05-30T13:50:01.872809Z

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-johnny 2023-05-30T13:54:16.268919Z

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...

sheluchin 2023-05-30T14:12:01.414219Z

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čius 2023-05-30T14:12:48.721669Z

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-johnny 2023-05-30T14:20:54.729799Z

Ah, it was the % character that represents the current file that was missing form the command, thanks @alex.sheluchin 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.

sheluchin 2023-05-30T14:22:36.720769Z

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-johnny 2023-05-30T15:16:23.778759Z

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

lispyclouds 2023-05-30T15:18:56.352189Z

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 😄

dave 2023-05-30T17:13:18.849139Z

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 🤔 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.

ericdallo 2023-05-30T17:14:26.670799Z

Hey Dave 👋 I think it's a nvim issue that was fixed recently as mentioned https://clojurians.slack.com/archives/CPABC1H61/p1685432803988229?thread_ts=1684325362.342129&amp;cid=CPABC1H61

dave 2023-05-30T17:17:19.100059Z

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

👍 1
ericdallo 2023-05-30T17:17:32.716969Z

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

dave 2023-05-30T17:18:45.625299Z

Nope, still hangs for me after updating Neovim 😞

😔 1
dave 2023-05-30T17:19:39.258549Z

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

dave 2023-05-30T17:20:49.258829Z

This looks like it might be related: https://github.com/neovim/neovim/issues/23819

ericdallo 2023-05-30T17:21:29.744809Z

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

ericdallo 2023-05-30T17:23:01.914459Z

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.

dave 2023-05-30T17:23:28.134859Z

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

dave 2023-05-30T17:24:55.767099Z

Nice, that fixed it!

ericdallo 2023-05-30T17:26:28.597319Z

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

dave 2023-05-30T17:27:11.359149Z

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

👍 1