Sample Header Ad - 728x90

How do I search for a string and then insert a newline at the beginning, for all lines in file?

1 vote
1 answer
999 views
I want to search an entire file for the string --- when it appears at the beginning of a line. I then want to insert a newline before each instance of that string. In my .vimrc file, I added this command: let @p=":1,$s/^---/^M---/" When I execute it, it does not insert the newline character. Instead it ignores everything from the ^M on, so it replaces the --- with a null string, removing the 3 dashes from each line that contains them at the beginning. What is wrong with my @p command? I doesn't seem to recognize the ^M character. Is this not allowed in a search and replace command? I can do this manually, by the way, by simply typing the exact same command at the : prompt at the bottom of the file. I also tried: let @p=":1,$s/^---/^M^[---/" (added an escape after the newline) - this gave me the exact same result. Looks like it's ignoring the escape along with the rest of the replace section. Can someone tell me the correct way to do this? I do want to keep these commands in the vimrc file, because I edit a lot of files that have the same set of commands (and involve several macros, all of which work fine except this one), so I don't want to recreate the same macros every time I open a new file. Thank you!
Asked by Bastette (11 rep)
Aug 16, 2022, 07:20 AM
Last activity: Aug 16, 2022, 12:46 PM