Last updated: Apr 10, 2024
Reading time·3 min

By default, when writing code in Jupyter Notebook, text and code are not wrapped into multiple lines.

Notice that a horizontal scrollbar is shown instead of wrapping the text.
Instead of having to scroll every time your line exceeds the screen's width, you could wrap the text as shown in the following screenshot.

To wrap code/text so it appears on multiple lines in Jupyter Notebook:
jupyter --config-dir command to print where your configuration
directory is located.jupyter --config-dir

In my case, the configuration directory is ~/.jupyter which is equivalent
to /home/<user>/.jupyter.
Open your config directory and create a nbconfig folder in it (assuming it
doesn't already exist).
In my case, the nbconfig folder is located under ~/.jupyter/nbconfig
which is equivalent to /home/<user>/.jupyter/nbconfig.
Create a notebook.json file in your nbconfig directory.

notebook.json file.{ "MarkdownCell": { "cm_config": { "lineWrapping": true } }, "CodeCell": { "cm_config": { "lineWrapping": true } } }

Make sure the code in your notebook.json file is valid JSON.
All properties should be double-quoted and you shouldn't have any trailing commas.
Stop your Jupyter Notebook server and rerun the jupyter-notebook command.
After you restart the kernel, code/text will wrap into multiple lines.

If you use Jupyter Notebook in Visual Studio Code, you can also enable the "Word Wrap" setting directly in the IDE.
Ctrl + Shift + P (or Command + Shift + P on macOS).F1 to open the Command Palette.
You can also open the settings screen by pressing Ctrl + , on Windows and
Linux or Cmd + , on macOS.
Type word wrap in the search field.
Set the Editor: Word Wrap setting to on.

I've also written an article on How to add a new line in a Jupyter Notebook markdown cell.
You can learn more about the related topics by checking out the following tutorials: