.. _panes-editor: ###### Editor ###### Spyder's multi-language **Editor** pane is the core element of the IDE, where you can create, open, and modify source files. The Editor offers a variety of core features, such as autocompletion, real-time analysis, syntax highlighting, horizontal and vertical splitting, and much more. In addition, it integrates a number of powerful tools for an easy to use, efficient programming experience. .. image:: /images/editor/editor-standard.webp :alt: Spyder's Editor pane, split horizontally and with style analysis .. _panes-editor-componets: ============== Key components ============== The Editor pane consists of the following areas: .. image:: /images/editor/editor-components.webp :alt: Spyder's Editor pane, showing its different areas (described below) 1. The left sidebar shows line numbers and displays any code analysis warnings that exist in the current file. Clicking a line number selects the text on that line, and clicking to the right of it sets a :ref:`breakpoint `. 2. The scrollbars allow vertical and horizontal navigation in a file. 3. The context (right-click) menu displays actions relevant to whatever was clicked. 4. The options ("hamburger") menu at the top right includes useful settings and actions for the Editor. 5. The location bar at the top of the pane shows the full path of the current file. 6. The tab bar displays the names of all opened files. It also has a :guilabel:`Browse tabs` button (at left) to show every open tab and switch between them—which comes in handy if many are open. .. _panes-editor-interface: ========= Interface ========= .. _panes-editor-interface-tabs: Tabs ~~~~ You can browse and navigate between open files in the Editor with tabs. Click the :guilabel:`Browse tabs` button on the left of the tab bar to display a list of open files, with the active tab checked. .. image:: /images/editor/editor-tabs-browser.webp :alt: Spyder's Editor pane, showing the tabs browser Reorder tabs by dragging and dropping, or with :guilabel:`Sort tabs alphabetically` in the options menu, which also allows closing all tabs to the left, right, or all but the active one. .. video:: /images/editor/editor-tabs-sorting.webm :loop: :alt: Spyder's Editor pane, showing how to browse and sort open tabs alphabetically .. _panes-editor-interface-switcher: File switcher ~~~~~~~~~~~~~ The Editor features a file switcher, accessed from the :menuselection:`File --> File Switcher` menu or via the :kbd:`Ctrl-P` shortcut (:kbd:`Cmd-P` on macOS). This allows browsing a list of files open in the Editor and present in your current project, if one is opened; click any file to jump to it. .. image:: /images/editor/editor-file-switcher.webp :alt: Spyder's Editor pane, showing the file switcher You can type any part of an open or project file's name and the list will be filtered accordingly; pressing :kbd:`Enter` will give focus to or open the currently-selected file, respectively. .. video:: /images/editor/editor-file-switcher.webm :loop: :alt: Spyder's Editor pane, showing searching and switching to an existing file .. note:: Including all files from the current project in the search is supported when Spyder is installed via our recommended standalone installers or Conda, but not Pip. .. _panes-editor-interface-split: Split panels ~~~~~~~~~~~~ The Editor can be split horizontally and vertically into as many distinct panels as desired. This allows viewing and editing the contents of several files (or different parts of the same file) at the same time. .. video:: /images/editor/editor-split-panels.webm :loop: :alt: Spyder's Editor pane, showing how to split panels Split the Editor with the :guilabel:`Split vertically` (:kbd:`Ctrl-Shift-{`) and :guilabel:`Split horizontally` (:kbd:`Ctrl-Shift--`) commands in the options menu, and use :guilabel:`Close this panel` (:kbd:`Alt-Shift-W`) to close the selected split panel. .. note:: :menuselection:`Close this panel` closes a split panel, while :menuselection:`Close` hides the entire Editor *pane* (including all splits, which are restored when the Editor is re-opened). .. _panes-editor-features: ================ Editing features ================ .. _panes-editor-features-highlighting: Syntax highlighting ~~~~~~~~~~~~~~~~~~~ To improve the readability of your code, Spyder has a syntax highlighting feature that determines the color and style of text in the Editor, as well as in the :ref:`panes-console`. You can configure and preview syntax highlighting themes and fonts under :menuselection:`Preferences --> Appearance`. The :guilabel:`Syntax highlighting theme` section allows you to change the color and style of the syntax elements and background to match your preferences. You can switch between available themes in the drop-down menu, modify the selected theme, create a new theme, and more. The :guilabel:`Fonts` section lets you change the interface and code font and size. .. video:: /images/editor/editor-syntax-highlighting.webm :loop: :alt: Spyder's Editor pane, showing how to switch between syntax highlighting themes .. note:: If :guilabel:`Automatic` is selected in the :guilabel:`Main interface` section, changing the syntax highlighting theme from dark to light will automatically change Spyder's overall interface theme to match. .. _panes-editor-features-cells: Code cells ~~~~~~~~~~ A "code cell" in Spyder is a block of lines, typically in a script, that can be executed all at once in the current :ref:`panes-console`. This is similar to "cell" behavior in Jupyter Notebook and MATLAB. You can divide your scripts into as many cells as needed, or none at all—the choice is yours. .. image:: /images/editor/editor-cells.webp :alt: Spyder's Editor pane, showing an example of a code cell You can separate cells by lines starting with either: * ``# %%`` (standard cell separator), or * ``# `` (IPython notebook cell separator) Providing a description to the right of the separator will give that cell its own name in the :ref:`panes-outline`. You can also create "subsections" by adding more ``%`` signs to the cell separator, e.g. ``# %%%`` to create a level 2 subsection, ``# %%%%`` for level 3, etc. This displays as multiple hierarchical levels in the :ref:`panes-outline` pane. .. image:: /images/editor/editor-subsections.webp :alt: Spyder outline pane, showing an example of sub sections .. note:: This only affects how the cell is displayed in the :ref:`panes-outline`, and doesn't affect running it in the Editor. To run the code in a cell, use :menuselection:`Run --> Run cell`, the :guilabel:`Run cell` button in the toolbar or the keyboard shortcut (:kbd:`Ctrl-Enter`/:kbd:`Cmd-Return` by default). For more about the different ways you can run code in cells, see :ref:`panes-editor-run-cell`. .. _panes-editor-features-formatting: Automatic formatting ~~~~~~~~~~~~~~~~~~~~ The Editor has built-in support for automatically formatting your code using several popular tools, including `Autopep8 `_, `Black `_ and `Ruff `__. The :guilabel:`Format file or selection with {tool}` command in the :guilabel:`Source` or context menu will format either the selected fragment (if text is selected) or the entire active file. .. video:: /images/editor/editor-automatic-formatting.webm :loop: :alt: Spyder Editor pane, showing an example of code selection formatting You can have the Editor automatically format your files every time you save them. To set this up, go to :menuselection:`Preferences --> Completion and linting --> Code formatting` and check the :guilabel:`Autoformat files on save` option. .. image:: /images/editor/editor-autoformat-setting.webp :alt: Spyder's preferences dialog, showing checking the autoformat files on save setting .. _panes-editor-run: ============ Running code ============ The Editor lets you run an entire file as well as specific lines, selections or cells. As your code is running, * The :ref:`panes-console` will display output and errors. * The :ref:`panes-variables` allows you to browse and interact with the objects generated. * The :ref:`panes-plots` pane renders the figures and images created. .. _panes-editor-run-file: Run file ~~~~~~~~ Run an entire Editor file using the :menuselection:`Run --> Run` menu item, the :guilabel:`Run file` toolbar button or the :kbd:`F5` key. Use :menuselection:`Run --> Re-Run last file` to re-run the most recent script executed with the above. .. _panes-editor-run-cell: Run cell ~~~~~~~~ To run a cell, place your cursor inside it and use the :menuselection:`Run --> Run cell` menu item, its corresponding toolbar button or the :kbd:`Ctrl-Enter` / :kbd:`Cmd-Return` keyboard shortcut. Use :guilabel:`Run cell and advance` in the :guilabel:`Run` menu/toolbar or :kbd:`Shift-Enter` to jump to the next cell after running, useful for stepping through cells quickly. .. _panes-editor-run-line: Run line/selection ~~~~~~~~~~~~~~~~~~ You can execute the current line—or multiple selected lines—using the :guilabel:`Run current line/selection` option from the toolbar or the :menuselection:`Run` menu, as well as with the :kbd:`F9` key (by default). After running the current line, the cursor automatically advances to the next one, so you can step through your code line by line. Unlike :guilabel:`Run file` or :guilabel:`Run cell` (by default), the executed lines are shown in the :ref:`panes-console`. .. _panes-editor-run-configuration: Run configuration ~~~~~~~~~~~~~~~~~ You can use the :guilabel:`Run configuration` dialog to set each file's working directory, console mode (current, dedicated or external), command line arguments, execution options (clear all variables, run in an existing/empty namespace, debug on error), and more. .. image:: /images/editor/editor-run-configuration.webp :alt: Spyder's Editor pane, showing the Run Configuration dialog To access it, click :menuselection:`Run --> Configuration per file` or press :kbd:`Ctrl-F6` / :kbd:`Cmd-F6`. The :guilabel:`Runner` drop-down allows to configure settings for the different runners capable of executing your files, including the :ref:`panes-debugger`, :ref:`panes-profiler`, :ref:`panes-pylint`, as well as running in an external system terminal. To configure settings for running in the default :ref:`panes-console`, select :guilabel:`IPython Console`. The relevant options for each runner can be set under :guilabel:`Custom configuration`, and you can save them as presets by giving them a :guilabel:`Name` and clicking :guilabel:`OK`. You can then switch between different presets from the :guilabel:`Preset configuration` dropdown below the :guilabel:`Runner` selection. .. _panes-editor-navigation: =============== Code navigation =============== .. _panes-editor-navigation-find: Find and replace ~~~~~~~~~~~~~~~~ To search for text in the current file, use :menuselection:`Search --> Find text` or :kbd:`Ctrl-F` / :kbd:`Cmd-F`, and to replace it, use :menuselection:`Search --> Replace text` or :kbd:`Ctrl-R` / :kbd:`Cmd-R`. Typing your search string in the resulting panel below the Editor highlights each result and counts the total. Navigate between matches with the :guilabel:`Find previous` and :guilabel:`Find next` buttons in the find/replace panel (up and down arrows), their corresponding entries in the :guilabel:`Search` menu, or use the :kbd:`F2` and :kbd:`F3` keys. Use the :guilabel:`.*` button to process search text as a `regular expression `_, :guilabel:`Aa` to treat it as case-sensitive and :guilabel:`[A]` to only match whole words (e.g. for ``data``, match ``data()`` but not ``dataframe``). For replacing, there are also options to replace the next occurrence or all in the current selection or file. .. image:: /images/editor/editor-find-replace-panel.webp :alt: Spyder's Editor pane, showing the find and replace panel .. _panes-editor-navigation-goto: Go to line ~~~~~~~~~~ The :guilabel:`Go to line` dialog allows jumping to a specific line in the active file. Open it with :menuselection:`Search --> Go to line` or :kbd:`Ctrl-L` / :kbd:`Cmd-L`, type the line number you want to scroll to and press :kbd:`Enter` (or click :guilabel:`OK`). .. video:: /images/editor/editor-go-to-line.webm :loop: :alt: Spyder's Editor pane, showing the go to line feature It also shows the current line number and total line count in the file. .. _panes-editor-navigation-selector: Class/function selector ~~~~~~~~~~~~~~~~~~~~~~~ This panel, activated under :menuselection:`Source --> Show class/function selector`, displays (as applicable) the name of the cell, function/method and class the Editor cursor is located inside. Use its dropdowns to view and jump to the functions, methods and classes in the current file. .. video:: /images/editor/editor-class-function-selector.webm :loop: :alt: Spyder's Editor pane, showing the class and function selector panel .. _panes-editor-lsp: ============================= Code analysis and completions ============================= Spyder uses the `Language Server Protocol `_ (LSP) to provide code completion and linting for the Editor, similar to VSCode, Neovim, and other popular editors/IDEs. .. note:: Many issues with completion and linting are outside of Spyder's control, and are either limitations with the LSP or the code that is being introspected, but some can be worked around. See :ref:`troubleshooting-common-completion` for troubleshooting steps. Python is supported out of the box, and advanced users can add completion and linting support for other languages by setting up LSP servers for them under :menuselection:`Preferences --> Completion and Linting --> Other languages`. .. _panes-editor-lsp-completion: Code completion ~~~~~~~~~~~~~~~ Automatic code completion as you type is enabled by default in the Editor, and can also be triggered manually with :kbd:`Ctrl-Space`/:kbd:`Cmd-Space`, showing you possible completions (with pop-up help for each) and available code snippets. For example, typing ``cla`` will display the keyword ``class``, the decorator ``classmethod`` and two built-in snippets with class templates. Select the desired completion with the arrow keys and :kbd:`Enter`, or by double clicking. .. image:: /images/editor/editor-code-completion.webp :alt: Spyder's Editor pane, showing a code completion example You can enable or disable on-the-fly code completion, as well as modify when it is triggered and what results are shown, under :menuselection:`Preferences --> Completion and Linting --> General --> Completions`. Spyder also allows you to define custom completion snippets to use, in addition to the ones offered by the LSP, under :menuselection:`Preferences --> Completion and Linting --> Snippets`. .. _panes-editor-lsp-linting: Linting and code style ~~~~~~~~~~~~~~~~~~~~~~ Spyder can optionally highlight syntax errors, style issues, and other potential problems with your code in the Editor, which can help you spot bugs quickly and make your code easier to read and understand. .. image:: /images/editor/editor-pane-code-error.webp :alt: Spyder's Editor pane, showing an example of a highlighted code error The Editor's real-time linting warns of syntax errors and likely bugs in your code. It is on by default, and can be disabled or customized under :menuselection:`Preferences --> Completion and Linting --> Linting`. The linting providers available include: * `Pyflakes `_ (default), which detects basic errors. * `Flake8 `_, which incorporates error checks, code style linting and complexity analysis. * `Ruff `_, which includes a huge variety of configurable lint rules covering a wide gamut of possible issues in your code. .. image:: /images/editor/editor-linting-setting.webp :alt: Spyder's preferences dialog, showing linting settings Flake8 and Ruff enable a default set of checks that aims to strike a balances between catching errors and potential problems, and issuing too many warnings, with Spyder additionally disabling some style-related checks by default. You can override these defaults or enable or disable any specific categories or individual checks using the :guilabel:`Show/Ignore these errors or warnings` fields and the rule prefix/numbers listed in their respective documentation (they use the same set of codes for the same checks, aside from a few cases where two third-party Flake8 checks have conflicting codes). You can also ignore or only check specific files by name or regular expression using the :guilabel:`Only check these filenames` and :guilabel:`Exclude these files or directories` options. .. image:: /images/editor/editor-code-style-setting.webp :alt: Spyder's preferences dialog, showing code style and formatting settings .. _panes-editor-lsp-introspection: Introspection features ~~~~~~~~~~~~~~~~~~~~~~ If there's a function, class or variable for which you would like to check its definition, hold :kbd:`Ctrl`/:kbd:`Cmd` and click its name in the Editor to jump to the file and line where it is declared. .. video:: /images/editor/editor-go-to-definition.webm :loop: :alt: Spyder's Editor pane, showing the go to definition feature You can hover over the name of an object for pop-up help, as :ref:`described in the Help pane docs `. .. image:: /images/editor/editor-hoverhint.webp :alt: Spyder's Editor pane, showing an example of a hover hint Finally, if you type the name of a function, method or class constructor and then an open parenthesis, a calltip will pop up which shows the function's parameters as you type them, as well as a summary of its documentation. These features can be enabled and customized under :menuselection:`Preferences --> Completion and Linting --> Introspection`. .. _panes-editor-shortcuts: ================== Keyboard shortcuts ================== To view the Editor's primary keyboard shortcuts, consult its section under :menuselection:`Help --> Shortcuts Summary`. The full list can be browsed, searched and customized (on double-click) in :menuselection:`Preferences --> Keyboard shortcuts`. .. _panes-editor-related: ============= Related panes ============= * :ref:`panes-pylint` * :ref:`panes-files` * :ref:`panes-find` * :ref:`panes-console` * :ref:`panes-projects`