Sign up (with export icon)

Slash commands

Show the table of contents

The slash commands feature lets you execute a predefined command by writing its name or alias directly in the editor. When you type a slash (/), the suggested commands are displayed in a panel next to it. You can also type a phrase after the / to filter the results in the panel and to easily find the desired command.

Unlock this feature with selected CKEditor Plans

Try all premium features – no credit card needed.

Sign up for a free trial Select a Plan

Demo

Copy link

Type the / character to invoke a panel with predefined commands. Then, filter the result list by typing more characters after the slash, for example: /list. Slash commands can also apply templates or styles defined in the editor.

Slash commands in CKEditor 5

Integrations: Templates & Styles

Slash commands integrate with template and style features out of the box.

  • Check which styles and templates are available.
  • Type the slash (/) character in the editor.
  • Find the chosen style or template in the panel.

Installation

Copy link
Note

The slash commands feature uses the mentions feature under the hood, so ensure you install it too.

After installing the editor, add the feature to your plugin list and toolbar configuration:

import { ClassicEditor, Mention } from 'ckeditor5';
import { SlashCommand } from 'ckeditor5-premium-features';

ClassicEditor
	.create( document.querySelector( '#editor' ), {
		licenseKey: '<YOUR_LICENSE_KEY>',
		plugins: [ SlashCommand, Mention, /* ... */ ],
		slashCommand: {
			// Configuration.
		}
	} )
	.then( /* ... */ )
	.catch( /* ... */ );
Copy code

Activating the feature

Copy link

To use this premium feature, you need to activate it with proper credentials. Refer to the License key and activation guide for details.

Configuration

Copy link
Note

For more technical details, check the plugin configuration reference.

Although the slash commands feature is based on the mention feature, it does not require any additional configuration to work. In particular, the / marker is configured automatically as well as the list of default slash commands that will work right away (as long as they are enabled in the editor).

However, if you want to modify this list, it is possible to easily add or remove commands through configuration. It is also possible to limit the number of commands displayed in the panel.

Adding slash commands

Copy link

It is possible to add both commands registered in the editor and commands that execute custom logic. The parameter you should use for custom commands is slashCommand.extraCommands.

Editor commands

Copy link

To add an editor command as a slash command, besides the id and the title you have to provide the commandName parameter. The example below shows how to add a bold slash command:

ClassicEditor
    .create( document.querySelector( '#editor' ), {
        // ... Other configuration options ...
        slashCommand: {
            extraCommands: [
                {
                    id: 'bold',
                    title: 'Bold',
                    commandName: 'bold',
                    // ...
                },
                // ...
            ]
        }
    } )
    .then( /* ... */ )
    .catch( /* ... */ );
Copy code

Proxy commands

Copy link

To add a proxy command (a command that executes custom logic), besides the id and the title you have to provide a callback function for the execute parameter. If you do not provide an execute parameter for a proxy command, the editor will throw an error when the user tries to execute it.

ClassicEditor
    .create( document.querySelector( '#editor' ), {
        // ... Other configuration options ...
        slashCommand: {
            extraCommands: [
                {
                    id: 'alert',
                    title: 'Alert',
                    execute: editor => { console.log( 'Custom logic was executed.' ) }
                    // ...
                },
                // More extra commands.
            ]
        }
    } )
    .then( /* ... */ )
    .catch( /* ... */ );
Copy code

Removing slash commands

Copy link

To remove a command from the default list of slash commands, add its name to the array of removed commands (slashCommand.removeCommands). The command will only be removed from the list of default slash commands, and not from the editor.

ClassicEditor
    .create( document.querySelector( '#editor' ), {
        // ... Other configuration options ...
        slashCommand: {
            removeCommands: [ 'heading', 'paragraph', /* ... */ ]
        }
    } )
    .then( /* ... */ )
    .catch( /* ... */ );
Copy code

Limiting displayed command list

Copy link

By default, all available slash commands are displayed in the panel. If you want to limit the number of commands displayed to the user, use the slashCommand.dropdownLimit parameter. It will determine the number of displayed items.

ClassicEditor
    .create( document.querySelector( '#editor' ), {
        // ... Other configuration options ...
        slashCommand: {
            dropdownLimit: 4
        }
    } )
    .then( /* ... */ )
    .catch( /* ... */ );
Copy code

Single slash command configuration

Copy link
Note

For more technical details, check the slash command configuration reference.

There are some more optional parameters of a single slash command entry you might find useful when configuring your own commands:

  • aliases – To extend command filtering in the panel.
  • description – Helpful if a title is not descriptive enough.
  • icon – To visually customize your own command.
  • isEnabled – To provide custom logic and decide when the command is displayed in the panel.

Here is an example of how you can use them:

ClassicEditor
    .create( document.querySelector( '#editor' ), {
        // ... Other configuration options ...
        slashCommand: {
            extraCommands: [
                {
                    id: 'bold',
                    title: 'Bold',
                    execute: editor => { /* Custom logic. */ },
                    aliases: [ 'strong' ],
                    description: 'Style the text in bold.',
                    icon: customIcon,
                    isEnabled: editor => { /* Custom logic. */ }
                },
                // ...
            ]
        }
    } )
    .then( /* ... */ )
    .catch( /* ... */ );
Copy code

Sorting and filtering

Copy link

By default, slash commands are sorted alphabetically by title. The order changes when the user starts filtering. Slash commands matched by id are displayed first, then sorted by title, then by aliases, and finally by description.

Integration with other features

Copy link

Slash commands are integrated with the template and style features. It means that all templates and styles can be applied also using the corresponding slash command.

Copy link

You may want to check the following similar productivity features of CKEditor 5: