3

I try to use regex such as ^ to find the start of the line in the gedit editor in Ubuntu 22.04 but it doesn't work. Also for $ as the end of the line.

Other regular expressions such as \n,\d,\s work well

7
  • 1
    What's the "standard text editor" you're talking about? ed, ex or vi (the 3 that I know are in the POSIX standard)? Or are you talking of gedit as the tag suggests? Commented Nov 29, 2023 at 6:34
  • Yes, please edit your question and clarify which editor. If you are using a GUI one, it's probably gedit, but Ubuntu also uses nano as the default command line editor. As far as I know, gedit doesn't support regular expression search, it is just a very simple editor. Commented Nov 29, 2023 at 10:41
  • @StéphaneChazelas Yes, it is gedit and it supports some of regular expressions. Commented Nov 30, 2023 at 7:07
  • 1
    Think of gedit like notepad in windows. Yes, a bit unfair because it is a little bit better. But It is still very basic (and slow for big files). Install a better text editor, this is one of the first things I do after installing my system. I prefer geany. In search field you can enable regex search and/or multiline match. Commented Nov 30, 2023 at 7:11
  • AFAICT gedit supports regexps in its Find and replace if you tick the regex box, but not in Find unless you install extra plugins. Commented Nov 30, 2023 at 7:11

1 Answer 1

2

^ and $ work in gedit's Find and replace provided you tick the Regular expression box but gedit skips empty matches.

^$, ^ alone match an empty string so won't be replaced. x* matches 0 or more x's but gedit won't replace the occurrences matching 0 x's, so it's equivalent to using x+.

If you want to use Find and replace to insert something at the start of the line, instead of replacing ^ with something, which won't work, you can replace ^. (the first character of the line) with something\0 (something followed by what was matched), bearing in mind that it won't work for empty lines where ^. doesn't match.

You can however replace (?s)^. with something\0. By enabling the s flag, . will also match on newline, so it will also work on empty lines. The only case where I find it doesn't work is on the last line if it happens to be empty, presumably because gedit doesn't include the line delimiter of the last line in the buffer its regexp matches against.

And to replace empty lines with something, replace ^\n with something\n instead of ^$ with something. Again, that doesn't work for the last line.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.