2

I have vim-athena package (the same is observed for vim-gtk) in my Debian machine.

  • Running vim spawns Vim in the terminal.
  • Running gvim spawns a new window with Vim in a Athena GUI.

But what is vim and gvim? Symlinks pointing to a same target.

$ readlink -e "$(type -p gvim)"
/usr/bin/vim.athena
$ readlink -e "$(type -p vim)"
/usr/bin/vim.athena

And that final target is a binary.

$ file --brief /usr/bin/vim.athena
ELF 64-bit LSB shared object, ...

How can it be that vim and gvim do different things if both resolve to the same binary?

Background:
I was changing my default editor via update-alternatives --config editor. In the list, there are vim.tiny and vim.athena, but even if I select the latter, Vim always opens in the terminal, not in a separate GUI.

Quasímodo
  • 18,865
  • 4
  • 36
  • 73

1 Answers1

7

The program can take different routes by looking at the zeroth-argument argv[0] (in the present case, vim or gvim).

Vim does it in main.c:

    params.argv = argv;
    ...
/* Figure out the way to work from the command name argv[0].
"vimdiff" starts diff mode, "rvim" sets "restricted", etc. */
parse_command_name(&params);
...

/* Check for: [r][e][g][vi|vim|view][diff][ex[im]] (sort of) / static void parse_command_name(mparm_T parmp) { char_u initstr; initstr = gettail((char_u )parmp->argv[0]); ...

/* "gvim" starts the GUI.  Also accept "Gvim" for MS-Windows. */
if (TOLOWER_ASC(initstr[0]) == 'g') {
    main_start_gui();

In this fragment, the program checks if the first char of the basename of argv[0] is "g"; if yes, it starts the GUI.

This can be observed by creating links:

ln -s /usr/bin/vim.athena vimX
ln -s /usr/bin/vim.athena gvimX
ln -s /usr/bin/vim.athena evimX
ln -s /usr/bin/vim.athena rvimX
ln -s /usr/bin/vim.athena exX

where X is whatever, and then ./vimX runs Vim in the terminal, ./gvimX in the GUI, ./evimX runs Vim in easy mode, etc.

Quasímodo
  • 18,865
  • 4
  • 36
  • 73
  • 1
    You can see similar things with other tools. Bash, for example, behaves differently (activates POSIX mode) if you call it from a symlnk named sh instead of bash. – terdon Feb 12 '21 at 11:50
  • 1
    @terdon, the machinery behind it is the same. Heck, you could even write shell scripts that behave differently depending on the name by which they are called. – vonbrand Feb 12 '21 at 16:30
  • 1
    @vonbrand yes, of course. I just felt that since sh/bash is a very well known example, it might be worth pointing out. – terdon Feb 12 '21 at 17:05