missing_class

Text Editors and vim

Table of Contents

Based on https://missing.csail.mit.edu/2020/editors/

Notation

Control-V can be expressed as:

Philosophy

Modes

Buffers, tabs, windows

Command-line

Command mode can be entered by typing : in normal mode

Movement

Selection

Visual modes:

Can use movement keys to make selection.

Edits

deletion example

Many commands that change text are made of an operator (noun) and a motion (verb): e.g. when used with deletion operator: d

Counts

You can combine nouns and verbs with a count, which will perform a given action a number of times.

Modifiers

Demo

Here is a broken fizz buzz implementation:

def fizz_buzz(limit):
    for i in range(limit):
        if i % 3 == 0:
            print('fizz')
        if i % 5 == 0:
            print('fizz')
        if i % 3 and i % 5:
            print(i)

def main():
    fizz_buzz(10)

We will fix the following issues:

Customizing Vim

Extending Vim with plugins

Vim-mode in other programs

Many tools support Vim emulation. The quality varies from good to great; depending on the tool, it may not support the fancier Vim features, but most cover the basics pretty well.

Shell

For vim keybindings:

Set default editor: export EDITOR=vim

Readline

Many programs use the GNU Readline library for their command-line interface. Readline supports (basic) Vim emulation too, which can be enabled by adding the following line to the ~/.inputrc file:

set editing-mode vi

With this setting, for example, the Python REPL will support Vim bindings.

Others

Advanced Vim

A good heuristic: whenever you’re using your editor and you think “there must be a better way of doing this”, there probably is: look it up online.

Search and replace

:s (substitute) command (documentation).

Execution

Type :! followed by command to execute external command

Multiple windows

Macros

Integration with PyCharm

  1. Install IdeaVim extension
  2. Add .ideavimrc file in your home directory:
    • Windows: C:\Users\James
    • Linux: ~/
  3. Activate plugins

Commands

Resources

Exercises

  1. Complete vimtutor. Note: it looks best in a 80x24 (80 columns by 24 lines) terminal window.
  2. Download our basic vimrc and save it to ~/.vimrc. Read through the well-commented file (using Vim!), and observe how Vim looks and behaves slightly differently with the new config.
  3. Install and configure a plugin: ctrlp.vim.
    1. Create the plugins directory with mkdir -p ~/.vim/pack/vendor/start
    2. Download the plugin: cd ~/.vim/pack/vendor/start; git clone https://github.com/ctrlpvim/ctrlp.vim
    3. Read the documentation for the plugin. Try using CtrlP to locate a file by navigating to a project directory, opening Vim, and using the Vim command-line to start :CtrlP.
    4. Customize CtrlP by adding configuration to your ~/.vimrc to open CtrlP by pressing Ctrl-P.
  4. To practice using Vim, re-do the Demo from lecture on your own machine.
  5. Use Vim for all your text editing for the next month. Whenever something seems inefficient, or when you think “there must be a better way”, try Googling it, there probably is. If you get stuck, come to office hours or send us an email.
  6. Configure your other tools to use Vim bindings (see instructions above).
  7. Further customize your ~/.vimrc and install more plugins.
  8. (Advanced) Convert XML to JSON (example file) using Vim macros. Try to do this on your own, but you can look at the macros section above if you get stuck.

Edit this page.