Contribute to this guide

guideFind and replace

The find and replace feature lets you find and replace any text in your document. This speeds up your work and helps with the consistency of your content.

# Demo

Use the find and replace toolbar button Find and replace to open the search dialog. Use it to find and replace words or phrases. You can also use the Ctrl/Cmd+F keyboard shortcut. Try replacing “AI” with “artificial intelligence” to make the content appeal to less tech-savvy users. Be careful to match the case!

This demo presents a limited set of features. Visit the feature-rich editor example to see more in action.

# Configuration

# Configuring the UI type

By default, the find and replace form displays inside a dialog. That allows for keeping it open while editing the document at the same time. Alternatively, you can display the feature in a dropdown. To do this, use the config.findAndReplace.uiType configuration option:

ClassicEditor
    .create( document.querySelector( '#editor' ), {
        findAndReplace: {
            uiType: 'dropdown'
        }
    } )
    .then( /* ... */ )
    .catch( /* ... */ );

# Installation

The find and replace feature is enabled by default in the superbuild only.

To add this feature to your editor, install the @ckeditor/ckeditor5-find-and-replace package:

npm install --save @ckeditor/ckeditor5-find-and-replace

Then add the FindAndReplace plugin to your plugin list:

import { FindAndReplace } from '@ckeditor/ckeditor5-find-and-replace';

ClassicEditor
    .create( document.querySelector( '#editor' ), {
        plugins: [ FindAndReplace, /* ... */ ],
        toolbar: [ 'findAndReplace', /* ... */ ],
    } )
    .then( /* ... */ )
    .catch( /* ... */ );

Read more about installing plugins.

# Common API

The FindAndReplace plugin registers the 'findAndReplace' UI button component and the 'find', 'findNext', 'findPrevious', 'replace' and 'replaceAll' commands.

You can execute the commands using the editor.execute() method:

// Find all occurrences of a given text.
editor.execute( 'find', 'steam' );

You can also move the highlight through all matched results with the 'findNext' and 'findPrevious' commands:

// Move the search highlight to the next match.
editor.execute( 'findNext' );

You can also replace all occurrences of a given text in the editor instance using the 'replaceAll' command:

editor.execute( 'replaceAll', 'diesel', 'steam' );

We recommend using the official CKEditor 5 inspector for development and debugging. It will give you tons of useful information about the state of the editor such as internal data structures, selection, commands, and many more.

# Contribute

The source code of the feature is available on GitHub at https://github.com/ckeditor/ckeditor5/tree/master/packages/ckeditor5-find-and-replace.