Help:Newlines and spaces

Edit box
In the edit box, if a line of text is so short that the first word of the next line would fit on it, this indicates that there is an invisible newline code at the end. In other cases it is not directly visible whether there is a newline or just word wrap, with a space between the last word of one line and the first word of the next line. One way to detect an unwanted newline is to try changing the width of the window. Or, to eliminate a suspected newline whether or not it is really there, simply delete characters until the last word of the first line runs together with the first word of the next, and enter the desired separator. This method also works when there were one or more spaces before the newline.

To detect unwanted space characters at end of line without making changes, put the cursor to the right of the last visible character and move it using the right arrow key. If there is no space at end of line, the cursor will move to the beginning of the next line; in this case there is almost always a newline present (but see the last example below). Otherwise, the cursor moves rightward and there is a space to its left. Continued use of the right arrow key will reveal any additional space characters, but the right arrow key cannot distinguish the presence or absence of a newline.

For page rendering these distinctions are not relevant in plain text (see below): all three possibilities render as a space. However, table and list code is sensitive to newlines, and if template expansion produces a newline at the start (or end), this cumulates with a newline before (or respectively after) the template call, giving a new paragraph.

Word wrap might also take place without a space, as in the last example below. Thus, if a line ends with a non-alphanumeric and the next starts with one, there might be nothing, a space, a newline or both between them. In at least one browser, it is not possible to distinguish "nothing" from a newline without either changing the window width or deleting characters. To reliably eliminate a possible newline between the end of one line and the beginning of the next, place the cursor to the right of the last character of the line and delete the next character (or place the cursor to the left of the first character of the next line and delete the previous character) until the opposite character disappears. Then replace it. See the last example below.

Examples (see edit box and vary the window width):

Here is a newline: Here is a newline with a space: Next line. Table code follows after a newline: The same without a newline:
 * 1) This ends with a newline.
 * Pqr
 * 1) No newline after this. #Pqr

A tab character is for example produced by Special:AllPages. It shows up in the edit box as at least one and at most 8 spaces, namely such a number that the last space is at a position of a multiple of 8, counting from the left from 1. Thus when inserting a character on the left, the character after the tab either does not move, or move 8 positions.

considers a space, a tab and a newline as all different, e.g.  (with first a space and then a tab) gives.

Rendering
Two or more spaces are rendered as one; one newline is rendered as a space; two or more newlines are rendered as such:

p q r   s    t u

v

w

gives

p q r   s    t u

v

w

A single newline is rendered as such using the parser function #tag:poem:

A tab is rendered as a space.

Trimming on save
In the case of one or more newlines at the start of the wikitext of a page, this number is reduced by one on every edit. Thus just pressing edit and save changes the page; this can be repeated, until no newline is left. Spaces (and hence lines with just spaces) are preserved. This unwanted behaviour has been fixed in MediaWiki 1.18 (see bug 12130).

Spaces and newlines at the end of the wikitext of the page are deleted right away on saving.

Thus a stored page has no newline at the end but may have one or more newlines at the start.

To preserve spaces and newlines at the start and end of the included part of a template, add a noinclude part (it can be dummy, in which case the pair of tags can be reduced to ) at the start and/or end, see and. Alternatively includeonly tags can be used.

Conversely, avoid an unwanted newline between the included part and the noinclude part.

Automatic newline
Automatically a newline is added before the expanded wikitext of a template, parser function or variable if this expanded wikitext starts with " " (ASCII 35), " " (42), " " (58), " " (59), or " ":

{{xpdoc|T table lb|
 * {{!}}||} }}

It is not prevented by a noinclude part at the start:

This also occurs when the result of a parser function starts with these characters:

If this is not desired, as a workaround some unconditional text, if any, can be moved into the then- and else parts:

pq

A newline is also added when the result of a variable starts with one of these characters. However, for one of the few variables where this could happen, PAGENAME (and related parser functions), this has been disabled, see *demo.

Thus string processing that processes characters one by one typically adds newlines before "*", "#", ":", ";". See e.g. Template:Sub with the second parameter not equal to 0.

Workarounds that may be suitable in some cases:

Use a parameter tag, see below.

Instead of p, use , where q1 is an adapted version of template q putting  in front of the result that q would give, excluding the possible automatic newline.

Similarly for some parser functions:

- for conditional functions (those with "#if..", and #switch) put the p as prefix before the then and else parts, like in the example above, and:

--

--

One might also remove the undesired newline(s) with a template like Template:Rmanl. The possibilities seem limited: since the parser functions for string operations are very limited, various not directly supported string operations require complex templates, so that quickly system limits are reached.

In some cases a suitable workaround is to use &lt;nowiki&gt;#&lt;/nowiki&gt; etc. or the numeric character references (NCR) &amp;#35; &amp;#42; &amp;#58; &amp;#59;, or to insert a zero-width non-breaking space or similar like &amp;#65279;. This depends on whether there is any further processing of the expanded wikitext, and if so, whether this is sensitive to this change.

For a conditional anchor in a link the  can be made unconditional, without any text after it it has no effect.

See also:
 * 12974

Using a parameter tag to avoid an automatic newline
A string starting with "*", "#", ":", or ";", not being prefixed by a newline, can only be produced in two ways: by explicitly putting at least the first character in the wikitext, or by using a parameter tag:
 * gives a;b if parameter p has value ";".
 * gives ab if parameter p is undefined.

This does not allow control on the page itself, but only when it is transcluded or substituted. If the template call contains one or more parameter definitions, and p is an expression that may evaluate to one of the parameter names, this provides branching with ";" etc. without newline as possible result.

For example, the template call is. If p evaluates to 1 or 2,  gives x or y respectively, otherwise q.

Typically the template call has to contain parameter definitions which do not represent choices of the end user. Therefore, to avoid bothering the end user with them, this is a call of an auxiliary template, while an outer template provides this call, while additionally passing on its own parameters.

For example, the template call is. If  evaluates to 1 or 2,   gives x or y respectively, if it evaluates to "n" it gives , and otherwise it gives q. In this example the case that   evaluates to "n" would typically be avoided, the result being an unintended side effect. For an example, see Template:Chr7a with auxiliary template Template:Chr7a/1. With the parameter name n there is no clash with the parameters with the implicit and explicit numerical names 0 - 99, and the result of Template:Digit is also numerical, so not equal to the character n.

Trimming on expansion
Spaces, tabs and newlines are stripped from:
 * the start and end of the expanded wikitext of a title, i.e., the start and end of the expanded wikitext of the name of a template, variable, and parameter, the start of the name a parser function, and the end of the first parameter of a parser function
 * the start and end of the expanded wikitext of the value of a named parameter
 * the start and end of the expanded wikitext of the value of a parameter of a parser function (for the end of the first parameter of a parser function this was already mentioned above)

Spaces, tabs and newlines are preserved:
 * between the name of a parser function and the colon (with the effect that with spaces or newlines it does not work!)
 * at the start and end of the expanded wikitext of the value of an unnamed template parameter
 * at the start and end of the expanded wikitext of the default value of a template parameter
 * at the start and end of the expanded wikitext of a template

For example:
 * gives "" [{{fullurl:special:ExpandTemplates|input=%22%7B%7B{{urlencode:1x{{!}}1= a } }}%7D%22}}]
 * gives "" [{{fullurl:special:ExpandTemplates|input=%22%7B%7B{{urlencode:1x{{!}}1= a } }}%7D%22}}]

gives ""

Of course, in a chain of operations, preservation requires preservation in every operation. For example, using and,  pq gives

pq

due to the three preservation rules, while pq, pq, and pq give pq, pq, and pq, because in each case somewhere in the expansion chain the newlines are stripped. Similarly, using, pq gives pq, while pq, pq, and pq give pq, pq, and pq.

Stripping of spaces and newlines can be inconvenient, but also convenient, it allows easy formatting of the wikitext without affecting the result.

" gives ""

"" gives ""

Preservation of spaces and newlines using unnamed parameters
For branching with preservation of spaces and newlines using unnamed parameters, seemingly trivial templates such as Template:Void and parameter selection templates can be used. This is for example done in various branching templates, e.g. Template:If.

Parameter selection templates


gives

Thus, both from index values and from results, spaces are stripped.

As a workaround one can use parameter selection templates Pn:

" " gives ""

This technique is used in Template:Sw (which can be extended to cover more cases):

" " gives "".

The allowed intermediate values are the positive integers for which there is a corresponding template Pn:

P1 P2 P3 P4 P5 P6 P7 P8 P9 P10 P11 P12 P13 P14 P15 P16 P17 P18 P19 P20 P21 P22 P23 P24 P25 P26 P27 P28 P29 P30 P31 P32 P33 P34 P35 P36 P37 P38 P39 P40 P41 P42 P43 P44 P45 P46 P47 P48 P49 P50 P51 P52 P53 P54 P55 P56 P57 P58 P59 P60 P61 P62 P63 P64 P65 P66 P67 P68 P69 P70 P71 P72 P73 P74 P75 P76 P77 P78 P79 P80 P81 P82 P83 P84 P85 P86 P87 P88 P89 P90 P91 P92 P93 P94 P95 P96 P97 P98 P99 P100

Alternatively one of the generic templates Template:Pp and Template:Ppp is used, see Help:Array.

Other parser functions
Thus inner newlines become %0A and inner spaces become +.

Conditional table rows
See Help:Table.

Alternatives
Depending on the application, an alternative for a pure newline or blank space in the expanded wikitext can be used, e.g.: Also the space etc. can be put between text and.
 * &amp;nbsp; or &amp;#160; (non-breaking space)
 * &amp;#32; (normal space)
 * &lt;br />