Python R SAS Keyboard Shortcuts

Make switching between languages easier | 07 January 2022

Keyboard shortcuts

Keyboard shortcuts are essential because: when we are used to writing in a language, switching to another one can make use feel slow and dumb. Using keyboard shortcuts will allow us to pick up speed and feel at home.

VSCode

Although Spyder and Ipython (notebook and shell) both are great, VSCode is especially useful when we are writing packages or modules. I use VSCode for all three languages. There are suggested add-ons for each of the languages. See keyboard shortcuts pdf for the comprehensive list.
VSCode Keyboard Shortcuts VSCode Keyboard Shortcuts You can also define your own keyboard shortcut.

Ipython shell commands

These aren’t technically shortcuts, but I count them in because they are convinient.

ls cd pwd all work in Ipython shell as they do in command prompt.

%automagic on enables magic commands without having to use prefix “%”.

%history -n 2-5 presents history line 2 to 5.

%run myStuff.py: runs code in this directory.

Functions defined in myStuff.py are alive in the current Ipython session after %run myStuff.py.

And get inspired by others’ notebooks.

RStudio

  • Ctrl+Enter To run a line of code from the source editor.
  • Ctrl+Shift+M is the pipe operator %>% is or Cmd+Shift+M (Mac).
  • Alt + - is the assignment operator <-, or Option + - (Mac).
  • Ctrl+L to clear all the code from your console.
  • Ctrl+2 and Ctrl+1 to move the curser back and forth the source editor.
  • Ctrl + ↑ to scroll through your command history by clicking or Cmd + ↑ (Mac).
  • Search a matching subset of the history: type the first few characters and then press Ctrl/Cmd + ↑
  • Rename all instances of a variable name: highlight one instance of the variable name and then using Code > Rename in Scope. This is better than using Edit > Replace and Find because it only looks for whole word matches.

Add custom snippets

Using custom snippets helps save time. Using 3 languages has a lot of syntax and libraries to remember.
See how to add a snippet for details.

  1. Use snippet generator to easily create the snippet json code.
  2. shift + command + p and type snippets, and then Preferences: Configure User Snippets.
  3. Hit New snippets, then choose language, and follow the directions. Note, while the snippet is in json, you don’t choose json. Choose Python if the snippet is for Python code, choose markdown if it is for markdown.