How to use CMake variables

CMake uses many variables that are officially documented. These have default values or values that are based on your project environment. You are allowed to change the value of these variables in your CMakeLists.txt. In addition, you can also define any variables you need. In fact, defining your own custom variables makes your CMakeLists.txt file easy to read and maintain.

To list all the variables that are defined and their current values, try this in a directory that has a CMakeLists.txt file:

$ cmake -LAH

A list of common variables used internally by CMake can be seen here. You can change the value of these variables in your CMakeLists.txt to modify the behavior of CMake for your project. See below for how to set variables.

To set a variable to a single value in your CMakeLists.txt file:

set(JoeLibraries -lfoo)

A variable can be set a list of values:

set(JoeLibraries -lfoo -lbar -lstarwars)

It can also be indented in different ways for ease of reading:


To use a variable in another command:


A variable can also be used to in setting the value of another variable:


Strings can be appended naturally to variables:

set(CurDir /home/joe/helloworld)
set(BinDir ${CurDir}/bin)

To check if a variable exists:

    message("CurDir variable exists")

To check if a variable does not exist:

if(NOT CurDir)
    message("CurDir variable does not exist")
endif(NOT CurDir)

To check if variable value is equal to a string:

    message("Build is Debug type")

Note that the comparison is case-sensitive.

If you want case-insensitive compare, you will need to convert the variable value to lowercase, store it in a new variable and compare that:

    message("Build is debug type")

Tried with: CMake 2.8.7 and Ubuntu 12.04 LTS

How to view environment variables in Vim

List of all environment variables in Vim.

When Vim is started, it grabs the environment variables of the operating system (like PATH) and it also sets it own internal environment variables (like MYVIMRC).

To see all the environment variables available inside Vim, type :echo $ and press Ctrl-D

To see the value that is currently assigned to a particular environment variable, for example $VIM, type :echo $VIM

For more details go here.

Tried with: Vim 7.3.46 on Windows 7 64-bit

Windows Environment Variables in Vim

All the Windows environment variables are read by Vim and they are available in Vim as Vim environment variables. Vim environment variables begin with the character $. Hence, the Windows environment variable name is prefixed with the $ character when it becomes a Vim environment variable.

For example, the Windows environment variable %USERPROFILE% will be available as $USERPROFILE inside Vim.

This post shows how you can list all the environment variables or view the value of a particular variable.

How to view variables in Vim

To view the list of all variables and their values in Vim:


Note that this does not list environment variables, this post shows how to view those.

If you find it hard to wade through the long list, you can redirect the output of this command to a buffer for further analysis. For example, to load the output of :let into a buffer:

:redir @a
:redir END

There are many types of variables in Vim and each has a special name or prefix:

g:	global variables
b:	local buffer variables
w:	local window variables
t:	local tab page variables
s:	script-local variables
l:	local function variables
v:	Vim variables.

To list the variables of a particular type, for example global variables, just use its prefix:

:let g:

Tried with: Vim 7.3 and Ubuntu 12.04 LTS