How to refer to the start of the selection?
honestSalami opened this issue · comments
Since the commands '^' and '$' refer to the beginning and end of a line, how can I refer to the beginning and end of the current selection?
like, if I have this text (where [] denote the start and end of the selection):
hello [world
how are you?
I'm feeling fine].
How can I insert a newline at the beginning and end of the selection, so that I end up with:
hello
world
how are you?
I'm feeling fine
.
given that x/^|$/ i/\n/
will not work as I expected?
The answer to your second question is :x/(.|\n)*/ c/\n&\n/
(the x can be left out if you already got the selection via an x or something other that updates the registers).
For your first question, you probably want to use :+/./
(the first character not-\n
after the current selection) for the end of the selection and :-/./
though that's the character before the current selection.
How can I insert a newline at the beginning and end of the selection, so that I end up with:
You can do something like :{ i/\n/ a/\n/ }
.
Sorry for taking so long to answer, but I wanted to give each answer the attention it deserved.
@ninewise
For your first question, you probably want to use
:+/./
(the first character not-\n
after the current selection) for the end of the selection and:-/./
though that's the character before the current selection.
This DOES select the character before the current selection, but it does not seem to be that useful as I can't use it as an anchor for testing matches. For example, if I want to select all the words that contain an 'o' and start with an 's' (a rather contrived example XD), this sre would seem to do the job: ,x/ [^ ]*o[^ ]* / x/[^ ]+/ -/.s[^ ] /
except that that just selects the first two letters before the selection which contain an s. After some experimentation, I think that there is no regex substitute for the beginning or end of the selection. This seems to me an important omission.
How can I insert a newline at the beginning and end of the selection, so that I end up with:
You can do something like
:{ i/\n/ a/\n/ }
.
This achieved just what I was aiming for, thanks! Though not having anchors for the beginning and end of the selection is quite bothersome in my opinion, I think that this implicit reference to them is quite elegant.