| Bytes | Lang | Time | Link |
|---|---|---|---|
| 007 | Thunno 2 N | 230718T140729Z | The Thon |
| 012 | MATL | 170409T023758Z | Luis Men |
| 005 | 05AB1E | 170408T204605Z | Emigna |
| 094 | Swift | 170408T201737Z | Mr. Xcod |
| 021 | Ruby | 170509T195724Z | Cyoce |
| 065 | C | 170409T103537Z | Steadybo |
| 016 | Actually | 170509T204136Z | user4594 |
| 016 | Juby | 170509T195515Z | Cyoce |
| 010 | Pyth | 170408T200910Z | KarlKast |
| 011 | CJam | 170409T024509Z | Luis Men |
| 062 | UberGenes | 170507T033425Z | quintopi |
| 028 | Cubix | 170427T220102Z | Luke |
| 040 | Tcl | 170425T214737Z | sergiol |
| 040 | Tcl | 170425T091543Z | sergiol |
| 034 | Mathematica | 170425T021841Z | Not a tr |
| 005 | Emacs | 170409T024815Z | user6213 |
| 022 | Carrot | 170417T074240Z | user4180 |
| 006 | Japt | 170413T182656Z | Oliver |
| 103 | BrainFlak | 170410T135326Z | Riley |
| 006 | Vim | 170409T224158Z | daniero |
| 061 | Common Lisp | 170409T171612Z | djeis |
| 006 | Charcoal | 170411T022359Z | ASCII-on |
| 003 | V | 170410T171630Z | DJMcMayh |
| 004 | V | 170408T194348Z | user4180 |
| 035 | C# | 170410T105554Z | TheLetha |
| 041 | REXX | 170410T121229Z | idrougge |
| 058 | Java 7 | 170410T110614Z | Kevin Cr |
| 024 | jq | 170409T170109Z | manatwor |
| 016 | PHP | 170410T094438Z | Christop |
| 009 | sed | 170409T033542Z | Max Mikh |
| nan | 170409T170555Z | Brad Gil | |
| 013 | Gema | 170409T165546Z | manatwor |
| 057 | brainfuck | 170409T164357Z | Erik the |
| 014 | GolfScript | 170409T092300Z | Erik the |
| nan | Perl 5 | 170408T211854Z | Dada |
| 017 | Octave | 170409T044809Z | rahnema1 |
| 007 | Crayon | 170408T203816Z | ETHprodu |
| 026 | JavaScript | 170408T201326Z | fəˈnɛtɪk |
| 039 | Python | 170408T195841Z | Jonathan |
| 028 | PowerShell | 170408T194705Z | colsw |
| 008 | Jelly | 170408T193811Z | Jonathan |
| 031 | Cheddar | 170408T201516Z | Downgoat |
| 013 | Stacked | 170408T201025Z | Conor O& |
| 043 | PHP | 170408T200156Z | Jör |
| 045 | Python 2 | 170408T194254Z | hyper-ne |
| 021 | Röda | 170408T195457Z | fergusq |
| 008 | Retina | 170408T193120Z | Martin E |
Thunno 2 N, 7 bytes
¶/ð4×s+
Explanation
¶/ð4×s+ # Implicit input
¶/ # Split on newlines
ð4× # Push four spaces
s+ # Prepend it to each
# Join on newlines
# Implicit output
MATL, 12 bytes
10&Yb"4Z"@gh
Input is a string with newlines. To enter this, you need to concatenate character 10 between the normal characters to represent newline (square brackets are concatenattion):
['Code' 10 'More code']
Try it at MATL online!
Explanation
10&Yb % Implicit input. Split at char 10 (newline). Gives cell array of strings
" % For each
4Z" % Push string of 4 spaces
@g % Push the contents of current cell array, i.e. a string with one of the
% original lines
h % Concatenate the two strings horizontally
% Implicit end. Implicit display
05AB1E, 7 5 bytes
Saved 2 bytes thanks to kalsowerus
¶¡4ú»
Explanation
¶¡ # split input on newline
4ú # prepend 4 spaces to each
» # join by newline
Swift, 94 bytes
func f(g:String){print(g.components(separatedBy:"\n").map{" "+$0}.reduce("",{$0+"\n"+$1}))}
This is a function, that can be used like this:
f(g:"YOUR\nSTRING")
Ruby, 22 21 Bytes
1 Byte saved thanks to @manatwork
->s{s.gsub /^/,' '*4}
Takes s, replaces all occurences of /^/ (start of line) with four spaces.
Actually, 16 bytes
9uc;§s⌠' 4*+⌡M@j
Explanation:
9uc;§s⌠' 4*+⌡M@j
9uc; push two newlines
§s raw input, split on newlines
⌠' 4*+⌡M for each line:
' 4*+ prepend 4 spaces
@j join with newlines
J-uby, 17 16 Bytes
~:gsub&' '*4&/^/
Explanation
~:gsub # :gsub with reversed arguments:
# (f)[regex,sub,str] == str.gsub(regex, sub)
&' '*4 # replace with four spaces
&/^/ # match the start of each line
This directly translates to (in Ruby):
->s{s.gsub(/^/,' '*4)}
Pyth, 10 Bytes
jm+*4\ d.z
If input as a list of lines would be allowed, I could do it in 7 bytes:
jm+*4\
longer solutions:
12 Bytes:
+*4d:Eb+b*4d
12 Bytes:
+*4dj+b*4d.z
13 Bytes:
t:E"^|
"+b*4d
CJam, 11 bytes
Thanks to @Challenger5 for a correction
qN/{S4*\N}%
Explanation
q e# Read whole input as a string with newlines
N/ e# Split at newlines, keeping empty pieces. Gives an array of strings
{ }% e# Map this function over the array of strings
e# The current string is automatically pushed
S4* e# Push a string of four spaces
\ e# Swap. Moves the original string after the four spaces
N e# Push a newline
e# Implicity display stack contents
UberGenes, 62 bytes
I had to enter this challenge with UberGenes, as a very similar program (that only inserted one space) was one of the first programs I ever wrote in the language, and it seemed like it would be easy to modify for this purpose.
=aA=p9=z4=cI=AC+a1-z1:pz=Ao:CA:Ii =b5+b5-bA+a1=d3*d7:db=i0
How it works:
=aA Set a to 61
(Begin main loop)
=p9 Set p to 9
=z4 z counts spaces
=cI Set c to 61
(Jumping to p jumps here)
=AC Put the space at position 61
at position a.
+a1-z1 Move a right and decrement z
:pz Jump to p if z is nonzero
(Jumping to d jumps here)
=Ao Read a character to position a.
:CA Jump to position 32+3 if input
was nonzero.
:Ii Otherwise, jump to position 61,
causing the entire string
that begins there to be
printed before halting.
(This is position 32+3=35)
=b5+b5 Set b to 10 (newline).
-bA Subtract the input character to
compare it with newline.
+a1 Move a right.
=d3*d7 Set d to 21
:db Jump to d if not newline.
=i0 Jump back to begin main loop.
(The 3 spaces at the end position a space character at position 61 so that, after =cI,
C refers to the space character--it will also be the first space printed.)
Cubix, 28 bytes
v.@>i??soS4.^(s<.....u!-Ns<v
Try it online!
Net form:
v . @
> i ?
? s o
S 4 . ^ ( s < . . . . .
u ! - N s < v . . . . .
. . . . . . . . . . . .
. . .
. . .
. . .
Explanation coming soon...
Tcl, 40
puts [regsub -all ^ [read stdin] " "]
demo — Notes:
- Avoid to press backspace key, it will execute the browser's History back.
- To end the input press
Ctrl+Dafter you had input all lines (you may have to press two times has it tends to get absorbed has a browser keybinding). If you paste the on a Windows interpreter, useCtrl+Z+Enterto mark end of input. - Pressing arrow keys will make appear strange chars instead of moving cursor.
- Yes, online interpreters really suck
- The rest of code is only for input and output, does not really process the string
Mathematica, 34 bytes
(Needs version 10.)
StringReplace[StartOfLine->" "]
Explanation: A string replacement operator where the start of each line is turned into " ".
Emacs, 5 keychords, 5 bytes
C-x h M-4 C-x tab
In at least one commonly used encoding for keyboard entry, each of these keychords is a single byte: 18 68 b4 18 09. Emacs entries tend to be very keychord-heavy, as each printable ASCII character stands for itself except as a subsequent character of a multi-character command (meaning that only keychords can be used to give actual commands).
I'm not sure how this compares to Vim (as opposed to V). But Vim is fairly commonly used on PPCG, and so I thought the other side of the editor wars deserves its time in the spotlight too.
This assumes that I/O is done via the buffer (the equivalent of the normal I/O conventions for vim), or taken from a file and output onto the screen (which comes to the same thing). If you do I/O via the region instead, which is natural for some forms of program, you can remove the leading two characters, for a score of 3 bytes; however, I don't think that complies with PPCG rules.
Explanation
C-x h M-4 C-x tab
C-x h Specify the entire buffer as the region
M-4 Give the argument 4 to the next command that runs
C-x tab Increase the indentation level of each line by a constant
The last builtin used here is, of course, incredibly useful for this challenge; the rest is just structure.
Carrot, 22 bytes, non-competing
Non-competing because of a bug with the website not showing leading whitespace in the output that was fixed recently
#^//^.*/gmS"
"
(note the leading spaces on each of the lines)
Try it online here.
Explanation
First we are in caret mode, where every character gets pushed to the stack (which is just a string). The four leading spaces on the first line push these spaces to the stack. Then the # pushes all of the input to the stack. Now the first line has four leading spaces. And the ^ takes us out of caret mode.
Next comes the / operator. The function of each operator depends on the argument it takes. In this case, the operator takes in a regex as its argument. The / operator with a regex argument returns the matches of the regex. The regex is /^.*/gm, this matches every line in the stack (except for the newlines). Now the stack turns from being a string into an array that contains the matches.
The S operator with a string argument joins the elements of the stack array with the argument. So in this case, we are joining the matches with this string, \n .
To simplify what was done here, the program prepends 4 spaces to the input and then substitutes newlines with a newline and 4 spaces (I really need to add a substitution operator to Carrot).
Japt, 7 6 bytes
Saved 1 byte thanks to @ETHproductions
miS²²R
Explanation:
miS²²R
m // At each char in the input:
iS²² // Prepend " " repeated 4 times
R // Rejoin with newlines
Brain-Flak, 109 103 bytes
-6 thanks to Wheat Wizard
Includes +1 for -c
((()()()()()){}){(({}<>)[()()((()()()()){})]<(((((({}){}){}))))>){(<{}{}{}{}{}>)}{}<>}<>{({}<>)<>}<>{}
((()()()()()){}) # Add a newline to the beginning
# This is needed to get spaces infront of the first line)
{ # For every character (call it C)
(({}<>) # Move C to the other stack
[()()((()()()()){})] # Push 8 and subtract 10 (\n) from C
<(((((({}){}){}))))>) # Push 4 spaces using the 8 from earlier
) # Push C - 10
{(< # If C - 10 != 0...
{}{}{}{}{} # Pop the 4 spaces that we added
>)}{} # End if
<> # Switch stacks to get the next character
} # End while
<>{({}<>)<>}<> # Reverse the stack (back to the original order)
{} # Pop the newline that we added
Vim, 6 keystrokes
<Ctrl-V>G4I <Esc>
Assumes that the cursor is on the beginning of the file, as if you opened the file from from the command line via vim filename.
<Ctrl-V> " Enter visual block move (enables rectangular selection)
G " Move to bottom line (selecting the entire first column)
4 " Repeat the following action 4 times
I " Insert at start of (each selected) line
" [input a space]
<Esc> " Exit insert mode
With a vim configured to use 4 spaces for indentation it would be 2 keystrokes: >G.
Common Lisp, 65 61 bytes
-4 thanks to Julian
(loop for l =(read-line t nil)while l do(format t"~4t~A~%"l))
Reads from *standard-input* (stdin, by default) and writes four spaces followed by each line to *standard-output* (stdout, by default).
Ungolfed
(loop :for l := (read-line t nil) :while l :do (format t"~4t~A~%" l))
Explanation
(read-line t nil)
Read a line from *standard-input* (shorthanded to t), not raising an error on EOF (the EOF return value is optional and defaults to nil). The line is returned as a string without the trialing "\n".
(format t "~4t~A~%" l)
Prints out formatted text to *standard-output* (shorthanded to t).
"~4t~A~%" is a formatting string. ~4t moves to the fourth column (prints 4 spaces), ~A prints out an argument to format, ~% prints out a newline.
(loop :for l := ... :while l :do ...)
Each time through the loop, assign l to the result of reading in a line of text, break the loop if there was no line to read, and print out the line with four spaces prepended.
Charcoal, 6 bytes
PSM⁴←
Note: There is a space at the end
Explanation
PS Print next input as string
M⁴← Move 4 cells left
(it's a space) Print space
V, 3 bytes (Non-competing)
4>G
This is answer uses a feature that I have been planning on adding for a while, but just got around to adding today. That makes this answer non-competing and invalid for winning. But it's still cool to show off such a useful/competitive feature!
Explanation:
4> " Add an indent of 4 to...
G " Every line from the current line (0 by default) to the end of the buffer
V, 4 bytes
Î4É
(Note the trailing space)
V is encoded in Latin1, where this is encoded like so:
00000000: ce34 c920 .4.
Explanation
Î " On every line
4É<space> " Prepend 4 spaces
Here's a solution that is also 4 bytes in UTF-8!
VG4>
VG " Select everything
> " Indent
4 " 4 times (with spaces)
C#, 46 35 bytes
s=>" "+s.Replace("\n","\n ");
REXX, 41 bytes
do while lines()>0
say ' 'linein()
end
Reads from stdin, writes to stdout.
Java 7, 58 bytes
String c(String s){return" "+s.replace("\n","\n ");}
Explanation:
- Append with four leading spaces
- Replace every new-line for a new-line + four spaces
jq, 24 characters
(19 characters code + 5 characters command line options)
(./"\n")[]|" "+.
Sample run:
bash-4.3$ jq -sRr '(./"\n")[]|" "+.' <<< $'Code\nMore code'
Code
More code
jq, 13 characters
(8 characters code + 5 characters command line options)
If we can rely on the interpreter's line oriented processing mode.
" "+.
bash-4.3$ jq -Rr '" "+.' <<< $'Code\nMore code' Code More code
PHP, 16
echo" $argn";
run with php -R <code>. -R runs the given code for every input line and $argn is fed the current input line. So this simply prints each line with additional four spaces in front of it.
sed, 16 10 9 bytes
s/^/ /
Edits
Reduced solution size from 16 to 10 bytes thanks to Kritixi Lithos.
-1 byte thanks to seshoumara.
Perl 6, 11 bytes
*.indent(4)
Expanded:
*\ # declare a WhateverCode lambda/closure (this is the parameter)
.indent( # call the `indent` method on the argument
4 # with the number 4
)
Gema, 13 characters
/.*/=\ $0
Sample run:
bash-4.3$ gema '/.*/=\ $0' <<< $'Code\nMore code'
Code
More code
brainfuck, 57 bytes
++++[>>++++++++<<-]>+<,[>[->....<]+<.----------[>-<[-]],]
You need to use \n for newlines, although they get parsed as actual newlines (input field at the bottom).
Set EOF = \0 (go to Options > End of input > char = \0).
GolfScript, 14 bytes
n/{' '4*\+}%n*
Note: if trailing newline is disallowed, append :n; for 17 bytes.
Perl 5, 11+1 = 12 bytes
11 bytes of code + -p flag.
s/^/ /mg
For once, explanations will be short: The regex replaces each beginning of line (^ combined with /m modifier) by four spaces - the end.
Crayon, 7 bytes
`¤q;3xq
Explanation
Crayon is a stack-based language designed for creating ASCII-art. It's still in the early stages of development, but it knows just enough to finish this challenge with a rather low byte count:
Implicit: input string is on the stack
`¤ Push a non-breaking space to the stack.
q; Draw this at the cursor (0,0 by default) and pop it.
3x Move three more spaces to the right.
q Draw the input string here (at 4,0).
Implicit: output the canvas, trimmed to a rectangle
Drawing the non-breaking space is necessary because Crayon automatically trims the output to a rectangle, so without the NBSP it would just print the original input.
JavaScript, 26 bytes
Thanks @Conor O'Brien for golfing off 8 bytes
x=>x.replace(/^/gm," ")
Replace with a regex with /g replaces all instances. m makes the regex treat each line separately for the start of the string ^.
Python, 44 39 bytes
Crossed out 44 is no longer 44 :)
-5 bytes thanks to ovs (avoid dequeue with a prepend)
lambda s:' '*4+s.replace('\n','\n ')
PowerShell, 29 28 Bytes
"$args"-split"
"|%{" "*4+$_}
-1 Thanks to fergusq, using an actual newline instead of the `n
takes the "$args" input as a string (cast using "s) and -splits it on a newline, then loops (%{}) through it, appending four spaces (" "*4) and the line ($_) then outputs it implicitly.
Jelly, 8 bytes
Ỵṭ€⁶ẋ4¤Y
How?
Ỵṭ€⁶ẋ4¤Y - Main link: string
Ỵ - split on newlines
¤ - nilad followed by ink(s) as a nilad:
⁶ - a space character
ẋ4 - repeated four times
ṭ€ - tack for €ach
Y - join with newlines
Some other 8 byte variants are:
Ỵṭ€⁶Yµ4¡ (4 repeats of split on newlines, tack a single space);
⁶ḤḤ;ЀỴY (doubling twice is like multiplying by 4, Ѐ maps over the right argument, so we can concatenate instead of tacking);
and other rearrangements thereof.
Cheddar, 31 bytes
@.lines.map((" ":+)).asLines
Really simply, but I posted because it shows off the new functional operators.
(" ":+) is the same as A -> " " + A. (i.e. + op as a function with " " bound to LHS).
I don't think even needs explanation
Stacked, 13 bytes
'^'4' '*mrepl
Explanation
'^'4' '*mrepl (* input: top of stack *)
mrepl perform multiline regex replacements,
'^' replacing /^/ with
4' '* four spaces
PHP, 43 Bytes
<?=" ".strtr($_GET[0],["\n"=>"\n "]);
Python 2, 87 45 bytes
print' '*4+'\n '.join(input().split('\n'))
Input is taken as 'Line1\nLine2\nLine3...' (Quotes necessary)
Thanks to @WheatWizard for giving me an idea that helped me golf 42 bytes.
Röda, 21 bytes
{(_/"
")|[` $_
`]}
It is an anonymous function. The input is pulled from the stream.
Explanation:
{
(_/"\n") | /* Splits the input at newlines */
[" ".._.."\n"] /* For each line, prints four spaces before the line */
}
Retina, 8 bytes
%`^
There are four spaces on the second line. Alternative solutions use either m`^ or %1` or 1%` on the first line. All of these match the position at the beginning of each line and replace it with four spaces.