thiagowfx
thiagowfx

Reputation: 5760

Creating an alias in bash to less.sh (less.vim)

I am trying to create an alias in bash to use the built-in less.sh from vim. My goal is to do something such as:

alias vless="/usr/local/Cellar/vim/8.0.0577/share/vim/vim80/macros/less.sh"

with the requirement to work at least on Linux and on MacOS (with or without homebrew).

The above path is from vim installed with homebrew; however, it is very inconvenient, since:

How can I make such an alias taking these two points into account?

Is there any switch like vim --installation-prefix or so to get the path where vim macros are installed on? Any other suggestions will be appreciated.

Note: I am aware, however I am not interested, in vimpager or in view (=vim -R).

Upvotes: 0

Views: 886

Answers (3)

Carl
Carl

Reputation: 1029

alias vless=`vim -e -T dumb --cmd 'exe "set t_cm=\<C-M>"|echo $VIMRUNTIME|quit' | tr -d '\015' `/macros/less.sh

It's close to @yolenoyer's excellent answer, but defines the alias directly instead of invoking a script, is simple and omits the tests/checks.

Tested on BigSur with vim installed with homebrew.

Upvotes: 0

Cole
Cole

Reputation: 751

Not sure if I have a good solution for your second bullet point, but as far as avoiding hardcoding path names, here is a hack my server admin uses:

find /usr/share/vim -name less.sh | tail -n 1

The result on our server: /usr/share/vim/vim72/macros/less.sh

If you wanted to put this in something like a bashrc

VLESS_CMD=`find /usr/share/vim -name less.sh | tail -n 1`
if [ -n "$VLESS_CMD" ]; then
    alias vless=$VLESS_CMD
fi

Upvotes: 0

yolenoyer
yolenoyer

Reputation: 9445

In :help $VIMRUNTIME, you can read:

In case you need the value of $VIMRUNTIME in a shell (e.g., for a script that greps in the help files) you might be able to use this:

VIMRUNTIME=`vim -e -T dumb --cmd 'exe "set t_cm=\<C-M>"|echo $VIMRUNTIME|quit' | tr -d '\015' `

So you could replace your alias with such a Bash script:

#!/bin/bash

vimruntime=`vim -e -T dumb --cmd 'exe "set t_cm=\<C-M>"|echo $VIMRUNTIME|quit' | tr -d '\015' `
[[ -z $vimruntime ]] && { echo 'Sorry, $VIMRUNTIME was not found' >&2; exit 1; }

vless=$vimruntime/macros/less.sh
[[ -x $vless ]] || { echo "Sorry, '$vless' is not accessible/executable" >&2; exit 1; }

$vless "$@"

( Tested with a "basic" linux vim installation, not with a homebrew installation. )

Upvotes: 3

Related Questions