g | x | w | all
Bytes Lang Time Link
007Thunno 2 N230718T140729ZThe Thon
012MATL170409T023758ZLuis Men
00505AB1E170408T204605ZEmigna
094Swift170408T201737ZMr. Xcod
021Ruby170509T195724ZCyoce
065C170409T103537ZSteadybo
016Actually170509T204136Zuser4594
016Juby170509T195515ZCyoce
010Pyth170408T200910ZKarlKast
011CJam170409T024509ZLuis Men
062UberGenes170507T033425Zquintopi
028Cubix170427T220102ZLuke
040Tcl170425T214737Zsergiol
040Tcl170425T091543Zsergiol
034Mathematica170425T021841ZNot a tr
005Emacs170409T024815Zuser6213
022Carrot170417T074240Zuser4180
006Japt170413T182656ZOliver
103BrainFlak170410T135326ZRiley
006Vim170409T224158Zdaniero
061Common Lisp170409T171612Zdjeis
006Charcoal170411T022359ZASCII-on
003V170410T171630ZDJMcMayh
004V170408T194348Zuser4180
035C#170410T105554ZTheLetha
041REXX170410T121229Zidrougge
058Java 7170410T110614ZKevin Cr
024jq170409T170109Zmanatwor
016PHP170410T094438ZChristop
009sed170409T033542ZMax Mikh
nan170409T170555ZBrad Gil
013Gema170409T165546Zmanatwor
057brainfuck170409T164357ZErik the
014GolfScript170409T092300ZErik the
nanPerl 5170408T211854ZDada
017Octave170409T044809Zrahnema1
007Crayon170408T203816ZETHprodu
026JavaScript170408T201326Zfəˈnɛtɪk
039Python170408T195841ZJonathan
028PowerShell170408T194705Zcolsw
008Jelly170408T193811ZJonathan
031Cheddar170408T201516ZDowngoat
013Stacked170408T201025ZConor O&
043PHP170408T200156ZJör
045Python 2170408T194254Zhyper-ne
021Röda170408T195457Zfergusq
008Retina170408T193120ZMartin E

Thunno 2 N, 7 bytes

¶/ð4×s+

Try it online!

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ú»

Try it online!

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.

C, 66 65 bytes

p(){printf("    ");}f(char*s){for(p();*s;)putchar(*s++)-10||p();}

Try it online!

Actually, 16 bytes

9uc;§s⌠' 4*+⌡M@j

Try it online!

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

Try it!

If input as a list of lines would be allowed, I could do it in 7 bytes:

jm+*4\ 

Try that

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}%

Try it online!

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

Now as a function:

proc p t {regsub -all -line ^ $t "    "}

demo

Did not save bytes after all!

Tcl, 40

puts [regsub -all ^ [read stdin] "    "]

demo — Notes:

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

Try it online!

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

((()()()()()){}){(({}<>)[()()((()()()()){})]<(((((({}){}){}))))>){(<{}{}{}{}{}>)}{}<>}<>{({}<>)<>}<>{}

Try it online!

((()()()()()){})        # 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⁴← 

Try it online!

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!

Try it online!

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É 

Try it online!

(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:

Try it here.

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

On-line test

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

On-line test

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/^/    /

Try it online!

Edits

Reduced solution size from 16 to 10 bytes thanks to Kritixi Lithos.

-1 byte thanks to seshoumara.

Perl 6, 11 bytes

*.indent(4)

Try it

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

++++[>>++++++++<<-]>+<,[>[->....<]+<.----------[>-<[-]],]

Online interpreter

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*

Try it online!

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

Try it online!

For once, explanations will be short: The regex replaces each beginning of line (^ combined with /m modifier) by four spaces - the end.

Octave, 17 bytes

@(s)["    "';s']'

Try it online!

Crayon, 7 bytes

`¤q;3xq

Try it online!

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 ^.

Try it online!

Python,  44  39 bytes

Crossed out &nbsp;44&nbsp; is no longer 44 :)

-5 bytes thanks to ovs (avoid dequeue with a prepend)

lambda s:' '*4+s.replace('\n','\n    ')

Try it online!

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

Try it online!

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

Try it online!

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

{(_/"
")|[`    $_
`]}

Try it online!

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

%`^

Try it online!

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.