I got tired of hacking ad-hoc text selection functions, so I wrote edtext: line selection and manipulation using ed address ranges.
https://nedbatchelder.com/blog/202602/edtext
If you have a fediverse account, you can quote this note from your own instance. Search https://hachyderm.io/users/nedbat/statuses/116040757638652440 on your instance and quote it. (Note that quoting is not supported in Mastodon.)