When your code blocks contain long lines, it can be helpful to enable word wrap to keep the code within the bounds of the container and avoid the need for horizontal scrolling.
Usage in markdown / MDX
Configuring word wrap per block
You can enable or disable word wrap on individual code blocks using the wrap boolean prop in the meta information of your code blocks. This is done by appending wrap or wrap=true to your opening code fence to enable word wrapping, or wrap=false to disable it:
The above code will be rendered like this:
Configuring indentation of wrapped lines
By default, wrapped parts of long lines will be aligned with their line’s indentation level, making the wrapped code appear to start at the same column. This increases readability of the wrapped code and can be especially useful for languages where indentation is significant, e.g. Python.
You can also disable this behavior so that wrapped parts of long lines will always start at column 1. This can be useful to reproduce terminal output.
To change this behavior for a code block, you can use the preserveIndent boolean prop in the meta information of your code blocks:
The above code will be rendered like this:
Usage in the <Code> component
The following props are available on the <Code> component to configure word wrap behavior:
Props
wrap
Type:booleanDefault:false
If true, word wrapping will be enabled for the code block, causing lines that exceed the available width to wrap to the next line. You can use the preserveIndent option to control how wrapped lines are indented.
If false, lines that exceed the available width will cause a horizontal scrollbar to appear.
preserveIndent
Type:booleanDefault:true
If true, wrapped parts of long lines will be aligned with their line’s indentation level, making the wrapped code appear to start at the same column. This increases readability of the wrapped code and can be especially useful for languages where indentation is significant, e.g. Python.
If false, wrapped parts of long lines will always start at column 1. This can be useful to reproduce terminal output.
Configuration
You can configure the default word wrap settings using the defaultProps option in your Expressive Code configuration. You can also change the defaults by language using the sub-property overridesByLang: