# Commands

Commands are statements that control aspects of the q environment. A command begins with a back-slash (\), followed by one or more characters. Some commands have an optional parameter which is separated from the commmand by whitespace.

Case is signficant in the command character.

To execute a command programmatically, place it in a string and use the value function.

	value "\\p 5042"

A back-slash and any other special characters in the string must be escaped.

## Directory (\d)

The \d command controls the current working context (directory).

To determine the current working context, issue \d with no parameter.

	\d
.


To set the current working context, issue \d followed by the target context.

	\d .tutorial
\d
.tutorial

If the specified context does not exist, using it in \d will cause its creation.

Issue \d . to set the current working context to the default context.

	\d .
\d
.


## Change O/S Directory (\cd path)

The \cd command affects the current working directory of the underlying operating system. To display the current directory, issue \cd with no argument.

	\cd
"c:\\"

The result of \cd is the text string as receives from the O/S with escapes where applicable. For Windows, the back-slash characters are escaped and are not converted to forward-slashes.

To change the current working directory, issue \cd with the path of the desired directory. If the directory does not exist, it will be created.

	\cd \q

Since the argument of \cd is not a string, special characters are not escaped.

## Variables (\v)

The \v command returns a sorted list containing the variables in the current context (directory).

	\v
s#Lhktppirsqrt2ttdetailsthird


## Functions (\f)

The \f command returns a sorted list containing the functions in the current context (directory).

	\f
s#difffg


A script can be loaded at startup of the q session or during a session. To load the script from the session, issue the \l command with the (optionally qualified) named of the script file.

For example, to load the distribution script sp.q from the q install directory,

	\l sp.q
+pcity!(p$p1p2p3p4p5p6p1p2;londonlondonlondonlondonlondonlon.. (+(,color)!,bluegreenred)!+(,qty)!,900 1000 1200 +spqty!(s$s1s1s1s2s3s4;p\$p1p4p6p2p2p4;300 200 100 400 200 300)


## Port (\p)

The \p command controls which port the kdb+ server listens on. For example,

	\p 5001


means that it will listen for connections on port 5001.

When you issue the \p commend, kdb+ attempts to open the port. In order for this to be successful, the security settings of the machine must be configured to allow it.

If the port has not been set, you will see.

	\p
0


This means that no connection to this instance of kdb+ is currently possible because it is not listening on any port.

## Timer (\t)

The \t command controls the timer. The optional parameter is the number of milliseconds between timer ticks, with 0 signifying that the timer is off. On each timer tick, the function .z.ts is invoked if it has been assigned.

To determine the current timer setting, issue \t with no parameter.

	\t
0


To set the timer, issue \t with the number of milliseconds. For example, to set the timer to tick once a second,

	\t 1000

The actual timer tick frequency is determined by the timer granularity supported by the underling operating system. This can be considerably less than a millisecond.

To turn the timer off,

	\t 0


## Time (\t expr)

When the \t command is invoked with an expression as its parameter, the expression is evaluated and its time of execution is reported. This can be used to profile code execution when tuning an application.

Often in q, there are multiple ways to achieve a desired result, but one may execute significantly faster. This may not matter for small tables or sporadic updates, but for processing very large volumes of data in real time it can be essential. Inserting \t at key points in the program can identify the critical routines that are consuming the most time. By measuring the execution times of alternate expressions for the critical routines, you can determine which is most efficient in your environment.

The following measures the time required to add the first 100,000 integers 10,000 times on the author's laptop.

	\t do[10000;sum til 100000]
5207


We conclude that adding the first 100,000 integers requires approximately 500 milliseconds.

If it is actually necessary to add the first 100,000 integers in an application, you should use the formula,

sn = (n*n-1)%2

We time it for n = 100,000:

	\t do[10000;(100000*99999)%2]
40


As you can see, this is ~ 130 times faster than performing the actual addition. We can do much better by replacing the division with a multiplication,

	\t do[10000;.5*100000*99999]
20


## Workspace (\w)

The workspace command \w displays four integer values that indicate memory usage by the current workspace.

	\w
82848 67634176 0 0j


The first value indicates the number of bytes currently used. The second indicates the total number of bytes allocated to the kdb+ process. The second indicates the maximum TBA used. The forth indicates the size of mapped file space.

## Operating System (\textoscom)

If a backslash is not followed by characters representing a recognized kdb+ command, the text is assumed to be an operating system command and is passed to the O/S for execution.

For example, you can issue,

	\dir                                / display Windows directory
\pwd                                / display Unix directory


Any return value from the O/S is displayed inside parentheses. If there is no return value, empty parentheses are displayed.

## Interrupt (Ctrl-C)

You can terminate a long-running routine by pressing the Ctrl-C combination.

## Terminate (\)

The Terminate command, denoted by a single backslash (\), exits one level of the q interpreter. This is useful when debugging a failed function evaluation.

	f:{x*y}
f[2;3]
k){x*y}
'type
*
2
3
)y
3
)\
_


Here the underscore denotes the blinking cursor.

If you issue \ at the "q)" prompt, you drop down to a k session.
q)\
_


Here the underscore denotes the blinking cursor. Because k is the underlying language in which q is implemented, some expressions will continue to execute as expected in the k session, but many will not. Explanation of k is beyond the scope of this manual.

To return to the q console from a k session and turn the "q)" priompt back on, enter a single Exit again.

	\
q)


## Exit q (\\)

To exit the q process, enter a double backslash,

	\\

You do not receive a confirmation prompt when you enter \\. The q session is immediately terminated.

## Precision (\P)

The Precision command \P (note the upper case) sets the display precision for floating point numbers to the specified number of digits.

The default precision is 7, meaning that the display of float or real values is rounded to the seventh significant digit.

	\P
7
f:1.234567890123456
f
1.234568


Set the precision with an int parameter,

	\P 12
f
1.23456789012


Set the precision to the maximum available with 0.

	\P 0
f
1.234567890123456


Prev: Workspace Organization, Next: Built-in Functions