This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-05-30
Channels
- # announcements (2)
- # babashka (3)
- # beginners (31)
- # biff (3)
- # calva (40)
- # chlorine-clover (2)
- # clerk (5)
- # clj-kondo (10)
- # clojure (13)
- # clojure-art (1)
- # clojure-denver (16)
- # clojure-europe (44)
- # clojure-nl (1)
- # clojure-norway (27)
- # clojure-sweden (3)
- # clojure-uk (1)
- # clojurescript (49)
- # clr (6)
- # community-development (2)
- # datalevin (10)
- # datomic (63)
- # events (2)
- # fulcro (9)
- # holy-lambda (15)
- # honeysql (8)
- # hoplon (6)
- # hyperfiddle (9)
- # introduce-yourself (1)
- # kaocha (1)
- # london-clojurians (2)
- # matrix (1)
- # nbb (7)
- # off-topic (38)
- # polylith (6)
- # re-frame (4)
- # reagent (2)
- # releases (3)
- # sci (13)
- # scittle (5)
- # shadow-cljs (6)
- # sql (5)
- # tools-deps (4)
- # vim (33)
- # web-security (8)
- # xtdb (2)
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
For the region replacement you can select the region and then do the :s/prefix/octo-prefix/g in nvim too
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
> 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.
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)
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.
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.
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
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.
What command is the s
an alias for? I assume its substitute?
:help :s
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.
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...
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.
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.
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.
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
.
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.
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 😄
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.
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&cid=CPABC1H61
I'm running the unstable version of Neovim (v0.10.0-dev) which ought to have those fixes, I would think
This looks like it might be related: https://github.com/neovim/neovim/issues/23819
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.
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