Can `cat` show files using code markup in colors?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







43















Sometimes I quickly want to view the contents of a file from the command line. For this I of course use cat, but it is often source files in Python, Java or simple HTML. For these files it would be handy if cat could give some color markup to the files, so that it reads more easy.



Can cat do such a thing?










share|improve this question

























  • This may already have an answer here

    – Wilf
    Jan 15 '14 at 16:58











  • ha @wilf I found the same code with google :D Also worth noting this topic on SO: superuser.com/questions/84426/…

    – Rinzwind
    Jan 15 '14 at 17:07











  • @Rinzwind - I searched stackoverflow , as it was bound to come up ;-)

    – Wilf
    Jan 15 '14 at 17:09











  • This question reminds me of cat -v considered harmful.

    – Snowball
    Jan 16 '14 at 5:39




















43















Sometimes I quickly want to view the contents of a file from the command line. For this I of course use cat, but it is often source files in Python, Java or simple HTML. For these files it would be handy if cat could give some color markup to the files, so that it reads more easy.



Can cat do such a thing?










share|improve this question

























  • This may already have an answer here

    – Wilf
    Jan 15 '14 at 16:58











  • ha @wilf I found the same code with google :D Also worth noting this topic on SO: superuser.com/questions/84426/…

    – Rinzwind
    Jan 15 '14 at 17:07











  • @Rinzwind - I searched stackoverflow , as it was bound to come up ;-)

    – Wilf
    Jan 15 '14 at 17:09











  • This question reminds me of cat -v considered harmful.

    – Snowball
    Jan 16 '14 at 5:39
















43












43








43


12






Sometimes I quickly want to view the contents of a file from the command line. For this I of course use cat, but it is often source files in Python, Java or simple HTML. For these files it would be handy if cat could give some color markup to the files, so that it reads more easy.



Can cat do such a thing?










share|improve this question
















Sometimes I quickly want to view the contents of a file from the command line. For this I of course use cat, but it is often source files in Python, Java or simple HTML. For these files it would be handy if cat could give some color markup to the files, so that it reads more easy.



Can cat do such a thing?







colors cat






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 15 '14 at 20:51









Peter Mortensen

1,03221016




1,03221016










asked Jan 15 '14 at 16:44









kramer65kramer65

94841532




94841532













  • This may already have an answer here

    – Wilf
    Jan 15 '14 at 16:58











  • ha @wilf I found the same code with google :D Also worth noting this topic on SO: superuser.com/questions/84426/…

    – Rinzwind
    Jan 15 '14 at 17:07











  • @Rinzwind - I searched stackoverflow , as it was bound to come up ;-)

    – Wilf
    Jan 15 '14 at 17:09











  • This question reminds me of cat -v considered harmful.

    – Snowball
    Jan 16 '14 at 5:39





















  • This may already have an answer here

    – Wilf
    Jan 15 '14 at 16:58











  • ha @wilf I found the same code with google :D Also worth noting this topic on SO: superuser.com/questions/84426/…

    – Rinzwind
    Jan 15 '14 at 17:07











  • @Rinzwind - I searched stackoverflow , as it was bound to come up ;-)

    – Wilf
    Jan 15 '14 at 17:09











  • This question reminds me of cat -v considered harmful.

    – Snowball
    Jan 16 '14 at 5:39



















This may already have an answer here

– Wilf
Jan 15 '14 at 16:58





This may already have an answer here

– Wilf
Jan 15 '14 at 16:58













ha @wilf I found the same code with google :D Also worth noting this topic on SO: superuser.com/questions/84426/…

– Rinzwind
Jan 15 '14 at 17:07





ha @wilf I found the same code with google :D Also worth noting this topic on SO: superuser.com/questions/84426/…

– Rinzwind
Jan 15 '14 at 17:07













@Rinzwind - I searched stackoverflow , as it was bound to come up ;-)

– Wilf
Jan 15 '14 at 17:09





@Rinzwind - I searched stackoverflow , as it was bound to come up ;-)

– Wilf
Jan 15 '14 at 17:09













This question reminds me of cat -v considered harmful.

– Snowball
Jan 16 '14 at 5:39







This question reminds me of cat -v considered harmful.

– Snowball
Jan 16 '14 at 5:39












8 Answers
8






active

oldest

votes


















45














cat is not able to do this. However, maybe pygments may be able to help you there. It is a python script and can be either installed via apt-get



sudo apt-get install python-pygments


or easily downloaded and installed via easy_install.



It supports lots of source code languages and also markup languages



It is used by



pygmentize -g <filename>





share|improve this answer





















  • 5





    Open file as pygmentize -g <filename>, the -g switch is important to handle file without lexer. for example if you try /etc/fstab to open without -g it will fail.

    – souravc
    Jan 15 '14 at 18:25











  • Thanks @souravc Added it to the answer. Interestingly, this argument does not seem to be mentioned in the man page or help page

    – txwikinger
    Jan 15 '14 at 18:29






  • 2





    Well I played a lot with it. you can find it, try pygmentize --help If -g is passed, attempt to guess the lexer from the file contents, or pass through as plain text if this fails (this can work for stdin).

    – souravc
    Jan 15 '14 at 18:33








  • 3





    This is the best, then alias cat to pygmentize -g and you are golden. In fish it is simple as funced cat... type pygmentize -g $argv... save..funcsave cat.

    – Elijah Lynn
    Jun 28 '15 at 13:04











  • in my bashrc I added: command -v pygmentize >/dev/null 2>&1 && alias cat='pygmentize -g'

    – Antonios Hadjigeorgalis
    Nov 29 '16 at 12:22





















18














Not from cat itself but you can use something like source highlite or supercat or highlight




Source-highlight



This program, given a source file, produces a document with syntax highlighting. It also provides a C++ highlight library (new) (since version 3.0).



Source-highlight reads source language specifications dynamically, thus it can be easily extended (without recompiling the sources) for handling new languages. It also reads output format specifications dynamically, and thus it can be easily extended (without recompiling the sources) for handling new output formats. The syntax for these specifications is quite easy (take a look at the manual).



The manual about installation:



See the file INSTALL for detailed building and installation instructions; anyway if you're used to compiling Linux software that comes with sources you may simply follow the usual procedure, i.e., untar the file you downloaded in a directory and then:




 cd <source code main directory>
./configure
make
make install



Supercat



This is Supercat's homepage. Supercat is a program that colorizes text based on matching regular expressions/strings/characters. Supercat supports html output as well as standard ASCII text. Unlike some text-colorizing programs that exist, Supercat does not require you to have to be a programmer to make colorization rules.



If you have written a supercat config file for a standard file type please do not hesitate to contact me at "bug-spc (at) nosredna (dot) net" for possible inclusion in the supercat distribution.




  • Here is an example page of Supercat's work on Gentoo's emerge.log


  • Installation/source file (tar.gz)


  • Example configuration file





Or with a function (source):



#!/bin/bash#!/bin/bash
if [ ! -t 0 ];then
file=/dev/stdin
elif [ -f $1 ];then
file=$1
else
echo "Usage: $0 code.c"
echo "or e.g. head code.c|$0"
exit 1
fi
pygmentize -f terminal -g $file


Requires: Pygments (sudo apt-get install python-pygments python3-pygments)
Add it as a function to bash .functions and give it a name like color()






share|improve this answer

































    14














    As from this answer here, you can use the python-pygments packages to highlight stuff. First do:



    sudo apt-get install python-pygments python3-pygments


    then:



    pygmentize -g FILENAME


    then have a go:



    enter image description here



    You can also set it as an alias, like in the answer I linked - basically, run this:



    echo "alias catc='pygmentize -g'" >> ~/.bash_aliases 
    chmod +x ~/.bash_aliases


    Close the terminal, open it again, and the catc command should now work - if it does not, make sure these lines are in the .bashrc file, and are uncommented:



    if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
    fi




    Another thing would be to just use nano:



    nano testfile


    enter image description here






    share|improve this answer

































      6














      man view or man vim



      Basic usage: view <filename>



      Quit: :q<Return> (add an <Esc> first if using vim), or ZZ (upper case z twice).



      The programmer's text editor vim has all you need already, and is likely already part of your system.



      vim has a read-only mode activated with view or vim -R. If all you want to do is view the marked-up file, it should be enough.



      Simple to use, navigable, available everywhere. No need to mess about with installing new software or writing bash scripts.






      share|improve this answer


























      • I know. The thing is that openeing and closing VIM is not quick. I want to instantly view a file and then continue on the command line. That's why I found pygmentize such a good one. Instant result, no quiting a program or anything.. :)

        – kramer65
        Jan 16 '14 at 11:10






      • 1





        @kramer65 OK of course it's your preference, but you don't need to "open" vim or view, you just provide it with the file: view file.py, so no difference there with cat. Sure you need to close it, but really, :q<Return> or ZZ are not difficult tasks.

        – a different ben
        Jan 17 '14 at 1:46











      • The major problem being that vi/vim/view don't write to stdout, so I can't easily write a command whilst glancing at the syntax highlighted output above.

        – Jack_Hu
        Oct 20 '18 at 14:06



















      4














      One can check out ccat.



      It adds syntax highlight to output files.






      share|improve this answer































        3














        cat can not produce syntax highlighting solely. Still you can do this as follows, using python-pygments. First install it from terminal as,



        sudo apt-get install python-pygments


        Now copy the function below ~/.bashrc. It will give you what you want moreover it will preserve the properties of cat otherwise there is no point of using cat



        catc(){
        cat "$@" > /tmp/.tmp
        pygmentize -g /tmp/.tmp
        rm /tmp/.tmp
        }


        Source ~/.bashrc as,



        . ~/.bashrc


        It will give colourized output,



        catc <filename>


        It will concatenate with color as well,



        catc <file1> <file2> ... <filen>





        share|improve this answer


























        • Is there a way to enable scrolling with up and down arrows when using pygmentize? I'm opening it using a command of the form xterm -e "pygmentize -g <filename>" and am finding that the only way to scroll is to use the scroll bar. I tried piping the output to less but that resulted in garbage output.

          – Leo Simon
          Nov 3 '16 at 14:58











        • yes you can use pygmentize with less. Create a bash function lessc as following: lessc () { pygmentize -gf terminal "$1" | less -R }

          – souravc
          Nov 3 '16 at 17:04



















        0














        Other answers cover why cat is not able to do it. Though you can do it with less using
        lesspipe.sh.






        share|improve this answer































          0














          Bat — A cat clone with wings



          You might also want to check out bat which has the following features:




          • Syntax highlighting

          • Git integration

          • Optionally showing non-printable characters

          • Automatic paging with less

          • File concatenation as a drop-in replacement for cat when redirected


           Preview



          Screenshot of bat showing a colored file



          Picture taken from the official GitHub



          Installation



          You can get the latest Debian package here and install it with:



          sudo dpkg -i bat_0.10.0_amd64.deb


          Adapting the version number and architecture.






          share|improve this answer
























            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "89"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f405960%2fcan-cat-show-files-using-code-markup-in-colors%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            8 Answers
            8






            active

            oldest

            votes








            8 Answers
            8






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            45














            cat is not able to do this. However, maybe pygments may be able to help you there. It is a python script and can be either installed via apt-get



            sudo apt-get install python-pygments


            or easily downloaded and installed via easy_install.



            It supports lots of source code languages and also markup languages



            It is used by



            pygmentize -g <filename>





            share|improve this answer





















            • 5





              Open file as pygmentize -g <filename>, the -g switch is important to handle file without lexer. for example if you try /etc/fstab to open without -g it will fail.

              – souravc
              Jan 15 '14 at 18:25











            • Thanks @souravc Added it to the answer. Interestingly, this argument does not seem to be mentioned in the man page or help page

              – txwikinger
              Jan 15 '14 at 18:29






            • 2





              Well I played a lot with it. you can find it, try pygmentize --help If -g is passed, attempt to guess the lexer from the file contents, or pass through as plain text if this fails (this can work for stdin).

              – souravc
              Jan 15 '14 at 18:33








            • 3





              This is the best, then alias cat to pygmentize -g and you are golden. In fish it is simple as funced cat... type pygmentize -g $argv... save..funcsave cat.

              – Elijah Lynn
              Jun 28 '15 at 13:04











            • in my bashrc I added: command -v pygmentize >/dev/null 2>&1 && alias cat='pygmentize -g'

              – Antonios Hadjigeorgalis
              Nov 29 '16 at 12:22


















            45














            cat is not able to do this. However, maybe pygments may be able to help you there. It is a python script and can be either installed via apt-get



            sudo apt-get install python-pygments


            or easily downloaded and installed via easy_install.



            It supports lots of source code languages and also markup languages



            It is used by



            pygmentize -g <filename>





            share|improve this answer





















            • 5





              Open file as pygmentize -g <filename>, the -g switch is important to handle file without lexer. for example if you try /etc/fstab to open without -g it will fail.

              – souravc
              Jan 15 '14 at 18:25











            • Thanks @souravc Added it to the answer. Interestingly, this argument does not seem to be mentioned in the man page or help page

              – txwikinger
              Jan 15 '14 at 18:29






            • 2





              Well I played a lot with it. you can find it, try pygmentize --help If -g is passed, attempt to guess the lexer from the file contents, or pass through as plain text if this fails (this can work for stdin).

              – souravc
              Jan 15 '14 at 18:33








            • 3





              This is the best, then alias cat to pygmentize -g and you are golden. In fish it is simple as funced cat... type pygmentize -g $argv... save..funcsave cat.

              – Elijah Lynn
              Jun 28 '15 at 13:04











            • in my bashrc I added: command -v pygmentize >/dev/null 2>&1 && alias cat='pygmentize -g'

              – Antonios Hadjigeorgalis
              Nov 29 '16 at 12:22
















            45












            45








            45







            cat is not able to do this. However, maybe pygments may be able to help you there. It is a python script and can be either installed via apt-get



            sudo apt-get install python-pygments


            or easily downloaded and installed via easy_install.



            It supports lots of source code languages and also markup languages



            It is used by



            pygmentize -g <filename>





            share|improve this answer















            cat is not able to do this. However, maybe pygments may be able to help you there. It is a python script and can be either installed via apt-get



            sudo apt-get install python-pygments


            or easily downloaded and installed via easy_install.



            It supports lots of source code languages and also markup languages



            It is used by



            pygmentize -g <filename>






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jan 15 '14 at 18:45

























            answered Jan 15 '14 at 17:05









            txwikingertxwikinger

            19.6k106693




            19.6k106693








            • 5





              Open file as pygmentize -g <filename>, the -g switch is important to handle file without lexer. for example if you try /etc/fstab to open without -g it will fail.

              – souravc
              Jan 15 '14 at 18:25











            • Thanks @souravc Added it to the answer. Interestingly, this argument does not seem to be mentioned in the man page or help page

              – txwikinger
              Jan 15 '14 at 18:29






            • 2





              Well I played a lot with it. you can find it, try pygmentize --help If -g is passed, attempt to guess the lexer from the file contents, or pass through as plain text if this fails (this can work for stdin).

              – souravc
              Jan 15 '14 at 18:33








            • 3





              This is the best, then alias cat to pygmentize -g and you are golden. In fish it is simple as funced cat... type pygmentize -g $argv... save..funcsave cat.

              – Elijah Lynn
              Jun 28 '15 at 13:04











            • in my bashrc I added: command -v pygmentize >/dev/null 2>&1 && alias cat='pygmentize -g'

              – Antonios Hadjigeorgalis
              Nov 29 '16 at 12:22
















            • 5





              Open file as pygmentize -g <filename>, the -g switch is important to handle file without lexer. for example if you try /etc/fstab to open without -g it will fail.

              – souravc
              Jan 15 '14 at 18:25











            • Thanks @souravc Added it to the answer. Interestingly, this argument does not seem to be mentioned in the man page or help page

              – txwikinger
              Jan 15 '14 at 18:29






            • 2





              Well I played a lot with it. you can find it, try pygmentize --help If -g is passed, attempt to guess the lexer from the file contents, or pass through as plain text if this fails (this can work for stdin).

              – souravc
              Jan 15 '14 at 18:33








            • 3





              This is the best, then alias cat to pygmentize -g and you are golden. In fish it is simple as funced cat... type pygmentize -g $argv... save..funcsave cat.

              – Elijah Lynn
              Jun 28 '15 at 13:04











            • in my bashrc I added: command -v pygmentize >/dev/null 2>&1 && alias cat='pygmentize -g'

              – Antonios Hadjigeorgalis
              Nov 29 '16 at 12:22










            5




            5





            Open file as pygmentize -g <filename>, the -g switch is important to handle file without lexer. for example if you try /etc/fstab to open without -g it will fail.

            – souravc
            Jan 15 '14 at 18:25





            Open file as pygmentize -g <filename>, the -g switch is important to handle file without lexer. for example if you try /etc/fstab to open without -g it will fail.

            – souravc
            Jan 15 '14 at 18:25













            Thanks @souravc Added it to the answer. Interestingly, this argument does not seem to be mentioned in the man page or help page

            – txwikinger
            Jan 15 '14 at 18:29





            Thanks @souravc Added it to the answer. Interestingly, this argument does not seem to be mentioned in the man page or help page

            – txwikinger
            Jan 15 '14 at 18:29




            2




            2





            Well I played a lot with it. you can find it, try pygmentize --help If -g is passed, attempt to guess the lexer from the file contents, or pass through as plain text if this fails (this can work for stdin).

            – souravc
            Jan 15 '14 at 18:33







            Well I played a lot with it. you can find it, try pygmentize --help If -g is passed, attempt to guess the lexer from the file contents, or pass through as plain text if this fails (this can work for stdin).

            – souravc
            Jan 15 '14 at 18:33






            3




            3





            This is the best, then alias cat to pygmentize -g and you are golden. In fish it is simple as funced cat... type pygmentize -g $argv... save..funcsave cat.

            – Elijah Lynn
            Jun 28 '15 at 13:04





            This is the best, then alias cat to pygmentize -g and you are golden. In fish it is simple as funced cat... type pygmentize -g $argv... save..funcsave cat.

            – Elijah Lynn
            Jun 28 '15 at 13:04













            in my bashrc I added: command -v pygmentize >/dev/null 2>&1 && alias cat='pygmentize -g'

            – Antonios Hadjigeorgalis
            Nov 29 '16 at 12:22







            in my bashrc I added: command -v pygmentize >/dev/null 2>&1 && alias cat='pygmentize -g'

            – Antonios Hadjigeorgalis
            Nov 29 '16 at 12:22















            18














            Not from cat itself but you can use something like source highlite or supercat or highlight




            Source-highlight



            This program, given a source file, produces a document with syntax highlighting. It also provides a C++ highlight library (new) (since version 3.0).



            Source-highlight reads source language specifications dynamically, thus it can be easily extended (without recompiling the sources) for handling new languages. It also reads output format specifications dynamically, and thus it can be easily extended (without recompiling the sources) for handling new output formats. The syntax for these specifications is quite easy (take a look at the manual).



            The manual about installation:



            See the file INSTALL for detailed building and installation instructions; anyway if you're used to compiling Linux software that comes with sources you may simply follow the usual procedure, i.e., untar the file you downloaded in a directory and then:




             cd <source code main directory>
            ./configure
            make
            make install



            Supercat



            This is Supercat's homepage. Supercat is a program that colorizes text based on matching regular expressions/strings/characters. Supercat supports html output as well as standard ASCII text. Unlike some text-colorizing programs that exist, Supercat does not require you to have to be a programmer to make colorization rules.



            If you have written a supercat config file for a standard file type please do not hesitate to contact me at "bug-spc (at) nosredna (dot) net" for possible inclusion in the supercat distribution.




            • Here is an example page of Supercat's work on Gentoo's emerge.log


            • Installation/source file (tar.gz)


            • Example configuration file





            Or with a function (source):



            #!/bin/bash#!/bin/bash
            if [ ! -t 0 ];then
            file=/dev/stdin
            elif [ -f $1 ];then
            file=$1
            else
            echo "Usage: $0 code.c"
            echo "or e.g. head code.c|$0"
            exit 1
            fi
            pygmentize -f terminal -g $file


            Requires: Pygments (sudo apt-get install python-pygments python3-pygments)
            Add it as a function to bash .functions and give it a name like color()






            share|improve this answer






























              18














              Not from cat itself but you can use something like source highlite or supercat or highlight




              Source-highlight



              This program, given a source file, produces a document with syntax highlighting. It also provides a C++ highlight library (new) (since version 3.0).



              Source-highlight reads source language specifications dynamically, thus it can be easily extended (without recompiling the sources) for handling new languages. It also reads output format specifications dynamically, and thus it can be easily extended (without recompiling the sources) for handling new output formats. The syntax for these specifications is quite easy (take a look at the manual).



              The manual about installation:



              See the file INSTALL for detailed building and installation instructions; anyway if you're used to compiling Linux software that comes with sources you may simply follow the usual procedure, i.e., untar the file you downloaded in a directory and then:




               cd <source code main directory>
              ./configure
              make
              make install



              Supercat



              This is Supercat's homepage. Supercat is a program that colorizes text based on matching regular expressions/strings/characters. Supercat supports html output as well as standard ASCII text. Unlike some text-colorizing programs that exist, Supercat does not require you to have to be a programmer to make colorization rules.



              If you have written a supercat config file for a standard file type please do not hesitate to contact me at "bug-spc (at) nosredna (dot) net" for possible inclusion in the supercat distribution.




              • Here is an example page of Supercat's work on Gentoo's emerge.log


              • Installation/source file (tar.gz)


              • Example configuration file





              Or with a function (source):



              #!/bin/bash#!/bin/bash
              if [ ! -t 0 ];then
              file=/dev/stdin
              elif [ -f $1 ];then
              file=$1
              else
              echo "Usage: $0 code.c"
              echo "or e.g. head code.c|$0"
              exit 1
              fi
              pygmentize -f terminal -g $file


              Requires: Pygments (sudo apt-get install python-pygments python3-pygments)
              Add it as a function to bash .functions and give it a name like color()






              share|improve this answer




























                18












                18








                18







                Not from cat itself but you can use something like source highlite or supercat or highlight




                Source-highlight



                This program, given a source file, produces a document with syntax highlighting. It also provides a C++ highlight library (new) (since version 3.0).



                Source-highlight reads source language specifications dynamically, thus it can be easily extended (without recompiling the sources) for handling new languages. It also reads output format specifications dynamically, and thus it can be easily extended (without recompiling the sources) for handling new output formats. The syntax for these specifications is quite easy (take a look at the manual).



                The manual about installation:



                See the file INSTALL for detailed building and installation instructions; anyway if you're used to compiling Linux software that comes with sources you may simply follow the usual procedure, i.e., untar the file you downloaded in a directory and then:




                 cd <source code main directory>
                ./configure
                make
                make install



                Supercat



                This is Supercat's homepage. Supercat is a program that colorizes text based on matching regular expressions/strings/characters. Supercat supports html output as well as standard ASCII text. Unlike some text-colorizing programs that exist, Supercat does not require you to have to be a programmer to make colorization rules.



                If you have written a supercat config file for a standard file type please do not hesitate to contact me at "bug-spc (at) nosredna (dot) net" for possible inclusion in the supercat distribution.




                • Here is an example page of Supercat's work on Gentoo's emerge.log


                • Installation/source file (tar.gz)


                • Example configuration file





                Or with a function (source):



                #!/bin/bash#!/bin/bash
                if [ ! -t 0 ];then
                file=/dev/stdin
                elif [ -f $1 ];then
                file=$1
                else
                echo "Usage: $0 code.c"
                echo "or e.g. head code.c|$0"
                exit 1
                fi
                pygmentize -f terminal -g $file


                Requires: Pygments (sudo apt-get install python-pygments python3-pygments)
                Add it as a function to bash .functions and give it a name like color()






                share|improve this answer















                Not from cat itself but you can use something like source highlite or supercat or highlight




                Source-highlight



                This program, given a source file, produces a document with syntax highlighting. It also provides a C++ highlight library (new) (since version 3.0).



                Source-highlight reads source language specifications dynamically, thus it can be easily extended (without recompiling the sources) for handling new languages. It also reads output format specifications dynamically, and thus it can be easily extended (without recompiling the sources) for handling new output formats. The syntax for these specifications is quite easy (take a look at the manual).



                The manual about installation:



                See the file INSTALL for detailed building and installation instructions; anyway if you're used to compiling Linux software that comes with sources you may simply follow the usual procedure, i.e., untar the file you downloaded in a directory and then:




                 cd <source code main directory>
                ./configure
                make
                make install



                Supercat



                This is Supercat's homepage. Supercat is a program that colorizes text based on matching regular expressions/strings/characters. Supercat supports html output as well as standard ASCII text. Unlike some text-colorizing programs that exist, Supercat does not require you to have to be a programmer to make colorization rules.



                If you have written a supercat config file for a standard file type please do not hesitate to contact me at "bug-spc (at) nosredna (dot) net" for possible inclusion in the supercat distribution.




                • Here is an example page of Supercat's work on Gentoo's emerge.log


                • Installation/source file (tar.gz)


                • Example configuration file





                Or with a function (source):



                #!/bin/bash#!/bin/bash
                if [ ! -t 0 ];then
                file=/dev/stdin
                elif [ -f $1 ];then
                file=$1
                else
                echo "Usage: $0 code.c"
                echo "or e.g. head code.c|$0"
                exit 1
                fi
                pygmentize -f terminal -g $file


                Requires: Pygments (sudo apt-get install python-pygments python3-pygments)
                Add it as a function to bash .functions and give it a name like color()







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Jan 15 '14 at 17:11

























                answered Jan 15 '14 at 17:05









                RinzwindRinzwind

                211k28406541




                211k28406541























                    14














                    As from this answer here, you can use the python-pygments packages to highlight stuff. First do:



                    sudo apt-get install python-pygments python3-pygments


                    then:



                    pygmentize -g FILENAME


                    then have a go:



                    enter image description here



                    You can also set it as an alias, like in the answer I linked - basically, run this:



                    echo "alias catc='pygmentize -g'" >> ~/.bash_aliases 
                    chmod +x ~/.bash_aliases


                    Close the terminal, open it again, and the catc command should now work - if it does not, make sure these lines are in the .bashrc file, and are uncommented:



                    if [ -f ~/.bash_aliases ]; then
                    . ~/.bash_aliases
                    fi




                    Another thing would be to just use nano:



                    nano testfile


                    enter image description here






                    share|improve this answer






























                      14














                      As from this answer here, you can use the python-pygments packages to highlight stuff. First do:



                      sudo apt-get install python-pygments python3-pygments


                      then:



                      pygmentize -g FILENAME


                      then have a go:



                      enter image description here



                      You can also set it as an alias, like in the answer I linked - basically, run this:



                      echo "alias catc='pygmentize -g'" >> ~/.bash_aliases 
                      chmod +x ~/.bash_aliases


                      Close the terminal, open it again, and the catc command should now work - if it does not, make sure these lines are in the .bashrc file, and are uncommented:



                      if [ -f ~/.bash_aliases ]; then
                      . ~/.bash_aliases
                      fi




                      Another thing would be to just use nano:



                      nano testfile


                      enter image description here






                      share|improve this answer




























                        14












                        14








                        14







                        As from this answer here, you can use the python-pygments packages to highlight stuff. First do:



                        sudo apt-get install python-pygments python3-pygments


                        then:



                        pygmentize -g FILENAME


                        then have a go:



                        enter image description here



                        You can also set it as an alias, like in the answer I linked - basically, run this:



                        echo "alias catc='pygmentize -g'" >> ~/.bash_aliases 
                        chmod +x ~/.bash_aliases


                        Close the terminal, open it again, and the catc command should now work - if it does not, make sure these lines are in the .bashrc file, and are uncommented:



                        if [ -f ~/.bash_aliases ]; then
                        . ~/.bash_aliases
                        fi




                        Another thing would be to just use nano:



                        nano testfile


                        enter image description here






                        share|improve this answer















                        As from this answer here, you can use the python-pygments packages to highlight stuff. First do:



                        sudo apt-get install python-pygments python3-pygments


                        then:



                        pygmentize -g FILENAME


                        then have a go:



                        enter image description here



                        You can also set it as an alias, like in the answer I linked - basically, run this:



                        echo "alias catc='pygmentize -g'" >> ~/.bash_aliases 
                        chmod +x ~/.bash_aliases


                        Close the terminal, open it again, and the catc command should now work - if it does not, make sure these lines are in the .bashrc file, and are uncommented:



                        if [ -f ~/.bash_aliases ]; then
                        . ~/.bash_aliases
                        fi




                        Another thing would be to just use nano:



                        nano testfile


                        enter image description here







                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited May 23 '17 at 12:39









                        Community

                        1




                        1










                        answered Jan 15 '14 at 17:08









                        WilfWilf

                        22k1167131




                        22k1167131























                            6














                            man view or man vim



                            Basic usage: view <filename>



                            Quit: :q<Return> (add an <Esc> first if using vim), or ZZ (upper case z twice).



                            The programmer's text editor vim has all you need already, and is likely already part of your system.



                            vim has a read-only mode activated with view or vim -R. If all you want to do is view the marked-up file, it should be enough.



                            Simple to use, navigable, available everywhere. No need to mess about with installing new software or writing bash scripts.






                            share|improve this answer


























                            • I know. The thing is that openeing and closing VIM is not quick. I want to instantly view a file and then continue on the command line. That's why I found pygmentize such a good one. Instant result, no quiting a program or anything.. :)

                              – kramer65
                              Jan 16 '14 at 11:10






                            • 1





                              @kramer65 OK of course it's your preference, but you don't need to "open" vim or view, you just provide it with the file: view file.py, so no difference there with cat. Sure you need to close it, but really, :q<Return> or ZZ are not difficult tasks.

                              – a different ben
                              Jan 17 '14 at 1:46











                            • The major problem being that vi/vim/view don't write to stdout, so I can't easily write a command whilst glancing at the syntax highlighted output above.

                              – Jack_Hu
                              Oct 20 '18 at 14:06
















                            6














                            man view or man vim



                            Basic usage: view <filename>



                            Quit: :q<Return> (add an <Esc> first if using vim), or ZZ (upper case z twice).



                            The programmer's text editor vim has all you need already, and is likely already part of your system.



                            vim has a read-only mode activated with view or vim -R. If all you want to do is view the marked-up file, it should be enough.



                            Simple to use, navigable, available everywhere. No need to mess about with installing new software or writing bash scripts.






                            share|improve this answer


























                            • I know. The thing is that openeing and closing VIM is not quick. I want to instantly view a file and then continue on the command line. That's why I found pygmentize such a good one. Instant result, no quiting a program or anything.. :)

                              – kramer65
                              Jan 16 '14 at 11:10






                            • 1





                              @kramer65 OK of course it's your preference, but you don't need to "open" vim or view, you just provide it with the file: view file.py, so no difference there with cat. Sure you need to close it, but really, :q<Return> or ZZ are not difficult tasks.

                              – a different ben
                              Jan 17 '14 at 1:46











                            • The major problem being that vi/vim/view don't write to stdout, so I can't easily write a command whilst glancing at the syntax highlighted output above.

                              – Jack_Hu
                              Oct 20 '18 at 14:06














                            6












                            6








                            6







                            man view or man vim



                            Basic usage: view <filename>



                            Quit: :q<Return> (add an <Esc> first if using vim), or ZZ (upper case z twice).



                            The programmer's text editor vim has all you need already, and is likely already part of your system.



                            vim has a read-only mode activated with view or vim -R. If all you want to do is view the marked-up file, it should be enough.



                            Simple to use, navigable, available everywhere. No need to mess about with installing new software or writing bash scripts.






                            share|improve this answer















                            man view or man vim



                            Basic usage: view <filename>



                            Quit: :q<Return> (add an <Esc> first if using vim), or ZZ (upper case z twice).



                            The programmer's text editor vim has all you need already, and is likely already part of your system.



                            vim has a read-only mode activated with view or vim -R. If all you want to do is view the marked-up file, it should be enough.



                            Simple to use, navigable, available everywhere. No need to mess about with installing new software or writing bash scripts.







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Jan 17 '14 at 1:49

























                            answered Jan 16 '14 at 5:55









                            a different bena different ben

                            278215




                            278215













                            • I know. The thing is that openeing and closing VIM is not quick. I want to instantly view a file and then continue on the command line. That's why I found pygmentize such a good one. Instant result, no quiting a program or anything.. :)

                              – kramer65
                              Jan 16 '14 at 11:10






                            • 1





                              @kramer65 OK of course it's your preference, but you don't need to "open" vim or view, you just provide it with the file: view file.py, so no difference there with cat. Sure you need to close it, but really, :q<Return> or ZZ are not difficult tasks.

                              – a different ben
                              Jan 17 '14 at 1:46











                            • The major problem being that vi/vim/view don't write to stdout, so I can't easily write a command whilst glancing at the syntax highlighted output above.

                              – Jack_Hu
                              Oct 20 '18 at 14:06



















                            • I know. The thing is that openeing and closing VIM is not quick. I want to instantly view a file and then continue on the command line. That's why I found pygmentize such a good one. Instant result, no quiting a program or anything.. :)

                              – kramer65
                              Jan 16 '14 at 11:10






                            • 1





                              @kramer65 OK of course it's your preference, but you don't need to "open" vim or view, you just provide it with the file: view file.py, so no difference there with cat. Sure you need to close it, but really, :q<Return> or ZZ are not difficult tasks.

                              – a different ben
                              Jan 17 '14 at 1:46











                            • The major problem being that vi/vim/view don't write to stdout, so I can't easily write a command whilst glancing at the syntax highlighted output above.

                              – Jack_Hu
                              Oct 20 '18 at 14:06

















                            I know. The thing is that openeing and closing VIM is not quick. I want to instantly view a file and then continue on the command line. That's why I found pygmentize such a good one. Instant result, no quiting a program or anything.. :)

                            – kramer65
                            Jan 16 '14 at 11:10





                            I know. The thing is that openeing and closing VIM is not quick. I want to instantly view a file and then continue on the command line. That's why I found pygmentize such a good one. Instant result, no quiting a program or anything.. :)

                            – kramer65
                            Jan 16 '14 at 11:10




                            1




                            1





                            @kramer65 OK of course it's your preference, but you don't need to "open" vim or view, you just provide it with the file: view file.py, so no difference there with cat. Sure you need to close it, but really, :q<Return> or ZZ are not difficult tasks.

                            – a different ben
                            Jan 17 '14 at 1:46





                            @kramer65 OK of course it's your preference, but you don't need to "open" vim or view, you just provide it with the file: view file.py, so no difference there with cat. Sure you need to close it, but really, :q<Return> or ZZ are not difficult tasks.

                            – a different ben
                            Jan 17 '14 at 1:46













                            The major problem being that vi/vim/view don't write to stdout, so I can't easily write a command whilst glancing at the syntax highlighted output above.

                            – Jack_Hu
                            Oct 20 '18 at 14:06





                            The major problem being that vi/vim/view don't write to stdout, so I can't easily write a command whilst glancing at the syntax highlighted output above.

                            – Jack_Hu
                            Oct 20 '18 at 14:06











                            4














                            One can check out ccat.



                            It adds syntax highlight to output files.






                            share|improve this answer




























                              4














                              One can check out ccat.



                              It adds syntax highlight to output files.






                              share|improve this answer


























                                4












                                4








                                4







                                One can check out ccat.



                                It adds syntax highlight to output files.






                                share|improve this answer













                                One can check out ccat.



                                It adds syntax highlight to output files.







                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Aug 27 '15 at 0:08









                                Alan DongAlan Dong

                                1413




                                1413























                                    3














                                    cat can not produce syntax highlighting solely. Still you can do this as follows, using python-pygments. First install it from terminal as,



                                    sudo apt-get install python-pygments


                                    Now copy the function below ~/.bashrc. It will give you what you want moreover it will preserve the properties of cat otherwise there is no point of using cat



                                    catc(){
                                    cat "$@" > /tmp/.tmp
                                    pygmentize -g /tmp/.tmp
                                    rm /tmp/.tmp
                                    }


                                    Source ~/.bashrc as,



                                    . ~/.bashrc


                                    It will give colourized output,



                                    catc <filename>


                                    It will concatenate with color as well,



                                    catc <file1> <file2> ... <filen>





                                    share|improve this answer


























                                    • Is there a way to enable scrolling with up and down arrows when using pygmentize? I'm opening it using a command of the form xterm -e "pygmentize -g <filename>" and am finding that the only way to scroll is to use the scroll bar. I tried piping the output to less but that resulted in garbage output.

                                      – Leo Simon
                                      Nov 3 '16 at 14:58











                                    • yes you can use pygmentize with less. Create a bash function lessc as following: lessc () { pygmentize -gf terminal "$1" | less -R }

                                      – souravc
                                      Nov 3 '16 at 17:04
















                                    3














                                    cat can not produce syntax highlighting solely. Still you can do this as follows, using python-pygments. First install it from terminal as,



                                    sudo apt-get install python-pygments


                                    Now copy the function below ~/.bashrc. It will give you what you want moreover it will preserve the properties of cat otherwise there is no point of using cat



                                    catc(){
                                    cat "$@" > /tmp/.tmp
                                    pygmentize -g /tmp/.tmp
                                    rm /tmp/.tmp
                                    }


                                    Source ~/.bashrc as,



                                    . ~/.bashrc


                                    It will give colourized output,



                                    catc <filename>


                                    It will concatenate with color as well,



                                    catc <file1> <file2> ... <filen>





                                    share|improve this answer


























                                    • Is there a way to enable scrolling with up and down arrows when using pygmentize? I'm opening it using a command of the form xterm -e "pygmentize -g <filename>" and am finding that the only way to scroll is to use the scroll bar. I tried piping the output to less but that resulted in garbage output.

                                      – Leo Simon
                                      Nov 3 '16 at 14:58











                                    • yes you can use pygmentize with less. Create a bash function lessc as following: lessc () { pygmentize -gf terminal "$1" | less -R }

                                      – souravc
                                      Nov 3 '16 at 17:04














                                    3












                                    3








                                    3







                                    cat can not produce syntax highlighting solely. Still you can do this as follows, using python-pygments. First install it from terminal as,



                                    sudo apt-get install python-pygments


                                    Now copy the function below ~/.bashrc. It will give you what you want moreover it will preserve the properties of cat otherwise there is no point of using cat



                                    catc(){
                                    cat "$@" > /tmp/.tmp
                                    pygmentize -g /tmp/.tmp
                                    rm /tmp/.tmp
                                    }


                                    Source ~/.bashrc as,



                                    . ~/.bashrc


                                    It will give colourized output,



                                    catc <filename>


                                    It will concatenate with color as well,



                                    catc <file1> <file2> ... <filen>





                                    share|improve this answer















                                    cat can not produce syntax highlighting solely. Still you can do this as follows, using python-pygments. First install it from terminal as,



                                    sudo apt-get install python-pygments


                                    Now copy the function below ~/.bashrc. It will give you what you want moreover it will preserve the properties of cat otherwise there is no point of using cat



                                    catc(){
                                    cat "$@" > /tmp/.tmp
                                    pygmentize -g /tmp/.tmp
                                    rm /tmp/.tmp
                                    }


                                    Source ~/.bashrc as,



                                    . ~/.bashrc


                                    It will give colourized output,



                                    catc <filename>


                                    It will concatenate with color as well,



                                    catc <file1> <file2> ... <filen>






                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited Jan 16 '14 at 3:33

























                                    answered Jan 15 '14 at 18:12









                                    souravcsouravc

                                    28.1k1378109




                                    28.1k1378109













                                    • Is there a way to enable scrolling with up and down arrows when using pygmentize? I'm opening it using a command of the form xterm -e "pygmentize -g <filename>" and am finding that the only way to scroll is to use the scroll bar. I tried piping the output to less but that resulted in garbage output.

                                      – Leo Simon
                                      Nov 3 '16 at 14:58











                                    • yes you can use pygmentize with less. Create a bash function lessc as following: lessc () { pygmentize -gf terminal "$1" | less -R }

                                      – souravc
                                      Nov 3 '16 at 17:04



















                                    • Is there a way to enable scrolling with up and down arrows when using pygmentize? I'm opening it using a command of the form xterm -e "pygmentize -g <filename>" and am finding that the only way to scroll is to use the scroll bar. I tried piping the output to less but that resulted in garbage output.

                                      – Leo Simon
                                      Nov 3 '16 at 14:58











                                    • yes you can use pygmentize with less. Create a bash function lessc as following: lessc () { pygmentize -gf terminal "$1" | less -R }

                                      – souravc
                                      Nov 3 '16 at 17:04

















                                    Is there a way to enable scrolling with up and down arrows when using pygmentize? I'm opening it using a command of the form xterm -e "pygmentize -g <filename>" and am finding that the only way to scroll is to use the scroll bar. I tried piping the output to less but that resulted in garbage output.

                                    – Leo Simon
                                    Nov 3 '16 at 14:58





                                    Is there a way to enable scrolling with up and down arrows when using pygmentize? I'm opening it using a command of the form xterm -e "pygmentize -g <filename>" and am finding that the only way to scroll is to use the scroll bar. I tried piping the output to less but that resulted in garbage output.

                                    – Leo Simon
                                    Nov 3 '16 at 14:58













                                    yes you can use pygmentize with less. Create a bash function lessc as following: lessc () { pygmentize -gf terminal "$1" | less -R }

                                    – souravc
                                    Nov 3 '16 at 17:04





                                    yes you can use pygmentize with less. Create a bash function lessc as following: lessc () { pygmentize -gf terminal "$1" | less -R }

                                    – souravc
                                    Nov 3 '16 at 17:04











                                    0














                                    Other answers cover why cat is not able to do it. Though you can do it with less using
                                    lesspipe.sh.






                                    share|improve this answer




























                                      0














                                      Other answers cover why cat is not able to do it. Though you can do it with less using
                                      lesspipe.sh.






                                      share|improve this answer


























                                        0












                                        0








                                        0







                                        Other answers cover why cat is not able to do it. Though you can do it with less using
                                        lesspipe.sh.






                                        share|improve this answer













                                        Other answers cover why cat is not able to do it. Though you can do it with less using
                                        lesspipe.sh.







                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered Jan 21 '14 at 22:23









                                        Fuad SaudFuad Saud

                                        1563




                                        1563























                                            0














                                            Bat — A cat clone with wings



                                            You might also want to check out bat which has the following features:




                                            • Syntax highlighting

                                            • Git integration

                                            • Optionally showing non-printable characters

                                            • Automatic paging with less

                                            • File concatenation as a drop-in replacement for cat when redirected


                                             Preview



                                            Screenshot of bat showing a colored file



                                            Picture taken from the official GitHub



                                            Installation



                                            You can get the latest Debian package here and install it with:



                                            sudo dpkg -i bat_0.10.0_amd64.deb


                                            Adapting the version number and architecture.






                                            share|improve this answer




























                                              0














                                              Bat — A cat clone with wings



                                              You might also want to check out bat which has the following features:




                                              • Syntax highlighting

                                              • Git integration

                                              • Optionally showing non-printable characters

                                              • Automatic paging with less

                                              • File concatenation as a drop-in replacement for cat when redirected


                                               Preview



                                              Screenshot of bat showing a colored file



                                              Picture taken from the official GitHub



                                              Installation



                                              You can get the latest Debian package here and install it with:



                                              sudo dpkg -i bat_0.10.0_amd64.deb


                                              Adapting the version number and architecture.






                                              share|improve this answer


























                                                0












                                                0








                                                0







                                                Bat — A cat clone with wings



                                                You might also want to check out bat which has the following features:




                                                • Syntax highlighting

                                                • Git integration

                                                • Optionally showing non-printable characters

                                                • Automatic paging with less

                                                • File concatenation as a drop-in replacement for cat when redirected


                                                 Preview



                                                Screenshot of bat showing a colored file



                                                Picture taken from the official GitHub



                                                Installation



                                                You can get the latest Debian package here and install it with:



                                                sudo dpkg -i bat_0.10.0_amd64.deb


                                                Adapting the version number and architecture.






                                                share|improve this answer













                                                Bat — A cat clone with wings



                                                You might also want to check out bat which has the following features:




                                                • Syntax highlighting

                                                • Git integration

                                                • Optionally showing non-printable characters

                                                • Automatic paging with less

                                                • File concatenation as a drop-in replacement for cat when redirected


                                                 Preview



                                                Screenshot of bat showing a colored file



                                                Picture taken from the official GitHub



                                                Installation



                                                You can get the latest Debian package here and install it with:



                                                sudo dpkg -i bat_0.10.0_amd64.deb


                                                Adapting the version number and architecture.







                                                share|improve this answer












                                                share|improve this answer



                                                share|improve this answer










                                                answered Apr 6 at 22:54









                                                Andrea LazzarottoAndrea Lazzarotto

                                                5,94822649




                                                5,94822649






























                                                    draft saved

                                                    draft discarded




















































                                                    Thanks for contributing an answer to Ask Ubuntu!


                                                    • Please be sure to answer the question. Provide details and share your research!

                                                    But avoid



                                                    • Asking for help, clarification, or responding to other answers.

                                                    • Making statements based on opinion; back them up with references or personal experience.


                                                    To learn more, see our tips on writing great answers.




                                                    draft saved


                                                    draft discarded














                                                    StackExchange.ready(
                                                    function () {
                                                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f405960%2fcan-cat-show-files-using-code-markup-in-colors%23new-answer', 'question_page');
                                                    }
                                                    );

                                                    Post as a guest















                                                    Required, but never shown





















































                                                    Required, but never shown














                                                    Required, but never shown












                                                    Required, but never shown







                                                    Required, but never shown

































                                                    Required, but never shown














                                                    Required, but never shown












                                                    Required, but never shown







                                                    Required, but never shown







                                                    Popular posts from this blog

                                                    數位音樂下載

                                                    When can things happen in Etherscan, such as the picture below?

                                                    格利澤436b