I want to find all the characters upto the first occurence of the character "/".

I tried using


but this included every character upto the last occurrence.

for example,


matched "test/path/to/" but I want it to only match "test/"

2 Answers 2


It doesn't actually match test/path/to/ -- it matches three times: test/, path/ and to/. If you want to match only match the first one, the easiest way is to anchor the pattern to something, for example, beginning of line:


which works if you really want to match everything before. In practice, you might want to only match what is e.g. separated by whitespace:


Vim has start and end anchors (\zs and \ze respectively) that you can use. In this case, the \ze anchor will help match what you're looking for:

foo\ze\/ matches the "foo" in "foo/bar/baz"

In this example, it stops the search the match after it finds "foo" followed by a "/" but does not include the "/" since we put the anchor before it.

You can read the help on the \ze anchor for more information:

\ze        Matches at any position, and sets the end of the match there: The
           previous char is the last char of the whole match. |/zero-width|
           Can be used multiple times, the last one encountered in a matching
           branch is used.

           Example: "end\ze\(if\|for\)" matches the "end" in "endif" and "endfor".
           This cannot be followed by a multi. |E888|

Your Answer

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

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