Skip to content

Instantly share code, notes, and snippets.

@alandipert
Created April 16, 2019 04:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alandipert/5ff8fc2d4ceac50549ce1c5fa3461ca8 to your computer and use it in GitHub Desktop.
Save alandipert/5ff8fc2d4ceac50549ce1c5fa3461ca8 to your computer and use it in GitHub Desktop.
---
title: "Editing with vim keys in RStudio"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# Intro
* Vim is a particular implementation (Windows, Mac, Linux)
* "Vim keys" paradigm widely implemented as plugins/extensions in other editors
* RStudio features Vim keys
* Promise: more efficient movement, editing
# Emacs vs Vim, or, Chorded vs Modal
* Two broad categories of historical approaches to editor commands
* "Chorded": commands are triggered by multiple keys at the same time
* Cmd-C and Cmd-V are "chords" on macOS to copy and paste
* Ctrl-A and Ctrl-E to move cursor to beginning, end of line
* In Emacs [and macOS](https://jblevins.org/log/kbd)
* TextEdit, nano, VS Code, Sublime, Atom, etc
* "Modal": commands are divided into "modes"
* Modes are global editor states
* Editor is in one mode at a time
* Special keys and commands transition between modes
* Same key sequences have different interpretation depending on mode
* Categories are loose: modal editor might have chords in a mode
* Chorded editors can have modes
# A Few Modes
* Insert mode
* Typed keys insert themselves
* Usually indicated visually by pipe cursor
* Arrow keys can be used for movement
* Enter from normal mode with `i`
* Normal mode
* Key sequences interpreted as commands
* Commands may change, delete, navigate
* Enter from insert mode with `Esc`
* Command mode
* Named actions that may accept arguments such as regexes, filenames
* Search/replace, finding/saving files, looking up help
* :help
* Enter from normal mode with `:`
* Visual mode
* The mode of visual selection
* Enter from normal mode
* Movement keys like normal mode
* [Registers: named clipboards](https://www.brianstorti.com/vim-registers/)
* Visual block mode
* Analagous to RStudio Option-click blocks/"multiple cursors"
# Normal mode
`~w~`
capitalize me
`Shift-V ~`
DOWNCASE THIS WHOLE LINE
`Ctrl-A` and `Ctrl-X`
Increment, decrement: 133
`2dw`
Delete two words
`ct,`
Change until the first comma, ok?
`ddp`
Swap two lines
* Line one
* Line two
# Text Objects
* Keys for applying commands relative to delimiters like `(`, `}`
* a form of [structural editing](https://en.wikipedia.org/wiki/Structure_editor)
Delete contents of vector from within:
`di(`
```{r, eval=FALSE}
c(1,2,3)
```
Change the contents of a string:
`ci"`
```{r,eval=FALSE}
old_adage <- "if you find yourself in a hole, stop digging"
```
Replace a block with its contents
`di{ va{ p gv <`
* `d`elete `i`nside block `{`
* `v`isually select `a` block `{`
* `p`ut default register contents into selection
* `gv`: start visual mode with same area as previous area
* `<` shift left
```{r,eval=FALSE}
{
bar()
flub()
}
```
Prepend asterisk to the following lines:
`Ctrl-V jj Shift-i * Esc`
One
Two
Three
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment