Whether you’re a beginner programmer, casual coder, data analyst/scientist, or anyone else that uses Python, I’d recommend you at least check out Spyder, among other IDEs. Granted, it may not the best choice if you’re a full-time Python developer. But otherwise it has a lot to offer with its blend of intuitive, beginner-friendly design and broad, powerful set of shoulders. I mean features.
If you’re interested, see the video above for a tour of Spyder where I run you through the basics, including a set of simple keyboard shortcuts. And below you’ll find a list of said keyboard shortcuts you can follow along with.
To be clear, these aren’t the optimal shortcuts you’d get with something like Vim, and your hand will regularly have to venture over to the arrow keys and their neighbors.
But you will avoid the mouse for the most part. And importantly, the learning curve is gentle so you’ll be able to focus your cognitive resources on the code. Not to mention that, if you’re a beginner, it’s going to be a while before your mind outpaces your fingers anyways.
So what I want to do here is show you the handful of shortcuts that are:
- useful to beginners, ones that anyone could regularly use
- reasonably intuitive, not too hard to memorize
- Spyder’s default shortcuts, so you don’t need to make any changes
- the same across different operating systems
Spyder Code Editor Shortcuts
First, here are the shortcuts you should know when working in the code editor.
|Left/Right (Arrow Key)||go back/forward a character|
|Up/Down (Arrow Key)||go up/down a line|
|Shift + Left/Right||highlight previous/next character|
|Shift + Up/Down||highlight to cursor’s new position on next line up/down|
|Ctrl + Left/Right||go to start of previous/next word|
|Ctrl + Shift + Left/Right||highlight to start of previous/next word|
|Home/End||go to start/end of line|
|Shift + Home/End||highlight to start/end of line|
|Ctrl + Home/End||go to start/end of file|
|Ctrl + Shift + Home/End||highlight to start/end of file|
|Ctrl + L||go to Line you specify|
|Alt + Up/Down||move current line up/down|
|Highlight lines + Tab||indent selected lines|
|Highlight lines + Shift + Tab||un-indent selected lines|
|Ctrl + 1||toggle whether a line is commented out|
|Ctrl + 4||format line into block comment|
|Ctrl + 5||undo block comment formatting|
|Backspace/Delete||delete previous/next character|
|Ctrl + Backspace/Delete||delete to start of previous/next word|
|Ctrl + D||Delete line|
|Ctrl + Z||undo|
|Ctrl + Shift + Z||redo|
|Ctrl + X||cut|
|Ctrl + C||Copy|
|Ctrl + V||paste|
|Ctrl + A||highlight All|
|Ctrl + F||Find text (Enter and Shift + Enter, or F3 and Shift + F3 to cycle matches)|
|Ctrl + R||Replace text|
|Ctrl + Space||code completion|
|Ctrl + I||Inspect object (see info in help pane)|
|Ctrl + G||Go to file and line where selected object is defined|
|Ctrl + Page Up/Page Down||cycle to previous/next file|
|Ctrl + Shift + E||switch to Editor pane|
|Ctrl + Shift + I||switch to Ipython console pane|
|Ctrl + Shift + V||switch to Variable explorer pane|
|Ctrl + Shift + H||switch to Help pane|
|Ctrl + Shift + X||switch to file eXplorer pane|
|Ctrl + = (or Ctrl + Mouse Wheel Forward)||zoom in|
|Ctrl + - (or Ctrl + Mouse Wheel Back)||zoom out|
|Ctrl + 0||zoom reset|
|Ctrl + N||open New file|
|Ctrl + O||Open existing file|
|Ctrl + S||Save current file|
|Ctrl + Shift + S||Save current file as|
|Ctrl + Alt + S||Save all open files|
|Highlight code + Ctrl + Enter||run selected code|
|Ctrl + W||close file|
|Ctrl + Shift + W||close all files|
|Ctrl + Q||Quit spyder|
Spyder IPython Console Shortcuts
Keep in mind that many of these Code Editor shortcuts also apply to the IPython console. But there are some commands that only apply to the console, the most useful of which are the following:
|Up/Down||go back/forward in command history|
|Ctrl + U||delete text on current line|
|Ctrl + L (or enter command “clear”)||cLear shell|
|Ctrl + T||open new ipython console Tab|
|Ctrl + .||restart kernel|
|Ctrl + C (or click red square in corner of pane)||Cancel action, stop running script, KeyboardInterrupt|
Spyder IPython Console Magic Commands
There are also special “magic” commands that you can use in the IPython console, which can take the effectiveness of Spyder to a whole new level. Try them out, and try either the
%magic commands to learn more about how to use the rest of them. If you use the
%magic command though, check out the last section of this article to make sure you’re able to see all of the output.
|%quickref||get list of magic commands|
|%magic||get detailed list of magic commands|
|%time||get execution time of statement based on single run|
|%timeit||get more accurate execution time of statement by averaging many runs|
|%hist||get command history of current session|
|%tb||print the last traceback|
|%reset||delete all defined variables in current namespace|
|%pwd||get present working directory|
|%cd||change present working directory|
|%run||run file as Python program inside IPython session and make all file variables accessible|
|%load||import script into code cell|
|%conda||run conda package manager|
|%pip||run pip package manager|
Spyder IPython Console Tab Completion
When in the middle of typing something in the IPython console, such as the name of a variable, function, module, or any other object, try pressing Tab. This will do one of 3 things. 1st, if there are multiple objects whose names could match what you’ve typed so far, you will see a list options for you to choose from. You can then use the arrow keys or mouse to highlight the desired name, and press Tab to complete the name. 2nd, if there is only one possible option, the name will automatically be completed. And 3rd, if what you’ve typed so far doesn’t match anything in the namespace, then nothing will happen.
Note that, in addition to saving keystrokes on object names you’re working with, this is also helpful in exploring what methods an object contains (try hitting Tab after typing the period after the object’s name) or what objects a module contains (similar idea).
Note that normally it won’t display any names starting with underscores. To show these objects, simply include the initial underscore in what you’re typing before hitting Tab.
Tab completion also works with file paths, and function arguments too, among other things. It’s a very powerful feature. Try it out!
Spyder IPython Console Introspection (?)
Type and object’s name and then add a question mark (?) to the end of it before hitting Enter. This will return helpful information on that object. Try the same thing again, but add two question marks (??) instead. This will return the same helpful information along with the source code if it’s available.
Additionally, you can combine wildcards (*) with question marks (?) to search the IPython namespace. For example, say you’ve import pandas with
import pandas as pd. And say you want to know about all the functions pandas contains related to reading files. Entering the command
pd.*read*? will return every object in the top-level pandas that has contains “read” somewhere in its name. Pretty cool huh!
Increasing Spyder IPython Console Line Limit
By default, Spyder’s IPython Console will only display the last 500 lines printed to the console. When you have a lot of output, that means a lot of what you might like to see is getting cut off. Therefore, you’ll likely want to increase this line limit like so: go to Tools -> Preferences -> IPython console and on the Display tab scroll down to the Source code section where it says something like “Buffer: 500 lines” and gives you the option of changing the number. I’d recommend trying 10,000 lines and see how you like it.
So what do you think? Are you a fan of Spyder yet? Or would something like the Forbidden IDE be more your style? Let me know if you have any questions.