# How to find the CodeMirror instance?

**URL:** <https://discourse.jupyter.org/t/how-to-find-the-codemirror-instance/9275>\
**Category:** JupyterLab\
**Created:** [May 20, 2021, 5:55pm UTC](https://discourse.jupyter.org/t/how-to-find-the-codemirror-instance/9275 "2021-05-20T17:55:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![adpatter](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/adpatter/32/21229_2.png) [@adpatter](https://discourse.jupyter.org/u/adpatter)\
**Post date:** [May 20, 2021, 5:55pm UTC](https://discourse.jupyter.org/t/how-to-find-the-codemirror-instance/9275/1 "2021-05-20T17:55:05Z")

</div>

The documentation indicates that there is a CodeMirror instance in the CodeEditorWrapper instance: [Notebook — JupyterLab 3.0.16 documentation](https://jupyterlab.readthedocs.io/en/stable/extension/notebook.html?highlight=codemirror#widget-hierarchy)

I found an `editor` there. I thought it might be an instance like the one described here: [CodeMirror: User Manual](https://codemirror.net/doc/manual.html), but it appears that it’s not.

Does anyone know how can get a reference to the CodeMirror instance?

Edit:

See also: [jupyter lab - How can I get a reference to a CodeMirror instance in JupyterLab? - Stack Overflow](https://stackoverflow.com/questions/67626233/how-can-i-get-a-reference-to-a-codemirror-instance-in-jupyterlab)

---

<div class="post-metadata">

**Author:** ![adpatter](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/adpatter/32/21229_2.png) [@adpatter](https://discourse.jupyter.org/u/adpatter)\
**Post date:** [May 20, 2021, 6:00pm UTC](https://discourse.jupyter.org/t/how-to-find-the-codemirror-instance/9275/2 "2021-05-20T18:00:57Z")

</div>

Specifically, I am trying do what they are doing here: [Get visible lines (with respect to current scroll) - discuss.CodeMirror](https://discuss.codemirror.net/t/get-visible-lines-with-respect-to-current-scroll/1429)

---

<div class="post-metadata">

**Author:** ![bollwyvl](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/bollwyvl/32/904_2.png) [@bollwyvl](https://discourse.jupyter.org/u/bollwyvl)\
**Post date:** [May 21, 2021, 2:51am UTC](https://discourse.jupyter.org/t/how-to-find-the-codemirror-instance/9275/3 "2021-05-21T02:51:19Z")

</div>

CodeMirror is kind of an odd duck. You can pull a reference to the global `Codemirror` object from [`ICodeMirror.CodeMirror`](https://github.com/jupyterlab/jupyterlab/blob/4a51c4000d9e2f6ac30eeb4358479c9f025f7152/packages/codemirror/src/tokens.ts#L22).

As for an instance of a specific editor bound to a widget… yeah, it’s in there somewhere. There are a few layers of abstraction, but somewhere you’ll find an IEditor that has a `.editor`, and _that_ is indeed _a_ CodeMirror instance. A number of functions are also available from _it’s_ `.document`.

However: it’s a little vague _where_ you are trying to find this _from_, whether with a handle to a Lab `Editor` instance, or individual cells in a `NotebookPanel`, etc.

Also: re that approach: watch yourself on `getBoundingClientRect`… it’s _very_ expensive, especially with large notebooks on the screen!

---

<div class="post-metadata">

**Author:** ![adpatter](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/adpatter/32/21229_2.png) [@adpatter](https://discourse.jupyter.org/u/adpatter)\
**Post date:** [May 21, 2021, 11:59am UTC](https://discourse.jupyter.org/t/how-to-find-the-codemirror-instance/9275/4 "2021-05-21T11:59:07Z")

</div>

Thank you for the guidance on this. After some casting, I was able to find it: [jupyter lab - How can I get a reference to a CodeMirror instance in JupyterLab? - Stack Overflow](https://stackoverflow.com/questions/67626233/how-can-i-get-a-reference-to-a-codemirror-instance-in-jupyterlab)
