python-3.xregexreplacejupyter-notebookfind-replace

How to insert a newline character in a jupyter notebook cell when using find and replace?


So I need to make changes in multiple cells in a Jupyter notebook. One of these changes is inserting a newlines in a code line.

fig = fig.update_xaxes(showgrid=True, gridwidth=0.3, gridcolor='LightBlue') fig.show()

This line of code is present in multiple cells in the notebook. I need to insert a newline before fig.show() for the code to work.

I guess I need to use "find and replace" functionality. But I am unable to insert a new line. I have tried using the regex Find replace with regex

But the output is \nfig.show().

How can I insert a newline character in multiple cells in Jupyter Notebook?


Solution

  • Don't know how to add a newline, but two possible workaraounds for your case:

    1. Replace fig.show() with (fig := fig.update_xaxes(showgrid=True, gridwidth=0.3, gridcolor='LightBlue')).show(), i.e. chaining the two commands using the walrus operator so that fig is also updated

    2. Put a ; between both commands: Replace fig.show() with fig = fig.update_xaxes(showgrid=True, gridwidth=0.3, gridcolor='LightBlue');fig.show()