2

I'm really struggling to figure out how to find all occurrences of a semicolon not preceded by a backslash. I'm hoping to find a quick vim command to clean up long bash commands that I've opened with fc -e vim. Usually I can just do a regular find semicolon and replace with semicolon then new line to split my commands onto separate lines to make my script more readable. In this case I have a find -exec command in there which ends with \; but is then piped into another command. I want this to all be on one line. I know it would be quick to do by hand but I'm trying to learn vim better. I've tried all kinds of things but from my understanding the following should work:

:%s/\(\\;\)\@<!;/;^M/g

But it catches the \; in my find command as well. I would appreciate one of you pros pointing out what I'm missing here. Thank you.

1 Answer 1

4

this regex should match what you want

/\(^\|[^\\]\)\zs;/

Broken down: /\(^\|[^\\]\)\zs;/ 122222222222233345 aabccdeeefgg

1 : /       start the search
2 : \(...\) group what to match first (consists of a...g)
  a: \(  begin a group (kind of making an atom)
  b: ^   start of line
  c: \|  or
  d: [   start a character class
  e: ^\\ elements of the class are all but \ (^ negates here)
  f: ]   end the character class
  g: \)  end of group
3 : \zs  set the search-start
4 : ;    what to search for
5 : /    end the search
3
  • I appreciate your quick answer. Would you mind explaining it a bit? I'm trying to break it down by piece and just reference man pages, but I'd like to hear your explanation if you're willing. It looks to me like the first part is from the beginning of the line (^ Then doesn't match backslash [^\] Then I know \zs starts a pattern match. I'm not sure what the pipe signifies there? Or am I wrong about the initial carrot signifying the beginning of the line? Thanks - I appreciate it. Jan 28, 2018 at 21:39
  • 1
    I unrolled it in the answer now.
    – Naumann
    Jan 28, 2018 at 22:04
  • Perfect! Thank you very much. Vim is such a powerful tool but woof it can be a bear when you're learning. I appreciate the help. Jan 28, 2018 at 22:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.