Update Alternative

We will use Update Alternatives here, which is explained best by the first 2 man pages paragraphs (http://manpages.ubuntu.com/manpages/hardy/man8/update-alternatives.8.html):

update-alternatives creates, removes, maintains and displays
information about the symbolic links comprising the Debian alternatives

It is possible for several programs fulfilling the same or similar
functions to be installed on a single system at the same time. For
example, many systems have several text editors installed at once.
This gives choice to the users of a system, allowing each to use a
different editor, if desired, but makes it difficult for a program to
make a good choice for an editor to invoke if the user has not
specified a particular preference.

Check out the lesson on Update Alternatives Below.

Update update-alternatives of vim to new vim (statically compiled vim)

In the mean time here is an example of updating all of the alternatives/links of every editor to point at a new vim you compiled. Note that this only works if all of the editors have a fancy keyword like “vim.tiny” or “vim.basic” or “vim” and we want to change them all to “vim.static” (our new one).

Lets say you followed instructions here (https://raymii.org/s/blog/Statically_cross_compiled_vim_for_x86_x86-64_and_mips.html) to make a new Vim compilation (such as a statically linked one as in the link, so that it doesnt depend on libraries – note it might still depend on glibc according to this link: https://github.com/ericpruitt/static-vim )

# to sum it up I compiled it like so:

# ... im root for all of this, but you dont have to be ... #

# first install dependencies and building tools
apt-get update
apt-get install libncurses5-dev git build-essential
apt-get install libc6-dev-i386
# change to directory where vim source code will download
cd /root
# download sources
git clone https://github.com/b4winckler/vim.git
# go to compilation directory
cd vim/src
# optionally download other vim sources (its okay if this does nothing):
apt-get build-dep vim
# set some compile flags/options. Run "nproc" or "cat /proc/cpuinfo" and count up your CPUs, if you have 4. Put 4 with -j4. If oyu have 2 put -j2. This will speed up compilation by spreading it out on more CPUs/threads.
export LDFLAGS="-static"
export MAKEFLAGS="-j4"
export CFLAGS=""
# For compilation we need to run "make" which looks at a "Makefile" for recipes/instructions on compilation. Different systems and options of compilation require different Makefiles. So a "Makefile" is usually generated by a "./configure" script. Simple programs dont have ./configure, they just have a trivial Makefile that would work on most systems.
# In the link --with-features=mini, And I want all of the features so I put --with-features=huge. Also I do the same other options because im compiling on a simple storage server without X (without a monitor/display abilities installed). If you are compiling on a server with X then perhaps you can omit --with-x=no and --disable-gui.
./configure --with-features=huge  --with-compiledby='infotinks <boss@infotinks.com>'  --with-x=no  --disable-gui  --disable-netbeans  --disable-pythoninterp  --disable-python3interp  --disable-rubyinterp  --disable-luainterp
# The above command will take a minute to generate a "Makefile", if there are errors you must take care of them (by googling) as the next step wont work.
# when configure is successfully done, run make to compile everything
# this will take a few minutes, and if it finished succesfully you will know as you wont see errors. Warnings are fine. If you have errors it will not make the final vim executable. Address the errors the same way as you did with configure, using google.
# test that your new vim works
# exit out of vim using ESCAPE :q! ENTER
# we will be renaming it vim.static and "installing" it to /usr/bin/ and pointing all of the editors to it. NOTE: installing just means copying the executable to a folder which belongs to the system variable PATH {run this in a shell to see all oyur PATH folders "echo $PATH"} and as you should know every file in the folders listed in the PATH can be called without using path. so I will be able to say "vim.static" from any folder and it will call "/usr/bin/vim.static.", I wont have to call "/usr/bin/vim.static", which saves many keystrokes.


Ideally as a last step you would like to install it.
Im assuming your in the vims source code src folder (that you compiled in). So your in vim/src. And in there is a compiled version of vim. (NOTE as per the instructions I assumed you didnt run “make install”, which would do similar steps to below).

cp vim /usr/bin/vim.static

So now you have your new vim here /usr/bin/vim.static

Now you will notice that most of your apps point to an old vim.

# update-alternatives --get-selections
awk                            auto     /usr/bin/mawk
c++                            auto     /usr/bin/g++
c89                            auto     /usr/bin/c89-gcc
c99                            auto     /usr/bin/c99-gcc
cc                             auto     /usr/bin/gcc
cpp                            auto     /usr/bin/cpp
editor                         auto     /usr/bin/vim.basic
ex                             auto     /usr/bin/vim.basic
ifenslave                      auto     /sbin/ifenslave-2.6
libblas.so.3                   auto     /usr/lib/libblas/libblas.so.3
lua-compiler                   auto     /usr/bin/luac5.1
lua-interpreter                auto     /usr/bin/lua5.1
lzma                           auto     /usr/bin/xz
ncftp                          auto     /usr/bin/ncftp3
pager                          auto     /bin/more
pico                           auto     /bin/nano
rcp                            auto     /usr/bin/scp
rename                         auto     /usr/bin/prename
rlogin                         auto     /usr/bin/slogin
rmt                            auto     /usr/sbin/rmt-tar
rsh                            auto     /usr/bin/ssh
rview                          auto     /usr/bin/vim.basic
rvim                           auto     /usr/bin/vim.basic
vi                             auto     /usr/bin/vim.basic
view                           auto     /usr/bin/vim.basic
vim                            auto     /usr/bin/vim.basic
vimdiff                        auto     /usr/bin/vim.basic
w                              auto     /usr/bin/w.procps

NOTE: the above output lists all of the alternatives and their current active targets. For example the alternative “editor” (which is located in /usr/bin/editor, you specify this upon installation of alternative) points to “/usr/bin/vim.basic”. so that means “/usr/bin/vim.basic” is launched everytime “editor” is ran. Also note that “usr/bin/vim.basic” is the current target of “editor” because “vim.basic” has the current highest priority between the other targets (vim.tiny, vim.basic). If we were to install a new target to editor with a highest priotity it would be set (launching editor would launch the new target). Note that an alternative can also be set to another target (not just its highest priority target) by using –config or –set, this will change its status from an auto (automatic) alternative to a manual alternative. With a manual alternative the current active target is the one you set, and not necessarily the one with the highest priority.

To update one of the apps, we just need to follow the 2 steps. For example lets say we want to change “editor” (which is opened by things like “crontab -e”, the crontab editor – so we will set the crontab editor to our new vim) to our new “vim.static”. First we install vim.static as an available “alternative” for editor. editor has many different alternative one of them is vim.basic, one of them is nano, one of them is vim.tiny. By default we also install a priority number, the highest the better (it picks it).

Step 1 install vim.static as an alternative for editor with priority 50:

# update-alternatives --verbose --install /usr/bin/editor editor /usr/bin/vim.static 50


Step 2 set vim.static as the current alternative for editor (meaning when “editor” is run “vim.static” will run):

# update-alternatives --set editor /usr/bin/vim.static


# update-alternatives --config editor

Then press the # associated for /usr/bin/vim.static and the enter key
NOTE: If you picked a high enough priority on step 1, you don’t even need to do step 2, as the default editor is set as the highest one in an auto alternative. (more on auto and manual alternatives below in lesson section)

Change all vim.basic to point to vim.static

Now lets change all of the things that point at vim.basic to point at vim.static.

# this changes every alternative seen in "update-alternatives --get-selections" with a specific target to a new target.
# before pasting: change OLD_TARGET and NEW_TARGET and NEW_PRIO to match your needs

(OLD_TARGET="/usr/bin/vim.basic"; NEW_TARGET="/usr/bin/vim.static"; NEW_PRIO=50; echo "*** BEFORE ***"; update-alternatives --get-selections; echo "*********"; IFS=$'\n'; for ALTERNATIVE in `update-alternatives --get-selections | grep ${OLD_TARGET} | awk '{print $1}'`; do update-alternatives --verbose --install `which ${ALTERNATIVE}` ${ALTERNATIVE} ${NEW_TARGET} ${NEW_PRIO}; update-alternatives --set ${ALTERNATIVE} ${NEW_TARGET}; done; echo "*** AFTER ***"; update-alternatives --get-selections)


Lesson on Update Alternative

To make a your own alternative program, such as “kosedit”, which will launch one of your many editors (which ever one is currently set). You can do this:

# update-alternatives --verbose --install /usr/bin/kosedit kosedit /usr/bin/vim.tiny 10
# update-alternatives --verbose --install /usr/bin/kosedit kosedit /usr/bin/vim.basic 20
# update-alternatives --verbose --install /usr/bin/kosedit kosedit /usr/bin/vim.static 5


Even though we processed vim.static last, it is now the active kosedit. The higher priority is set as the current one in auto mode (as we can see each alternative is by default in “auto” mode).

# update-alternatives --get-selections | grep kosedit
kosedit auto /usr/bin/vim.basic


So now everytime I call kosedit it will launch /usr/bin/vim.basic. kosedit will work because its in our PATH (/usr/bin). Whats the point of all of this? Well hopefully kosedit is less typing then vim.basic. Also this helps point many editors that other programs use towards your edit. For example we all know that the crontab editor uses editor (/usr/bin/editor), so we could either “cp /usr/bin/vim.static /usr/bin/editor” but thats not a pretty way, or we could “ln -s /usr/bin/vim.static /usr/bin/editor”, but instead there is a program for this, so lets just use it (update-alternatives).

If I reinstall vim.static with priority 50 it will be the defaults

# update-alternatives --verbose --install /usr/bin/kosedit kosedit /usr/bin/vim.static 50

# update-alternatives --get-selections | grep kosedit
kosedit auto /usr/bin/vim.static


How to manually pick an alternative?

# update-alternatives --config kosedit
There are 3 choices for the alternative kosedit (providing /usr/bin/kosedit).

Selection Path Priority Status
* 0 /usr/bin/vim.static 50 auto mode
1 /usr/bin/vim.basic 20 manual mode
2 /usr/bin/vim.static 50 manual mode
3 /usr/bin/vim.tiny 10 manual mode

Press enter to keep the current choice[*], or type selection number:

Then I type 0,1,2,3 to pick which ever one. That will change my status to “static”

For example, I picked 1, vim.basic:

# update-alternatives --get-selections | grep kosedit
kosedit manual /usr/bin/vim.basic

How to go auto pick an alternative (switch from manual to auto)?

# update-alternatives --auto kosedit

Now the highest priority alternative will win. Which is vim.static.

Leave a Reply

Your email address will not be published. Required fields are marked *