5 ms·
OT: in Emacs, you could prevent this from happening by using C-u M-% (replace-word): in that case, only those instances of "div" that are separate words (not pa
by kleiba 7y ago
OT: in Emacs, you could prevent this from happening by using C-u M-% (replace-word): in that case, only those instances of "div" that are separate words (not parts of other words) will be replaced.
How would you avoid "inpiduals" (and similar mistakes) in other editors?
- daveFNbuck 7y agoIn many editors, you can use a regex match for word boundaries on either side of div, like \bdiv\d. Some also have a checkbox for the word matching functionality.
- jjtheblunt 7y agothe underlying regex libraries often (like your emacs) have a symbol for word boundaries. emacs is great, though, agreed.
- flear 7y agoJust replace div> with p> ?
- mehrdadn 7y ago"Match whole words" is pretty common. And in every editor invariably ends up disagreeing with you on what precisely constitutes a word boundary.
- stefco_ 7y agoIn vim: :%s/\<div\>/p/g (add C at the end if you want to confirm each change)
- allannienhuis 7y agoTIL what \< means. Thank you.
- deleted 7y ago[deleted]
- deleted 7y ago[deleted]
- nopacience 7y ago%s/<\(\/\?\)\(div\)>/<\1p>/g
- bch 7y agoHuh - that’s confusing in the context of this example, but indeed tests fine on my system (nvi). Since the “word” in question is “<div>”, I thought the \< was just an escape. I looked in various docs and the best I see in Tcl re_syntax(n) is \< and \> are synonyms for [[:<:]] and [[:>:]] respectively, which are constraints matching empty strings at the beginning and end of words, where a word a sequence of word characters... [a-zA-Z0-9_] TIL, too...
- stefco_ 7y agoRight? Totally nonintuitive! But easy enough to remember and type. I figured I wasn't the only one who found it a bit surprising ^_^
- whyenot 7y agoOpen it in a web browser and then copy and past it into the editor as plain text.
- Kudos 7y agoIn this particular case I would just have included the starting < in the search and replace.
- frereubu 7y agoYou mean div> otherwise it wouldn't pick up the closing tags :)
- codetrotter 7y agoIf you replace div> then you will miss any opening tags that have attributes. E.g. <div class=baz> or <div id=foobar>. In Vim I would do :%s@\(</\?\)div@\1p@g It’s a little bit convoluted but it replaces the tag names in both opening and closing tags and does not replace it anywhere else.
- frereubu 7y agoFair point...
- keerthiko 7y agoI believe replacing `<div`\'<p' and `/div`\'/p' are preferred so you can retain any tag properties (class, id, style, etc), but it could still leave behind any tags with superfluous spaces.
- deleted 7y ago[deleted]