Product SiteDocumentation Site

12.5.7. Source Files

Source files are the text files prepared with instructions telling LilyPond the content of the score you want it to create. They are so called because these files are the "source" of what you wish to create. As with programming languages, the text inside these files is often referred to as "source code." It sounds scary to think that you must edit code in order to use LilyPond, but "code" just means that it isn't normal English (or insert-language-here).
The particular formatting (the placement of tabs, spaces, and newlines) is not determined by LilyPond, but by individual users. This can be a headache when you encounter somebody else's formatting, but it is one of the keys to the application's flexibility. This guide uses a very specific style of formatting, but it should be understood that this is simply the style of one user, affected by their experiences, and the tasks they usually perform in LilyPond.
You will eventually develop your own style, better-suited to the kinds of tasks that you accomplish. When you do this, there is only one rule to keep in mind: be consistent within source files. When source files are programmed in a consistent way, it means that anybody who wants to use those files (like yourself, in the future) will easily be able to determine how they are organized.

12.5.7.1. Organizing Source Files

LilyPond files are constructed as a series of commands. For better or worse, the LilyPond interpreter allows a great deal of flexibility when it comes to source file setup. This can lead to confusion about where things should be done, especially when using automated score-setup tools like Frescobaldi.
The generic structure of a LilyPond source file is this:
\version version_number

\header { things like title, composer, and so on }

\score
{
	\new Staff
	{
		notes go here
	}

	\layout
	{
	}

	and so on
}
Confusion arises here: for maximum flexibility, LilyPond allows source files to create its own commands. On hearing this, you may think, "Okay that's it - I don't need advanced commands or any of this stuff, so I'm packing it in and just using Finale!" There's no need to do that just yet - commands are easy! Think of commands - whether you wrote them or they were included with LilyPond - as a means of text-substition.
It works like this:
  1. You "define" (create) a command with the form commandName = { lots of commands }
  2. You can then use the command anywhere below that, as many times as you want, by writing \commandName in your source file. When LilyPond processes that portion of text, it will instead see whatever you wrote in the definition.
It's as easy as 1-2!
Frescobaldi (along with most LilyPond users) take advantage of this functionality to provide well-organized, easy-to-use source files.
Here is a good template source file, that might be created for you by Frescobaldi:
\version "2.12.2"

\header
{
	title = "Empty Example"
}

violin = \relative c''
{
	\key c \major
	\time 4/4
	% Music follows here.

	Put your music here
}

\score
{
	\new Staff \with
		{
			instrumentName = "Violin"
		}
		\violin
	\layout { }
}
This example uses many built-in commands (like \relative and \key), and it defines the violin command. Frescobaldi places a % Music follows here. comment where you should input your notes. Notice that your notes belong in the violin section. This means that you write part of the violin command.
Frescobaldi separates the violin's notes from the \score section so that the source file is easier to read. You can fix errors more easily in well-organized source files. If you complete the following tutorials, you will learn three strategies to organize sources files.
The \layout section is empty in this example. Like the \score section, \layout is a command. The \layout command tells LilyPond to output a musical score. Advanced users sometimes remove the \layout command, but most source files use it.