Indent long line stdout

Related searches

Let's say I have a standard 80 columns terminal, execute command with long line output (i.e. stdout from ls) that splits into two or more lines, and want to indent the continuation line of all my bash stdout.

Indent should be configurable, 1 or 2 or 3 or whatever spaces.

from this

lrwxrwxrwx 1 root root 24 Feb 19 1970 sdcard -> /storage/emula ted/legacy/

to this

lrwxrwxrwx 1 root root 24 Feb 19 1970 sdcard -> /storage/emula ted/legacy/

Read this Indenting multi-line output in a shell script so I tried to pipe | sed 's/^/ /' but gives me the exact opposite, indents the first lines and not the continuation.

Ideally I would put a script in profile.rc or whatever so every time I open an interactive shell and execute any command long output gets indented .

I'd use awk for this.

awk -v width="$COLUMNS" -v spaces=4 '
  pad = sprintf("%*s", spaces, "") # generate `spaces` spaces
NF {                               # if current line is not empty
  while (length > width) {         # while length of current line is greater than `width`
    print substr($0, 1, width)     # print `width` chars from the beginning of it
    $0 = pad substr($0, width + 1) # and leave `pad` + remaining chars
  if ($0 != "")                    # if there are remaining chars
    print                          # print them too 
} 1' file

In one line:

awk -v w="$COLUMNS" -v s=4 'BEGIN{p=sprintf("%*s",s,"")} NF{while(length>w){print substr($0,1,w);$0=p substr($0,w+1)} if($0!="") print;next} 1'

As @Mark suggested in comments, you can put this in a function and add it to .bashrc for ease of use.

function wrap() {
  awk -v w="$COLUMNS" -v s=4 'BEGIN{p=sprintf("%*s",s,"")} NF{while(length>w){print substr($0,1,w);$0=p substr($0,w+1)} if($0!="") print;next} 1'


ls -l | wrap

Edit by Ed Morton per request:

Very similar to oguzismails script above but should work with Busybox or any other awk:

$ cat tst.awk
BEGIN { pad = sprintf("%" spaces "s","") }
    while ( length($0) > width ) {
        printf "%s", substr($0,1,width)
        $0 = substr($0,width+1)
        if ( $0 != "" ) {
            print ""
            $0 = pad $0
$ echo '123456789012345678901234567890' | awk -v spaces=3 -v width=30 -f tst.awk
$ echo '123456789012345678901234567890' | awk -v spaces=3 -v width=15 -f tst.awk
$ echo '' | awk -v spaces=3 -v width=15 -f tst.awk


That first test case is to show that you don't get a blank line printed after a full-width input line and the third is to show that it doesn't delete blank rows. Normally I'd have used sprintf("%*s",spaces,"") to create the pad string but I see in a comment that that doesn't work in the apparently non-POSIX awk you're using.

Wrap and indent text using coreutils, A very long line that I want to fold on the word boundary and indent as well Here's a shorter answer that uses fold then shifts its output by 11� Now i want to add new constraints that is if the string/line is longer than 78 characters, break and indent it where it makes sense so the line does not run longer than 78 characters. Example: This one is a very long line that runs off the right side because it is longer than 78 characters long It should then be

This might work for you (GNU sed):

sed 's/./&\n  /80;P;D' file

This splits lines into length 80 and indents the following line(s) by 2 spaces.

Or if you prefer:

s='  ' c=80
sed "s/./&\n$s/$c;P;D" file

To prevent empty lines being printed, use:

sed 's/./&\n/80;s/\n$//;s/\n    /;P;D' file

or more easily:

sed 's/./\n    &/81;P;D' file

indent(1) - Linux manual page, indent [options] [input-files] indent [options] [single-input-file] [-o output-file] indent -- -bbo, --break-before-boolean-operator Prefer to break long lines before� I have this really long line in Python, due to several reasons, that I'm trying to break up. long_variable_name = somelongmodule.SomeLongClassName.VeryLongFunctionName( I know the recommended way to break up a line is at the parentheses, but I haven't even gotten there yet.

One possible solution using pure bash string manipulation. You can make the script to read stdin and format whatever it read.

MAX_LEN=5  # length of the first/longest line
IND_LEN=2  # number of indentation spaces
while read line; do
    printf "${line:0:$MAX_LEN}\n"
    for i in $(seq $MAX_LEN $short_len ${#line}); do
        printf "%*s${line:$i:$short_len}\n" $IND_LEN

Usage: (assuming the script is saved as

$ echo '0123456789' | ./

How to wrap long lines in Python?, The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces. If necessary Make sure to indent the continued line appropriately. This will give the output: The output from all of the default MOTD files have two spaces at the start of each line so it looks nicely indented, Indent long line stdout. Related. 3744.

indent(1), indent [options] [single-input-file] [-o output-file]. indent --version -bbo, --break- before-boolean-operator: Prefer to break long lines before boolean operators. The top marker (down arrow) controls the first line indent, and the middle marker (up arrow) controls the hanging indent (the indent for lines after the first line in each paragraph). The bottom marker (small box) controls the left indent. To adjust these indents, you can click-and-drag each marker to the right or left.

Reformatting Paragraphs (Perl Cookbook, 2nd Edition), Your string is too big to fit the screen, and you want to break it up into lines of words, of lines, so it must somehow reformat the paragraphs when they're output. merge multiple lines into one, then wrap one long line use Text::Wrap; undef� Click the drop down menu under “Special”. Select “First Line” to automatically indent the first line of each new paragraph. 4 Enter the indent size.

This is a long line of text. debug_contents (indent=1, file=sys.stdout, _ids=None) ¶ This is a long line of text.

  • awk would seem to be the easiest way of doing this - consider adding that tag. You could pick up the width of the terminal with tput cols and filter with an awk script like awk w=$(tput cols) 'length($0)>w{print substr($0,1,w);print "____",substr($0,w+1);next}1' Then put that in a bash function for ease of use.
  • Just pipe the output to column -t.
  • As bash was tagged, you could probably: awk -v width=$COLUMNS ...?
  • I'm getting Unexpected token, Android 4.4 busybox 1.30.1
  • @JuTutt if it's an option install a POSIX compliant awk. Otherwise see Ed Morton's edit on my answer.
  • tried to make it in a one line function but no luckawk -v spaces=3 -v width=80 'BEGIN{pad=sprintf("%",spaces,"s","")} {while(length($0)>width){printf "%s",substr($0,1,width);$0=substr($0,width+1)};if($0!=""){print "" $0=pad $0}print}'
  • also made an alias in .bashrc alias ll='ls -Al | wrap', works with first @oguzismail, is there a way to append this wrap function to all commands without making an alias for everyone?
  • That would delete empty lines or lines of all blank chars. You're missing a ' at the end of the first script btw.
  • See for some of the reasons not to do this (including it will be extremely slow, will corrupt the text, can produce cryptic syntax errors, will behave different depending on the contents of the directory where you run it, and has security issues).
  • Some specific comments: Use lower case or mixed case variable names to avoid the risk of colliding with environment or shell variable names. Don't use seq unless you have to. Use C-style for loops instead: for ((i=max_len; i<=${#line}; i+=ind_len)). Don't put variables in the format argument of printf: printf '%*s%s\n' "$ind_len" '' "${line:$i:$short_len}" You can do math this way: (( short_len = max_len - ind_len )) which I find cleaner looking.