Contribute to this guide

guideFind and replace

The find and replace feature allows for finding and replacing any text in the editor easily. It helps the user find words, word parts or phrases matching the case of the searched text, which is especially helpful in lengthy documents and one that may utilize certain words in different contexts. It also lets the editor replace a chosen one or all instances of the searched phrase with a single click, making tedious, repeated changes fast and easy. This may e.g. help ensuring the cohesion of an edited piece of code, while renaming a variable or a function.

The find and replace feature is enabled by default in the superbuild only. See the installation section to learn how to enable it in your editor.

# Demo

Use the find and replace toolbar button Find and replace to invoke the search panel and find and replace desired words or phrases or use the Ctrl/Cmd+F keyboard shortcut. For a starter, try replacing “steam” with “diesel” to make the demo content more up to date. Be careful to match the case, for there are different instances of the word present in the document!

This demo only presents a limited set of features. Visit the full-featured editor example to see more in action.

# Installation

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/src/findandreplace';

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.

The commands can be executed 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 in https://github.com/ckeditor/ckeditor5/tree/master/packages/ckeditor5-find-and-replace.