Something for the emacs users! πŸ˜„

This is a small script for all fellow emacs users. :smile:

It starts emacs by adjusting the window size of the emacs window according to the contents of given file.
For example:

  emacs ~/.bashrc

will start emacs with the window size (width and height) suited for the contents of ~/.bashrc.

If you think it is worth while, you can write it into your ~/.bashrc, or (preferably) into an executable file on your PATH, like /usr/local/bin/emacs.

You can also modify the limits given in the script (variables wmax, wmin, hmax and hmin) to suit better to your monitor size.

Enjoy! :vulcan_salute:t2:

#!/bin/bash

# Start 'emacs' by
#  - adjusting window width and height according to document's contents
#  - using reverse video colors for having dark window background

_emacs_geometry() {
    # Adjust emacs window size based on the file contents.

    local wmax=300     # max width in characters
    local wmin=80      # min width in characters
    local hmax=120     # max height in characters
    local hmin=35      # min height in characters
    local w h
    local doc="$(cat "$arg")"

    w=$(echo "$doc" | /usr/bin/wc -L)
    ((w+=7))                         # 5 is minimum?
    test $w -gt $wmax && w=$wmax
    test $w -lt $wmin && w=$wmin

    h=$(echo "$doc" | /usr/bin/wc -l)
    test $h -gt $hmax && h=$hmax
    test $h -lt $hmin && h=$hmin

    args+=("--geometry=${w}x$h")
}

emacs()
{
    local arg
    local args=(--reverse-video)  # use "dark" background

    for arg in "$@"
    do
        case "$arg" in
            -*) ;;
            *)  test -r "$arg" && _emacs_geometry ;;
        esac
        args+=("$arg")
    done

    /usr/bin/emacs "${args[@]}" >& /dev/null &
}

emacs "$@"
7 Likes

I’ve been a casual Linux users since the late 1990s but I’ve recently retired so playing with Linux a lot.

Could you share what you do with emacs. I love the look but not sure that I would ever use it.

Thanks

You can do a lot with emacs, but the time investment to learn it is quite a bit.

1 Like

Thanks for the link, I’ll watch that video.

Yeah, I know it’s going to take time, that’s why I mentioned I’m retired, LoL.

1 Like