Search code examples
macoszshaliasoh-my-zshzshrc

Zsh shell alias


I'm using Zsh on Mac OS Terminal as my default shell and using .zshrc file for updating the aliasing

One of the command I am trying to update is cat. I want to print the output of the command with syntax highlighting and row number. So when I type cat <filename> it should be aliased to pygmentize -g <filename> | nl -b a

Different methods I've tried so far, Method #1: alias cat="pygmentize -g $1 | nl -b a"

This is printing the file content with row numbers right away but not returning for a while. So when I pressed Ctrl+C I've got below error.

  Traceback (most recent call last):
  File "/usr/local/bin/pygmentize", line 33, in <module>
    sys.exit(load_entry_point('Pygments==2.7.3', 'console_scripts', 'pygmentize')())
  File "/usr/local/Cellar/pygments/2.7.3/libexec/lib/python3.9/site-packages/pygments/cmdline.py", line 557, in main
    return main_inner(popts, args, usage)
  File "/usr/local/Cellar/pygments/2.7.3/libexec/lib/python3.9/site-packages/pygments/cmdline.py", line 408, in main_inner
    code = sys.stdin.buffer.read()  # use .buffer to get a binary stream
KeyboardInterrupt

Method #2: function cat() { pygmentize -g $1 | nl -b a }

Same output and when pressed Ctrl+C same stack trace like above.

Could someone please help me figure this out?


Solution

  • remove the "function" keyword and it should work fine:

    cat() {
        pygmentize -g $1 | nl -b a
    }
    

    If you want to make it scroll a little nicer:

    cat() {                      
        pygmentize -g $1 | nl -b a | less -Rai
    }
    

    I'd also recommend calling it something slightly different like ccat, as cat can be useful still being easily accessible by any scripts you may run.