18
\$\begingroup\$

When using Markup, like on the SE network, an indentation of four spaces before a line of text denotes it as part of a code block, as I hope you know. If you don't, here's an example (with . representing a space):

....Code
....More code

results in

Code
More code

The problem is, when you copy-paste code into an answer, you need to indent each line manually. This is especially difficult when working with ungolfed code, as it's likely already indented and can cause confusion. You can just select your code and hit Ctrl + K, it turns out. Hours of my life wasted for no reason aside...

So, your goal is, given an input, return it with four spaces before each line. In the spirit of saving time copy-pasting, you are to process the entire input as a single string (so long as your language can parse it). If your language is unable to process a character (such as newlines) in strings, you may assume it is denoted/escaped through some other method supported by the language; however, the output must output each line on its own line (so no passing something like ....foo\n....bar).

Standard loopholes not allowed. As this is , the shortest answer in bytes wins. Good luck!

\$\endgroup\$
  • 3
    \$\begingroup\$ "you need to indent each line manually" (or select the text and click the button :)) \$\endgroup\$ – Jonathan Allan Apr 8 '17 at 19:29
  • 11
    \$\begingroup\$ @JonathanAllan "Button"? Surely you mean "keyboard shortcut". (Ctrl+K) \$\endgroup\$ – Martin Ender Apr 8 '17 at 19:29
  • 31
    \$\begingroup\$ @JonathanAllan ...I... I am very upset. SO MUCH TIME. WASTED. \$\endgroup\$ – Papayaman1000 Apr 8 '17 at 19:31
  • 6
    \$\begingroup\$ While I'm fairly confident that Kritixi's V answer won't be beaten, I would typically recommend waiting a bit longer before accepting an answer, because accepting so early poses a disadvantage to people who could answer with a shorter answer but weren't on the site at the time (timezones or just not always being on PPCG 24/7) \$\endgroup\$ – HyperNeutrino Apr 8 '17 at 20:12
  • 2
    \$\begingroup\$ +1 for informing people about Ctrl + K \$\endgroup\$ – Koishore Roy Apr 9 '17 at 9:54

46 Answers 46

17
\$\begingroup\$

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)
\$\endgroup\$
  • 2
    \$\begingroup\$ Hope somebody picks up that phone, cause wow did someone call it. \$\endgroup\$ – Papayaman1000 Apr 8 '17 at 19:45
  • \$\begingroup\$ Alternate solution: 4ñ>G \$\endgroup\$ – DJMcMayhem Apr 8 '17 at 20:07
  • \$\begingroup\$ @DJMcMayhem But it uses tabs to indent instead of spaces \$\endgroup\$ – Cows quack Apr 8 '17 at 20:09
  • \$\begingroup\$ In vim, yes. In V, no, it's 4 spaces \$\endgroup\$ – DJMcMayhem Apr 8 '17 at 20:10
  • \$\begingroup\$ @DJMcMayhem Thanks, that inspired another alternative solution that is only 4 bytes in UTF-8! \$\endgroup\$ – Cows quack Apr 8 '17 at 20:17
9
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ Would Crayon let you do the opposite: output the string, then move four spaces to the left and output a nbsp? That's likely to cost less in stack manipulation, although I don't know whether Crayon would correctly move the canvas into the right place. \$\endgroup\$ – user62131 Apr 9 '17 at 13:26
  • \$\begingroup\$ @ais523 Hmm, that's a really good idea... unfortunately, that'd require moving to x=-4, which is not an easy task at the moment. I really should push these changes I've been working on for a month... :P \$\endgroup\$ – ETHproductions Apr 9 '17 at 18:50
7
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ I suspected a Retina answer would be first. \$\endgroup\$ – Neil Apr 8 '17 at 19:31
  • \$\begingroup\$ Well that was fast. \$\endgroup\$ – Papayaman1000 Apr 8 '17 at 19:32
  • \$\begingroup\$ @Neil I'll be surprised if this doesn't get beaten by V (or even raw Vim). :) \$\endgroup\$ – Martin Ender Apr 8 '17 at 19:32
  • 1
    \$\begingroup\$ @MartinEnder Here ya go (4 bytes in V): codegolf.stackexchange.com/a/115870/41805 :) \$\endgroup\$ – Cows quack Apr 8 '17 at 19:44
7
\$\begingroup\$

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

\$\endgroup\$
  • \$\begingroup\$ Oh did you change how the parser works? From what I remember : would cause problems with ?: \$\endgroup\$ – Conor O'Brien Apr 8 '17 at 20:17
  • \$\begingroup\$ @ConorO'Brien I kinda forgot how I fixed it but I believe that because there's a : with no matching ?, the parser will choose to treat it as a functional op. This still requires the functional op to be wrapped in parens but yeah \$\endgroup\$ – Downgoat Apr 8 '17 at 20:20
  • \$\begingroup\$ +1 for smiley :+) \$\endgroup\$ – LarsW Apr 8 '17 at 23:17
  • \$\begingroup\$ What does @ mean? \$\endgroup\$ – Leaky Nun Apr 27 '17 at 10:10
6
\$\begingroup\$

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!

\$\endgroup\$
  • \$\begingroup\$ lambda s:' '*4+s.replace('\n','\n ') for 39 bytes \$\endgroup\$ – ovs Apr 8 '17 at 20:12
  • \$\begingroup\$ @ovs - I don't see it... do you mean lambda s:' '*4+s.replace('\n','\n ')[1:] for 40 (which does not work) or something else? \$\endgroup\$ – Jonathan Allan Apr 8 '17 at 20:16
  • 1
    \$\begingroup\$ Just lambda s:' '*4+s.replace('\n','\n<4 spaces>') TIO \$\endgroup\$ – ovs Apr 8 '17 at 20:28
  • \$\begingroup\$ @ovs Ah yes, of course (the comment four space markdown rendering threw me and I didn't notice it in my reply either) thanks for the saving! \$\endgroup\$ – Jonathan Allan Apr 8 '17 at 20:30
6
\$\begingroup\$

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!

\$\endgroup\$
  • \$\begingroup\$ It seems to jumble non-whitespace in the input into asdf repeated over and over. \$\endgroup\$ – Papayaman1000 Apr 8 '17 at 20:16
  • \$\begingroup\$ You should note what I had put as the input in TIO \$\endgroup\$ – fəˈnɛtɪk Apr 8 '17 at 20:17
  • \$\begingroup\$ You can save a few bytes by doing x=>x.replace(/^|\n/g,"$&    ") to get the first line and the following lines all in one go \$\endgroup\$ – ETHproductions Apr 8 '17 at 20:18
  • 1
    \$\begingroup\$ 26 bytes: Try it online! \$\endgroup\$ – Conor O'Brien Apr 8 '17 at 20:19
  • 1
    \$\begingroup\$ Or ^ works too I guess ;-) \$\endgroup\$ – ETHproductions Apr 8 '17 at 20:20
4
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ I grew up on python. That's what made me think this would be sort of hard. Even ignoring RegEx, I was so wrong, turns out. \$\endgroup\$ – Papayaman1000 Apr 8 '17 at 19:44
  • \$\begingroup\$ @Papayaman1000 It's a fairly trivial challenge, both with and without RegEx, though I find it to be very interesting. \$\endgroup\$ – HyperNeutrino Apr 8 '17 at 19:45
  • 2
    \$\begingroup\$ I will admit, since I didn't know about Ctrl + K, the real reasons for this challenge being proposed were... less than for just pure puzzling. \$\endgroup\$ – Papayaman1000 Apr 8 '17 at 19:47
  • \$\begingroup\$ @Papayaman1000 Haha, yes, it was quite annoying having to press space 4 times in front of each line. Especially since I usually use Python and so I have multiple lines all the time (it's not so bad when I used to use Java). Eventually I just got lazy and used my text editor to replace ^ with ` `. \$\endgroup\$ – HyperNeutrino Apr 8 '17 at 19:49
  • 1
    \$\begingroup\$ @WheatWizard Thanks for the suggestion, I golfed it down a bit. Is it a bit better now? \$\endgroup\$ – HyperNeutrino Apr 8 '17 at 19:58
4
\$\begingroup\$

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.

\$\endgroup\$
4
\$\begingroup\$

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.

\$\endgroup\$
3
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ Can you use a newline character instead of `n? \$\endgroup\$ – fergusq Apr 8 '17 at 20:02
  • \$\begingroup\$ @fergusq indeed I can, updated. \$\endgroup\$ – colsw Apr 8 '17 at 20:27
  • \$\begingroup\$ Presumably the newline character is \r\n on windows which is still two bytes - or are there rules clarifying how many bytes a newline character takes? \$\endgroup\$ – poizan42 Apr 10 '17 at 1:58
  • \$\begingroup\$ @poizan42 I'm not sure if there's a meta post on it, but I can run this in the default console with just the newline so there's no reason to consider it invalid. \$\endgroup\$ – colsw Apr 10 '17 at 8:42
3
\$\begingroup\$

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
\$\endgroup\$
  • \$\begingroup\$ Got it down to 9: jbm*4\ .z \$\endgroup\$ – clap May 9 '17 at 0:43
  • \$\begingroup\$ @ConfusedMr_C That just overwrites every line with 4 spaces. \$\endgroup\$ – KarlKastor May 9 '17 at 19:11
  • \$\begingroup\$ I forgot the d, whoops. Your 10 byte answer is what I was going for, anyway \$\endgroup\$ – clap May 14 '17 at 20:59
2
\$\begingroup\$

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 */
}
\$\endgroup\$
  • \$\begingroup\$ Does identity() just pull all values from STDIN? \$\endgroup\$ – Cows quack Apr 8 '17 at 19:57
  • \$\begingroup\$ @KritixiLithos Yes. identity pulls values from the input stream and pushes them to its output stream. It is identical to push(x) for x. \$\endgroup\$ – fergusq Apr 8 '17 at 20:01
2
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ This reads input a line at a time, rather than a string as a whole. \$\endgroup\$ – user62131 Apr 9 '17 at 2:21
  • \$\begingroup\$ @ais523 Roughly half of the answers, including the top two, read input in the exact same way. \$\endgroup\$ – Maxim Mikhaylov Apr 9 '17 at 3:39
  • \$\begingroup\$ @ais523 I'd say it processes the input one line at a time, but it can read it as a whole string (if you supply it with <<< "..." for instance). Don't you agree? \$\endgroup\$ – Dada Apr 9 '17 at 8:21
  • \$\begingroup\$ @ais523 after thinking a bit more about it, I think you're right. (I've update my code accordingly) \$\endgroup\$ – Dada Apr 9 '17 at 8:40
2
\$\begingroup\$

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
)
\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ You can get to 15 bytes by using the -r flag (1 byte) so that you can remove the backslashes before the parentheses. \$\endgroup\$ – Cows quack Apr 9 '17 at 7:52
  • \$\begingroup\$ You can get to 13 by using s/.*/ &/ (remove the parentheses and replace \1 with &) \$\endgroup\$ – Cows quack Apr 9 '17 at 7:54
  • \$\begingroup\$ @KritixiLithos Thanks! It works even without *. \$\endgroup\$ – Maxim Mikhaylov Apr 9 '17 at 12:23
  • \$\begingroup\$ Or simply s:^: :, for 9 bytes. \$\endgroup\$ – seshoumara Apr 9 '17 at 17:53
  • \$\begingroup\$ @seshoumara I've never seen colons used this way in a sed script... Do you know which part of the manual describes this syntax? \$\endgroup\$ – Maxim Mikhaylov Apr 9 '17 at 18:37
2
\$\begingroup\$

Java 7, 58 bytes

String c(String s){return"    "+s.replace("\n","\n    ");}

Explanation:

Try it here.

  • Append with four leading spaces
  • Replace every new-line for a new-line + four spaces
\$\endgroup\$
  • \$\begingroup\$ I am forever sad that Java's regex mechanisms require other libraries for the most part. \$\endgroup\$ – Poke Apr 10 '17 at 17:30
  • \$\begingroup\$ I think you need replaceAll \$\endgroup\$ – Khaled.K May 9 '17 at 9:36
  • \$\begingroup\$ @Khaled.K Why? Both .replace and .replaceAll will replace all occurrences of the searched String with the replacement. .replace is used for literal Strings, and .replaceAll for regexes. Since \n isn't a regex, .replace can be used without a problem to replace all newlines with a newline + four spaces, which you can also check in the "Try it line" link I provided. \$\endgroup\$ – Kevin Cruijssen May 9 '17 at 11:31
2
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

PHP, 43 Bytes

<?="    ".strtr($_GET[0],["\n"=>"\n    "]);
\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

Octave, 17 bytes

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

Try it online!

\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
  • \$\begingroup\$ Neat! At least take some pride in the fact that your language took the top spot even beforehand [even if it is a dirty golfing language... nah, jk]! \$\endgroup\$ – Papayaman1000 Apr 11 '17 at 1:10
1
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ I'm pretty sure you can remove the ZZ at the end. Usually vim submissions are fine just outputting to the buffer rather than to a file. \$\endgroup\$ – DJMcMayhem Apr 10 '17 at 17:14
  • \$\begingroup\$ Alright thanks, I removed ZZ then. \$\endgroup\$ – daniero Apr 11 '17 at 17:53
1
\$\begingroup\$

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  
\$\endgroup\$
  • \$\begingroup\$ Nice job. S²² would work as well in place of Sp4, not that it saves you anything in this case. Speaking of which, I think you can just do miS²²R to remove the R flag (basically miS²², but split at newlines beforehand and join with newlines afterward) \$\endgroup\$ – ETHproductions Apr 15 '17 at 14:49
1
\$\begingroup\$

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.)
\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
  • 1
    \$\begingroup\$ Doesn't work on abc\n\ndef. It returns ....abc\n....def because % discards empty elements. You want to use / to split instead, because it keeps the empty elements. \$\endgroup\$ – Esolanging Fruit May 9 '17 at 1:40
  • \$\begingroup\$ @Challenger5 Thanks, corrected! \$\endgroup\$ – Luis Mendo May 9 '17 at 7:39
1
\$\begingroup\$

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)}
\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

C, 66 65 bytes

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

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Neat solution, but you could go with s;char*l;f(){while(getline(&l,&s,stdin)+1)printf("____%s",l);} which 62 bytes \$\endgroup\$ – Khaled.K May 9 '17 at 9:48
  • \$\begingroup\$ @Khaled.K Thanks, but that doesn't seem to work without including <stdio.h> (because of the stdin). \$\endgroup\$ – Steadybox May 9 '17 at 10:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.