Skip to content
  • About
    • What is Symfony?
    • Community
    • News
    • Contributing
    • Support
  • Documentation
    • Symfony Docs
    • Symfony Book
    • Screencasts
    • Symfony Bundles
    • Symfony Cloud
    • Training
  • Services
    • Platform.sh for Symfony Best platform to deploy Symfony apps
    • SymfonyInsight Automatic quality checks for your apps
    • Symfony Certification Prove your knowledge and boost your career
    • SensioLabs Professional services to help you with Symfony
    • Blackfire Profile and monitor performance of your apps
  • Other
  • Blog
  • Download
sponsored by
  1. Home
  2. Documentation
  3. Console
  4. How to Color and Style the Console Output

How to Color and Style the Console Output

Edit this page

Symfony provides an optional console style to render the input and output of commands in a consistent way. If you prefer to apply your own style, use the utilities explained in this article to show colors in the command output (e.g. to differentiate between important messages, titles, comments, etc.).

Note

By default, the Windows command console doesn't support output coloring. The Console component disables output coloring for Windows systems, but if your commands invoke other scripts which emit color sequences, they will be wrongly displayed as raw escape characters. Install the Cmder, ConEmu, ANSICON, Mintty (used by default in GitBash and Cygwin) or Hyper free applications to add coloring support to your Windows command console.

Using Color Styles

Whenever you output text, you can surround the text with tags to color its output. For example:

1
2
3
4
5
6
7
8
9
10
11
// green text
$output->writeln('<info>foo</info>');

// yellow text
$output->writeln('<comment>foo</comment>');

// black text on a cyan background
$output->writeln('<question>foo</question>');

// white text on a red background
$output->writeln('<error>foo</error>');

The closing tag can be replaced by </>, which revokes all formatting options established by the last opened tag.

It is possible to define your own styles using the OutputFormatterStyle class:

1
2
3
4
5
6
7
use Symfony\Component\Console\Formatter\OutputFormatterStyle;

// ...
$outputStyle = new OutputFormatterStyle('red', '#ff0', ['bold', 'blink']);
$output->getFormatter()->setStyle('fire', $outputStyle);

$output->writeln('<fire>foo</>');

Any hex color is supported for foreground and background colors. Besides that, these named colors are supported: black, red, green, yellow, blue, magenta, cyan, white, gray, bright-red, bright-green, bright-yellow, bright-blue, bright-magenta, bright-cyan and bright-white.

Note

If the terminal doesn't support true colors, the given color is replaced by the nearest color depending on the terminal capabilities. E.g. #c0392b is degraded to #d75f5f in 256-color terminals and to red in 8-color terminals.

And available options are: bold, underscore, blink, reverse (enables the "reverse video" mode where the background and foreground colors are swapped) and conceal (sets the foreground color to transparent, making the typed text invisible - although it can be selected and copied; this option is commonly used when asking the user to type sensitive information).

You can also set these colors and options directly inside the tag name:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// using named colors
$output->writeln('<fg=green>foo</>');

// using hexadecimal colors
$output->writeln('<fg=#c0392b>foo</>');

// black text on a cyan background
$output->writeln('<fg=black;bg=cyan>foo</>');

// bold text on a yellow background
$output->writeln('<bg=yellow;options=bold>foo</>');

// bold text with underscore
$output->writeln('<options=bold,underscore>foo</>');

Note

If you need to render a tag literally, escape it with a backslash: \<info> or use the escape() method to escape all the tags included in the given string.

Displaying Clickable Links

Commands can use the special <href> tag to display links similar to the <a> elements of web pages:

1
$output->writeln('<href=https://symfony.com>Symfony Homepage</>');

If your terminal belongs to the list of terminal emulators that support links you can click on the "Symfony Homepage" text to open its URL in your default browser. Otherwise, you'll see "Symfony Homepage" as regular text and the URL will be lost.

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version

    Symfony 7.1 is backed by

    Peruse our complete Symfony & PHP solutions catalog for your web development needs.

    Peruse our complete Symfony & PHP solutions catalog for your web development needs.

    Save your teams and projects before they sink

    Save your teams and projects before they sink

    Version:

    Table of Contents

    • Using Color Styles
      • Displaying Clickable Links

    Symfony footer

    Avatar of Oriol Viñals, a Symfony contributor

    Thanks Oriol Viñals for being a Symfony contributor

    2 commits • 16 lines changed

    View all contributors that help us make Symfony

    Become a Symfony contributor

    Be an active part of the community and contribute ideas, code and bug fixes. Both experts and newcomers are welcome.

    Learn how to contribute

    Symfony™ is a trademark of Symfony SAS. All rights reserved.

    • What is Symfony?

      • What is Symfony?
      • Symfony at a Glance
      • Symfony Components
      • Symfony Releases
      • Security Policy
      • Logo & Screenshots
      • Trademark & Licenses
      • symfony1 Legacy
    • Learn Symfony

      • Symfony Docs
      • Symfony Book
      • Reference
      • Bundles
      • Best Practices
      • Training
      • eLearning Platform
      • Certification
    • Screencasts

      • Learn Symfony
      • Learn PHP
      • Learn JavaScript
      • Learn Drupal
      • Learn RESTful APIs
    • Community

      • Symfony Community
      • SymfonyConnect
      • Events & Meetups
      • Projects using Symfony
      • Contributors
      • Symfony Jobs
      • Backers
      • Code of Conduct
      • Downloads Stats
      • Support
    • Blog

      • All Blog Posts
      • A Week of Symfony
      • Case Studies
      • Cloud
      • Community
      • Conferences
      • Diversity
      • Living on the edge
      • Releases
      • Security Advisories
      • Symfony Insight
      • Twig
      • SensioLabs Blog
    • Services

      • SensioLabs services
      • Train developers
      • Manage your project quality
      • Improve your project performance
      • Host Symfony projects

      Powered by

    Follow Symfony