kludge to avoid screen cluttering by less, vim and others.

Discussion in 'Unix & X11' started by McGeek, Jan 6, 2008.

  1. McGeek

    uix_expand uix_collapse

    Dec 29, 2007
    Likes Received:
    I have written a script which leaves the screen uncluttered when finishing less.
    This script is written for bash in the "good old terminal", but works well with
    iTerm too. I wrote this script because less is opposite of more as less is so
    much more as more is so much less.

    I hope you will use this script and reap the rewards of using less while reading
    textfiles, gaining from less's features, avoiding the cluttering which may have
    made you disliking using less. You can make a copy of the script and modify and wrap
    the script around any other characterbased program which clutters your terminal screen.

    The script works by beeing placed in your ~/bin which I assume is before
    /usr/bin in your $PATH where the binary less resides. You must modify the paths
    in the script if they are different from that. (both the binary less, the script,
    and the kludge.scr)

    The script installs an interrupthandler which are triggered by changing the
    windowsize. The interupthandler figures out what it must do to preserve
    your screen when you exit less, and just does so, except for four characters
    to the extreme left on one line. (wich may well be part of your prompt).
    The interrupthandler gets its work done, by calling a kludge which are
    relatively referenced in the script from your homefolder, -presumes ~/bin
    - YOU MUST EDIT THE SCRIPT OTHERWISE. The configuration is like it is because
    that is what it takes to make the correct things happen in bash.
    I think this could have been accomplished much easier using another shell,
    but most people uses bash, especially newcomers, and they deserve to have
    it as easy as possible, while reaping the productivity gains laying dormant
    in the Unix core, so I hope you will share the script with you liberally,
    if you think it is worth the time and the work it takes to "install" it.
    I hope you will give this script a try, as to make less work comfortably for you,
    I have included an environment variable with all settings I like in less, which
    you may modify.

    Less was the first program I had that made me think "wow" back in 1986, beeing used
    to the "more" command, - which was, and is so much less than less. You can for instance
    invoke BBedit with a file you are viewing in less by pressing "v" if you have BBedit
    specified in the $EDITOR variable. You can pipe some text to the clipboard.
    Or you can pipe some lines out of a document you are viewing in less and into
    a file while viewing, you can load it with multiple files, and search them all,
    a programemer can make less work with tagfiles; you can have less create a logfile
    of what you read and, you can even scroll backwards. All in all less is a very handy
    tool which I think everybody would gain from using,in opposite to more.
    Especially when it leaves the screen uncluttered.

    Great care have been taken in order to make this kludge work properly.
    You may do whatever you wish to do with it, aside from selling it alone, but you
    are free to do whatever that you please with it, aside from distributing
    malfunctioning copies or incomplete copies or tinker with the Copyright notice
    in the scripts.
    -------------------------------------------------- here comes the kludge for less - sends with you a tar as well. 8859 - encoded I think.

    #! /bin/bash
    # Less - lets us keep our screen nice even after resize, having used less or any other
    # character based program - like vim, which may leave an unorderly screen.
    # The fact that programs do clutter up the screen is because they probably didn't figure
    # that we one day would be able to resize our terminals when they specified the standards at
    # Ansi back in the 60's.

    # Installing : put it together with "kludge.bash" in your $HOME/bin folder ( ~/bin ).
    # its intended to work with the bash shell under MacOsX, the binary less is supposed
    # to reside in /usr/bin, if it isnt; ("which less" reveals where), adjust the path.

    # less - to be placed in the ~/bin folder is the wrap around less to make it behave
    # Copyright 2008 Tommy Bollman Public Domain. -No WARRANTIES ABOUT WHAT SO EVER-
    # Please do modify it for other programs which need helps with its cleanup as well.
    # ~ is an expansion for your home directory aka /Users/John\ Doe
    # Please document your version properly if you are posting it, relieving others.

    export LESS=" -I -r -f -J -S -g -M -x 4"
    # -I ignore case when searching
    # -r "raw" do not preparate ctrl-chars,
    # -f force open special files (may be binary) BEWARE OF ANSISEQUENCES.
    # -J show status column
    # -S chop long lines.
    # -g highlight on last hit in the search.
    # -M Most Verbose status column...
    # -x 4 tabspacing = 4
    # -------------------------------------- the screen handling starts here.................

    ESC=`printf "\e"`
    ScreenRedraw_off=`echo -n "$ESC""[8m"`
    ScreenRedraw_on=`echo -n "$ESC""[0m"`

    function OkayScreen()
    export PS1="" # Turns off the prompt to avoid cluttering..
    echo -n ${ScreenRedraw_off}
    CURLINES=`bash -i < ~/bin/kludge.bash `
    # ^^^^^^^^^^^ NB! the path where kludge.bash should be placed.
    if [ $CURLINES -gt $ORIGLINES ] ; then
    TO_SKIP="$(expr "$CURLINES" '-' "$ORIGLINES")"
    if [ $TO_SKIP -lt 3 ] ; then
    TO_SKIP="$(expr "$TO_SKIP" '-' '2')"
    TO_SKIP="$(expr "$TO_SKIP" '-' '1')"
    tput cuu 1 #cursor up one line
    echo -n ${ScreenRedraw_on}
    echo -n "\$####" #restores an erased '$' making only 3 chars disappear.
    # ^ $ = prompt - $PS1. .(I have just a dollar here but if yours is longer,
    # you can add the first four if it's static, and you'll loose nothing!!)
    echo -n ${ScreenRedraw_off}
    tput cud $TO_SKIP # move cursor to where it should be.
    echo -n ${ScreenRedraw_on}
    echo # activate the cli at correct position.
    tput cuu 2
    echo ${ScreenRedraw_on}
    trap OkayScreen SIGWINCH
    /usr/bin/less $@
    # ^^^^^^^^ NB! The path where the BINARY less is installed.
    trap '' SIGWINCH

    -------------------------------------------------------------------------------- and here is the kludge wich makes it all work!
    #! /bin/bash
    # kludge.scr - to be placed in the ~/bin folder is the inner workings of the bash script named less
    # Copyright 2008 Tommy Bollman
    shopt -s checkwinsize
    echo $LINES

    Attached Files:

Share This Page