A vimscript variable longstring (for instance obtained by let longstring = getline('.')) contains a long sequence of characters (numbers, letters, commas, spaces, ...).

I want to replace a specific character, let it be A, with a carriage return, so that when I use call writefile(longstring, 'this.file', '') I obtain a file with several lines, each containing what was found between two consecutive As.

I tried with let longstring = substitute(longstring,'A','\r','g'), which only results (after writefile) in a single long line file with ^M in place of A. I guess I'm using the wrong replace string, but \n doesn't work either.

1 Answer 1


The first argument of writefile must be a list and each list item represents a line in the new file.

Using split() you could make a pattern-separated list out of longstring as follows:

call writefile(split(longstring, "A"), 'this.file', '')

and obtain the intended result.


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.