Skip to main content
Version: Next

RsvimCmd

The Rsvim.cmd global object for Ex commands.

Example

// Create a alias to 'Rsvim.cmd'.
const cmd = Rsvim.cmd;

Methods

create()

create(
name,
callback,
attributes?,
options?): CommandDefinition;

Create a ex command with a callback function.

warning

The builtin command js cannot be override.

Parameters

ParameterTypeDescription

name

string

Command name that is going to create. Only letters (a-z and A-Z), digits (0-9), underscore (_) and exclamation (!) are allowed in a command name. Command name must not begin with a digit.

callback

CommandCallback

The backend logic that implements the command. It accepts an ctx parameter that contains all the information when user is running it. See RsvimCmd.CommandCallback.

attributes?

CommandAttributes

Attributes that control the command behavior. This parameter can be omitted, it will use the default attributes, see RsvimCmd.CommandAttributes.

options?

CommandOptions

Options that control how the command is created. This parameter can be omitted, it will use the default options, see RsvimCmd.CommandOptions.

Returns

CommandDefinition

It returns undefined is the command is newly created. Or it returns a command definition that was defined previously.

Throws

Throws TypeError if any parameters are invalid. Or throws Error if command name or alias already exists, but force option is not set to override existing command forcibly.

Example

function write(ctx: any): void {
try {
const bytes = Rsvim.buf.writeSync(bufId);
Rsvim.cmd.echo(`Buffer ${bufId} has been saved, ${bytes} bytes written`);
} catch (e) {
Rsvim.cmd.echo(`Error: failed to save buffer ${bufId}, exception: ${e}`);
}
}
Rsvim.cmd.create("write", write);

echo()

echo(message): void;

Echo message to the command-line.

Parameters

ParameterTypeDescription

message

any

It accepts string and other primitive types, except null and undefined.

Returns

void

Throws

Throws TypeError if the parameter is null or undefined or no parameter provided.

Example

Rsvim.cmd.echo("Hello Rsvim!");

list()

list(): string[];

List all registered ex command names.

warning

The builtin js command will not be listed here.

Returns

string[]

Returns all registered ex command names, except the js command.

Example

Rsvim.cmd.list().forEach((name) => {
Rsvim.cmd.echo(`Command: ${name}`);
});

get()

get(name): CommandDefinition;

Get ex command definition by name.

warning

The builtin js command cannot be get.

Parameters

ParameterType

name

string

Returns

CommandDefinition

Returns command definition by its name, except the js command.

Example

const def = Rsvim.cmd.get("write");
Rsvim.cmd.echo(`Command: ${def.name}`);

remove()

remove(name): CommandDefinition;

Remove an ex command by name.

warning

The builtin command js cannot be removed.

Parameters

ParameterTypeDescription

name

string

The command name to be removed.

Returns

CommandDefinition

Returns the removed RsvimCmd.CommandDefinition, or undefined if no command is been removed.

Throws

Throws TypeError if name is not a string.

Example

Rsvim.cmd.list().forEach((cmd) => {
// Remove all registered commands.
Rsvim.cmd.remove(cmd.name);
});