I want to convert the 5 space indentation in a python file to 4 space indentation. I want the command to do the following
remove a single space in all the lines which starts with a space followed by characters.
I issued the command %s/^\ [a-zA-Z]*// which seems to work. Later i figured out that the command should actually be
remove a single space in all the lines which starts with a space followed by any number of spaces followed by characters.
However still i am not able to figure out how the command(above) is working. It should basically report error for the following stating pattern not found but still it works.
class H:
def __init__():
hell()
It’s working because
*means “match zero or more of the previous atom”. In your case, it’s matching zero. You probably wanted to use\+instead which means “match one or more of the previous atom”.In actuality, you could have just dropped the
*entirely because just a space followed by a single character would have matched what you were originally searching for. There are better regular expressions for what you’re trying to accomplish, but that’s not what you’re asking here.Edit (clarification):
Your regex as it stands (
^\ [a-zA-Z]*) translates to:^: From the start of the line\: Match a space[a-zA-Z]: Followed by a letter*: Zero or more times (of the previous atom – a letter)