Search code examples
linuxvimpager

vim as pager for journalctl


I would like to use vim as pager for journalctl. Is it possible and how?

I am on fedora, when I naively open journalctl, it is a default pager, I am unable to go to last line quickly. I have tried PAGER="vim -" journalctl as found on a forum without any change.

Using export SYSTEMD_PAGER=cat effectively use cat as pager, but I have been unable to use export SYSTEMD_PAGER=vim.

How would you read your journal within vim?


Solution

  • Try piping to vim:

    journalctl -b | vim - 
    

    Use Priorities to Filter Output

    Typically, journalctl will color code log entries depending on whether they are errors, warnings, info, etc. These colors correspond to priorities. This color coding will not be present in the logs piped to vim.

    A simple way to search for logs with a particular priority is to use the journalctl option -p:

    journalctl -b -p 3 | vim -
    

    where 3 corresponds to errors.

    You can use this table to determine which priority to use.

    Priority Log Level
    emerg 0
    alert 1
    crit 2
    err 3
    warning 4
    notice 5
    info 6
    debug 7

    See the --priority= option in the journalctl manual page for more information.

    Use Color Coding in Vim

    If you still want to see color coded logs in Vim, that is possible.

    The underlying basis for this are ANSI escape codes. It is not necessary to know anything about ANSI escape codes.

    But, to get color to work, we need to solve two problems:

    1. Instruct journalctl to output ANSI escape codes.
    2. Format ANSI escape codes in Vim.

    Instruct journalctl to output ANSI escape codes

    The manual page for journalctl mentions an environment variable which will produce the desired output.

    Set the SYSTEMD_COLORS environment variable to true in your shell:

    export SYSTEMD_COLORS=true
    

    You can make this variable permanent by adding it to your ~/.bashrc

    Now, when you run journalctl, you will notice ANSI escape codes in vim:

    journalctl -b -p 4 | vim -
    

    Example of ANSI escape code:

    ^[[0;1;38;5;185m

    Or, if output to the shell, logs will be color coded there:

    journalctl -b -p 4 -n 10 --no-pager
    

    Format ANSI escape codes in Vim

    Vim requires a plugin to interpret ANSI escape codes. More information can be found in the links below.

    The file you need can be found here (direct).

    From a shell, open the downloaded file with vim:

    vim AnsiEsc.vba.gz
    

    Then, within vim, install the plugin:

    :so %
    

    You can quit vim after installing the plugin:

    :q 
    

    Color Example

    After setting the environment variable and installing the plugin, try viewing color coded logs.

    From the shell, pipe color coded logs to vim:

    journalctl -b -p 4 | vim -
    

    From vim, activate the plugin to format the logs:

    :AnsiEsc
    

    Now you should see color coded systemd logs in vim.

    Regards,

    References

    journalctl | Arch manual pages

    ANSI escape code | Wikipedia

    env, setenv, & export | Unix & Linux

    bash: true or false | Stack Overflow

    Permanently Setting Environmental Variables | DigitalOcean

    Can colorized output be captured via shell redirect? | Stack Overflow

    ANSI Color Codes in VIM | Stack Overflow

    Introduction to Plugins | Vim Online Help

    AnsiEsc Plugin | vim.org (Outdated AnsiEsc.vba.gz)

    AnsiEsc | Dr Chip's Vim Page (Find the latest AnsiEsc.vba.gz here.)

    AnsiEsc Manual | Dr Chip's Vim Page

    vimball | vim.org

    :so | Vim Online Help

    % | Vim Online Help