Skip to main content

Long line grep

Sometimes when you grep or rg through a codebase among many others you get a search result that is a huge one-liner and shadows other search results moving them out of the screen. Search result can be a non pretty formatted json or something else. Assuming you want search for search_string, preserve the coloring and still be able to see all search results in one terminal screen. Let’s assume 200 symbols is enough. Having that in mind we can pipe grep output to cut.

grep -rinI search_string --color=always | cut -c 1-200

And of course one can create a custom function that uses grep with the 200 symbol cut. Just put this into your .bashrc or .bash_profile (.zsh, .zsh_profile if you use zsh) and you are good to go. Source you rc file or just relogin with a new terminal and issue is resolved.

sgrep() {
    grep -rinI --color=always "$1" | cut -c 1-200
}