view is just vim

January 24, 2025

Report a bug

I recently found out accidentally at work that vim and view were the same thing when I happened to be editing a file on view instead of my beloved vim editor.

Note: This is a follow up post from my microblog

For context, view is often used in lieu of vi when trying to open a file for read only while retaining the same shortcuts as vi. This is why it surprised me to see that I could modify a file when view was supposed to be read only. If we were to take a look at the documentation:

$ man -Leng view
VIM(1)                                                                  General Commands Manual                                                                  VIM(1)

NAME
       vim - Vi IMproved, a programmer's text editor

SYNOPSIS
       vim [options] [file ..]
       vim [options] -
       vim [options] -t tag
       vim [options] -q [errorfile]

       ex gex
       view
       gvim gview vimx evim eview
       rvim rview rgvim rgview

Interestingly, the man pages for view points to vim and we can see all sorts of different types of editors listed along with it such as gvim. AIX 7.3 documentation states that view Starts the vi editor in read-only mode. This is indeed evident when I take a look at how view is defined in my system (Fedora 41):

$ cat /usr/bin/view
#!/usr/bin/sh

# run vim -R if available
if test -f /usr/bin/vim
then
  exec /usr/bin/vim -R "$@"
fi

# run vi otherwise
exec /usr/libexec/vi -R "$@"

where -R is a flag for Read-only mode:

       -R          Read-only  mode.  The 'readonly' option will be set.  You can still edit the buffer, but will be prevented from accidentally overwriting a file.  If
                   you do want to overwrite a file, add an exclamation mark to the Ex command, as in ":w!".  The -R option also implies the -n option (see above).  The
                   'readonly' option can be reset with ":set noro".  See ":help 'readonly'".

Vim Oddities

What I found particularly odd was how at work, on one system view was simply a symlink to vi

$ realpath view
/usr/bin/vi

while on another machine, the two had the same md5sum (the md5sum is for illustration purposes, I just replicated the behavior on my local machine):

zaku@fedora:/usr/bin$ md5sum view
8fe562f5dd43b70c38f10ee2ec3310ca  view
zaku@fedora:/usr/bin$ md5sum vim
8fe562f5dd43b70c38f10ee2ec3310ca  vim

This odd behavior made me confused so I decided to make an experiment seeing how the only difference between view and vim on both systems at work was their names:

$ ln -s /usr/bin/vim view-pika
$ ls -l view-pika
lrwxrwxrwx. 1 zaku zaku 12 22 janv. 22:52 view-pika -> /usr/bin/vim

And it BEHAVED THE SAME as view. Thus I concluded, vim behaves differently depending on the name of the command being executed. More precisely, if the program started with the name view then it would open vim as read-only by taking a look at argv[0]. Upon looking at the source code on Github under main.c::parse_command_name():

    if (STRNICMP(initstr, "view", 4) == 0)

where initstr = gettail((char_u *)parmp->argv[0]); as suspected. This explains why pika-view did not work but view-pika worked. It only compared the first 4 characters of argv[0] to see if it starts with view. If you inspect the code more, you’ll see that vim has many faces.

This behavior is entirely documented on the man pages which I never noticed:

Vim behaves differently, depending on the name of the command (the executable may still be the same file).

       vim       The "normal" way, everything is default.

       ex        Start in Ex mode.  Go to Normal mode with the ":vi" command.  Can also be done with the "-e" argument.

       view      Start in read-only mode.  You will be protected from writing the files.  Can also be done with the "-R" argument.

       gvim gview
                 The GUI version.  Starts a new window.

       gex       Starts a new gvim window in Ex mode. Can also be done with the "-e" argument to gvim

       vimx      Starts gvim in "Vi" mode similar to "vim", but with additional features like xterm clipboard support

       evim eview
                 The GUI version in easy mode.  Starts a new window.  Can also be done with the "-y" argument.

       rvim rview rgvim rgview
                 Like the above, but with restrictions.  It will not be possible to start shell commands, or suspend Vim.  Can also be done with the "-Z" argument.

Extra Random Information on VIM and VI

1.) Viewing Compilation Flags

That was all I wanted to look at in regards to view and vim. One interesting timbit about vim is that you can see what it appears to be the compilation flag by running: vim --version:

         fichier vimrc système : "/etc/vimrc"
     fichier vimrc utilisateur : "$HOME/.vimrc"
  2e fichier vimrc utilisateur : "~/.vim/vimrc"
  3e fichier vimrc utilisateur : "~/.config/vim/vimrc"
      fichier exrc utilisateur : "$HOME/.exrc"
 fichier de valeurs par défaut : "$VIMRUNTIME/defaults.vim"
               $VIM par défaut : "/usr/share/vim"
Compilation : gcc -c -I. -Iproto -DHAVE_CONFIG_H -O2 -flto=auto -ffat-lto-objects -fexceptions -g -grecord-gcc-switches -pipe -Wall -Wno-complain-wrong-lang -Werror=format-security -Wp,-D_GLIBCXX_ASSERTIONS -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -fstack-protector-strong -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -march=x86-64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -mtls-dialect=gnu2 -fno-omit-frame-pointer -mno-omit-leaf-frame-pointer -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -DSYS_VIMRC_FILE=/etc/vimrc -D_REENTRANT -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1 
Édition de liens : gcc -Wl,--enable-new-dtags -Wl,-z,relro -Wl,--as-needed -Wl,-z,pack-relative-relocs -Wl,-z,now -Wl,--build-id=sha1 -Wl,-z,relro -Wl,--as-needed -Wl,-z,pack-relative-relocs -Wl,-z,now -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -Wl,--build-id=sha1 -specs=/usr/lib/rpm/redhat/redhat-package-notes -L/usr/local/lib -o vim -lm -lselinux -lncurses -lsodium -lacl -lattr -lgpm

2.) One unique thing about vim is a charityware. If you simply type in vim, the menu will ask you to help children in Uganda through ICCF Holland.

vim menu telling users to support children in Uganda

3.) Vimconf is held in Japan. This indicates that vim either has a strong presence in Japan or a very dedicated fanbase.

4.) Ubuntu 24.04 ships vim.tiny, likely a more stripped down version of vim

5.) vi packaged on a QNX virtual target is called elvis, an enhanced clone of vi. QNX probably ships elvis as the default editor due to its small size compared to vim (though this also means less features compared to vim). The QNX Raspberry Pi 4 image though ships with regular vim. Similarly to vim, renaming elvis to view will open the editor in read only mode.

# vi --version
elvis 2.2.0
Copyright (c) 1995-2003 by Steve Kirkendall
Permission is granted to redistribute the source or binaries under the terms of
of the Perl `Clarified Artistic License', as described in the doc/license.html
file.  In particular, unmodified versions can be freely redistributed.
Elvis is offered with no warranty, to the extent permitted by law.

v.s.

   system vimrc file: "$VIM/vimrc"
     user vimrc file: "$HOME/.vimrc"
 2nd user vimrc file: "~/.vim/vimrc"
 3rd user vimrc file: "~/.config/vim/vimrc"
      user exrc file: "$HOME/.exrc"
       defaults file: "$VIMRUNTIME/defaults.vim"
  fall-back for $VIM: "/builds/workspace/build/stage/target/qnx/usr/share/vim
"
Compilation: aarch64-unknown-nto-qnx8.0.0-gcc -mlittle-endian -mlittle-endian -c -I. -Iproto -DHAVE_CONFIG_H -mlittle-endian -I/builds/workspace/build/stage/target/qnx/usr/include -I/builds/workspace/build/qnx_sdp/target/qnx/usr/include -mlittle-endian -O2 -Wall -fplugin=/builds/workspace/build/qnx_sdp/host/linux/x86_64/usr/lib/gcc/aarch64-unknown-nto-qnx8.0.0/12.2.0/plugin/cmdline_save.so -fplugin=srcversion -fplugin-arg-srcversion-path=/builds/workspace/build/qnx_sdp/target/qnx -fplugin-arg-srcversion-path=/builds/workspace/build/code -fplugin-arg-srcversion-path=/builds/workspace/build/stage/target/qnx -fplugin-arg-srcversion-path=/builds/workspace/build/qnx_sdp/host/linux/x86_64 -fplugin-arg-srcversion-buildid=vim_br-main_be-800-16 -g -D_REENTRANT -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1 
Linking: aarch64-unknown-nto-qnx8.0.0-gcc -mlittle-endian -mlittle-endian -mlittle-endian -L/builds/workspace/build/stage/target/qnx/aarch64le/lib -L/builds/workspace/build/stage/target/qnx/aarch64le/usr/lib -L/builds/workspace/build/qnx_sdp/target/qnx/aarch64le/lib -L/builds/workspace/build/qnx_sdp/target/qnx/aarch64le/usr/lib -Wl,-Map,install.map -Wl,--build-id=md5 -Wl,--as-needed -o vim -lm -lsocket -lncurses -liconv -lintl 

Twitter, Facebook