1. Introduction

When working with files, knowing their absolute path may sometimes be critical.

In this tutorial, we look at ways to show the full path of the current file in the Vi editor. First, we briefly discuss absolute and relative paths, as well as their benefits and drawbacks. Next, we explore basic ways to temporarily show the current file’s full path within the Vi editor. Finally, we configure the status and title options of Vi to display the information we want permanently.

For brevity, we use vi (Vi) when referencing both the Vi and Vim editors.

We tested the code in this tutorial on Debian 11 (Bullseye) with GNU Bash 5.1.4. It should work in most POSIX-compliant environments.

2. Absolute and Relative Paths

In Linux, files are just links to inodes of the filesystem. However, there are pseudo-filesystems that don’t actually use the concept.

Regardless, every file has a path. An absolute or full path represents the exact place a file (link) resides:

$ realpath file

Here, we used the realpath command to get the full path of a file. For this purpose, we were in the file’s directory first so that we could reference the file directly by name alone. Importantly, absolute paths start with a forward slash.

On the other hand, from the / root directory, the relative path to the same file is ./dir/file. Let’s explore why we’d need a full path instead.

3. Why Use Absolute Paths

There are many reasons to choose absolute over relative paths:

  • uniquely identify a file (link)
  • the directory structure can serve as meta information
  • may hint at the mount point

Of course, pointing at a specific file is preferable in most settings. Otherwise, we risk issues when dealing with identical filenames. Naturally, relative paths are more convenient when porting. In these cases, we don’t need to know any exact locations.

However, the full path can show a file’s category, purpose, or even ownership (e.g., /home/user1/documents/). This may be very helpful when editing and appending information.

Finally, mount points can serve as a way of knowing which filesystem holds the contents of our file:

$ df
Filesystem     1K-blocks     Used Available Use% Mounted on
udev              937360        0    937360   0% /dev
tmpfs             196216     1072    195144   1% /run
/dev/sda1      101639152 30903964  65526000  33% /
tmpfs             981060        0    981060   0% /dev/shm
tmpfs               5120        4      5116   1% /run/lock
/dev/sdb1      102625208       24  97365972   1% /mnt/sec
tmpfs             196212       60    196152   1% /run/user/1000
tmpfs             196212       64    196148   1% /run/user/133

Here, we use df to show the mount points, which we can then cross-reference with full but not necessarily absolute paths. This may be important not only due to storage constraints but also because of potential limitations each filesystem type may pose.

4. Absolute Paths in Vi

The ubiquitous vi editor is part of both the SUS and POSIX standards.

Thus, it can handle most kinds of paths. Still, by default, Vi only displays filenames or, at most, relative paths. However, we can configure or simply use the Vi editor to show full paths as well. Let’s open a file:

$ vi file

In all cases below, we’ll assume vi was run from /dir/ with the command above.

4.1. The % Register

One of the registers in Vi is called % percent and holds the name of the current file (or its contents, depending on the context). In shell expressions, % has the file path relative to the current working directory of Vi.

In fact, we can see what any register holds via the echo command by prefixing the register with @. For example, :echo @% displays the contents of %, in our case: file.

Since we’re interested in more than the name of the file, we can use a filename modifier to show the full path:

:echo expand('%:p')

Here, the expand function built into Vi evaluates %:p, where :p expands the contents of % to a full path. Basically, the command returns /dir/file. Moreover, when using links, we might need to resolve them via realpath in the shell:

:!realpath %

Press ENTER or type command to continue

Further, we can achieve a similar result with :echo resolve(expand(‘%:p’)), which would not require calling a shell command.

Moreover, appending :h (%:p:h), which is like dirname in bash, we can even get only the head of the path: /dir.

Of course, while we can assign shortcuts to all of them, entering commands is more cumbersome than using hotkeys.

4.2. Ctrl+G

One way to see the path of an edited file is the Ctrl+G key combination, which is equivalent to the :f[ile] command:

"file" [Modified] 666 lines --100%--

We can see the same line for each buffer via :ls:

  1 %a   "file"                         line 1
Press ENTER or type command to continue

On its own, Ctrl+G is the same as 0 Ctrl+G, which only provides the path relative to the current working directory of Vi. However, if the prefix number is 1, we get the full path to the current file at the bottom of the screen:

"/dir/file" [Modified] 666 lines --100%--

If we use a number above 1 as in 2 Ctrl+G, Vi provides the buffer name as well:

buf 1: "/dir/file" [Modified] 666 lines --100%--

Still, all of the above requires an explicit key combination. We might want to have the full file path visible at all times.

5. Vi Status and Title

Indeed, the Vi editor supports status and title lines. The former is at the bottom, while the latter is at the top of the editor.

By default, the status line is not visible unless there are at least two windows. To change that, we can set the laststatus special variable:

:set laststatus=2

Setting laststatus=2 (default is 1), we force Vi to show the status line at all times. This means we get the currently edited file’s name on the second to last line at the bottom of the editor.

If we want to see other information in the status line, we can modify the statusline special variable according to our needs. For example, to add the full path of the current file, we can append (+=) it with %F:

:set statusline+=%F

Importantly, status lines may be unique to each window.

Similarly, we can use :set title to allow the use of titlestring as the current window’s title contents:

:set titlestring=%F

Here, we set the title to the full path of the current file. Of course, titles are not always available since they require terminal window title support.

By adding each of the commands above to our ~/.vimrc or /etc/vim/vimrc files, we can configure the settings permanently:

set laststatus=2
set statusline+=%F
set title
set titlestring=%F

After including this configuration and opening our /dir/file alone, we can see the changes in effect:

Vi Title and Status

Of course, we can replace %F with other fields as well as built-in or default Vi functions.

6. Summary

In this article, we discussed relative and absolute file paths, concentrating on ways to show the full path of the current file in the Vi editor.

In conclusion, Vi provides many options for temporarily and permanently displaying a file’s absolute path.