[HowTo] Set Environment Variables

Ever seen mention of sudoedit? Ever wanted to use pacdiff with something other than vim -d ?

We can use Environment Variables

First lets understand how they work, the ways we can configure them, and what the differences are.

The ArchWiki goes into detail here:
https://wiki.archlinux.org/index.php/Environment_variables#Defining_variables

Variables can be defined for each command we run. In the sudoedit example that means we can manually tell it to use our choice of editor by doing something like this:
SUDO_EDITOR=nano sudoedit /path/to/file

But what about setting a default for when we just type sudoedit?

We can use certain locations to define variables globally for the whole system or per-user.
But each location works a little differently, and you may have reason to use one over the other. Refer to the wiki article to make an informed decision, but due to limitations of other files I will be using /etc/environment here. This will help ensure it applies to the most users in the most cases.

/etc/environment sets these variables globally and accepts “KEY=VAL” entries.
One on each line, with full paths being preferred.

So, in the example of sudoedit it would look like this:
SUDO_EDITOR=/usr/bin/nano
There are also a few other related variables. And many more for a whole range of options.

I will leave here an example of my /etc/environment configuration:

EDITOR=/usr/bin/micro
SUDO_EDITOR=/usr/bin/micro
VISUAL=/usr/bin/kate
DIFFPROG=/usr/bin/meld
GDK_SCALE=1.25

These variables are my defaults now, and will be used anywhere applicable.
As examples, whenever I want to use sudo -e or sudoedit then micro will automatically be used, pacdiff will use meld by default, and I’m setting the GTK items on my KDE desktop to scale 1.25x .




Note - This is quickly copied from the old forum. Formatting/updates pending

6 Likes