I never use source with !, I'm just curious.

I know source! is used to read normal commands. But its behavior is a bit strange.

My experimental file:


My normal command file:


Assume cursor at "l" of "line1", after source! cmdfile, cursor moved to "i" of "line2" instead of "line1". If I add a blank line after "l" in cmdfile, and try everything again, cursor will move from "l" of "line1" to "i" of "line3".

It looks like linefeed was read as a normal command <c-j>.

Is that normal? Am I doing something wrong? What would source! be good for if it always read linefeeds as a normal command <c-j> ?

  • 1
    Not sure whether it helps, but you can use the optional third argument of writefile() to pass it the flag b when it writes into a file. This could be used to prevent a NL from being written at the end of the last written line. For example: :call writefile(['l'], '/tmp/cmdfile', 'b') | so! /tmp/cmdfile. You can check the contents of /tmp/cmdfile by running :!od -tc % while it's the current file.
    – user938271
    Apr 8, 2019 at 2:36
  • And according to this chart, C-j is a linefeed, which makes it sometimes difficult to map something to it in Vim.
    – user938271
    Apr 8, 2019 at 2:44
  • It's good to know writefile with b flag, but i'm not sure if this is the right way to use source!. C-j is very handy, i nmap it to BTags of fzf-vim, that's also how i find the strange behavior in the very beginning.
    – dedowsdi
    Apr 8, 2019 at 3:02

1 Answer 1


source! reads literal character (including linefeed) as it's command.

Although newline is statement separator in ex command file, newline is still newline in normal command file, it make things so much easier and clearer if you want source! to generate multi-line text.

I guess source! mainly has 2 usages:

  • Insert multi-line text, it would be a mess if lines are separated by^M.

    iclass ClassName
  • As a normal command storage file, it can be used together with :global, :*do family. The trailing newline must be removed if you want to use it this way, as @user938271 pointed out, you can use writefile with b flag to save without trailing newline, here are some other ways to do it.
  • 1
    Preventing the final newline: set noendofline nofixendofline
    – Ralf
    Apr 8, 2019 at 7:46
  • This is the best way.
    – dedowsdi
    Apr 8, 2019 at 8:11

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.