How to find-replace leading newlines in Cursor
I had a bunch of files with empty newlines at the start of the documents. I tried searching for these in Cursor (this should also work for VSCode) using \A
to match at the absolute beginning of the file, but VSCode considered that invalid syntax. So I had to use negative-look-behind instead:
(?<![\s\S])(?:\r?\n)+
(?<![\s\S])
= “there are no characters of any kind before me”?<!
is the negative look-behind\s\S
means "whitespace or non-whitespace", i.e. any character
(?:\r?\n)+
= the leading blank lines(?:
starts the non-capturing group- then match one or more blank new-lines
Similarly, to delete one or more empty lines at the end of files, you can use:
(\r?\n)(?:\r?\n)+$
...where $
means the very end of the file.