Contribute to this guide

Automatic text transformation (autocorrect)

The text transformation feature enables autocorrection. It automatically changes predefined text fragments into their improved forms.

# Demo

Type snippets such as (c), 3/4, !=, ---, "foo" into the editor below and see how they get transformed into their typographically nicer forms. You can see the complete list of predefined transformations in the TextTransformationConfig documentation.

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

# Additional feature information

Here are some examples of snippets changed by the text transformation feature:

From To
(tm)
1/2 ½
->
--
"foo" “foo”

This feature comes pre-configured with a set of the most popular transformations. You can, however, remove existing ones or add your own autocorrect entries.

While most often this feature is used to insert special characters that are not present on your keyboard, you can also use it to achieve other goals. For instance, you can improve the users’ productivity by configuring it to expand some abbreviations (like team or company names) into their full forms.

You may find interesting details and usage examples in the Automatic text transformation in CKEditor 5 blog post after reading this guide.

# Installation

⚠️ New import paths

Starting with version 42.0.0, we changed the format of import paths. This guide uses the new, shorter format. Refer to the Packages in the legacy setup guide if you use an older version of CKEditor 5.

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

import { ClassicEditor, TextTransformation } from 'ckeditor5';

ClassicEditor
	.create( document.querySelector( '#editor' ), {
		licenseKey: '<YOUR_LICENSE_KEY>', // Or 'GPL'.
		plugins: [ TextTransformation, /* ... */ ],
		typing: {
			// Configuration.
		}
	} )
	.then( /* ... */ )
	.catch( /* ... */ );

# Configuring transformations

This feature comes pre-configured with a set of transformations. You can find the list of them in the TextTransformationConfig documentation.

By using the options defined below you can extend, limit, or override this list:

# Example: Using transformations.include

For instance, to use the transformations from the “quotes” and “typography” groups and to turn CKE into CKEditor, you can use the transformations.include property like this:

ClassicEditor
    .create( editorElement, {
        // ... Other configuration options ...
        typing: {
            transformations: {
                include: [
                    // Use only the 'quotes' and 'typography' groups.
                    'quotes',
                    'typography',

                    // Plus some custom transformation.
                    { from: 'CKE', to: 'CKEditor' }
                ],
            }
        }
    } )
    .then( /* ... */ )
    .catch( /* ... */ );

# Example: Using transformations.remove and extra

Another example, removing some transformations and adding some extra ones:

ClassicEditor
    .create( editorElement, {
        // ... Other configuration options ...
        typing: {
            transformations: {
                remove: [
                    // Do not use the transformations from the
                    // 'symbols' and 'quotes' groups.
                    'symbols',
                    'quotes',

                    // As well as the following transformations.
                    'arrowLeft',
                    'arrowRight'
                ],

                extra: [
                    // Add some custom transformations, for example, for emojis.
                    { from: ':)', to: '🙂' },
                    { from: ':+1:', to: '👍' },
                    { from: ':tada:', to: '🎉' },

                    // You can also define patterns using regular expressions.
                    // Note: The pattern must end with `$` and all its fragments must be wrapped
                    // with capturing groups.
                    // The following rule replaces ` "foo"` with ` «foo»`.
                    {
                        from: /(^|\s)(")([^"]*)(")$/,
                        to: [ null, '«', null, '»' ]
                    },

                    // Finally, you can define `to` as a callback.
                    // This (naive) rule will auto-capitalize the first word after a period, question mark, or an exclamation mark.
                    {
                        from: /([.?!] )([a-z])$/,
                        to: matches => [ null, matches[ 1 ].toUpperCase() ]
                    }
                ],
            }
        }
    } )
    .then( /* ... */ )
    .catch( /* ... */ );

You can read more about the format of transformation rules in TextTransformationDescription.

You can test these custom rules in the demo. Try typing :) or :+1: and see how the text gets transformed into emojis. You can also write some sentences to test how the editor capitalizes words after a period, a quotation mark, or an exclamation mark.

In addition to enabling automatic text transformations, you may want to check the following productivity features:

  • Autoformatting – Quickly apply formatting to the content you are writing.
  • Autolink – Turns the links and email addresses typed or pasted into the editor into active URLs.
  • Slash commands – Execute a predefined command by writing its name or alias directly in the editor.
  • Mentions – Brings support for smart autocompletion.

# Contribute

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