Vim Overview

Associate Professor Justin Dressel

Schmid College of Science and Technology

 

Vim

Vi "Improved"

  • "vi" = "visual interface" text editor written by Bill Joy in 1977
  • Philosophy: minimize key strokes, maximize productivity
  • Structure: modal editor - commands first, text next

Rudimentary workflow:

  1. Move cursor and manipulate text in command mode
  2. Press 'i' to enter insert mode
  3. Insert text, using arrow keys to move if needed.
  4. Press Esc to exit insert mode. Repeat 1-4 as needed.
  5. Open menu with ':'  Write and quit file with "wq" followed by Enter. Exit without saving as "q!" followed by Enter instead.

Structure of Commands

Vim commands usually have the same basic structure:

(optional action) (optional number of times) (direction)

l            # move right one character
h            # move left one character
j            # move down one line
k            # move up one line

3l           # move right 3 characters
300j         # move down 300 lines

dh           # delete the character to the left
d3k          # delete three lines going up
dd           # delete entire line

yl           # yank (copy) character to the right
y3h          # yank 3 characters to the left
yy           # yank entire line

p            # paste last deleted or yanked text

i            # enter insert mode
<esc>        # escape insert mode to command mode
u        # undo
Ctrl-r   # redo

Don't Panic.  Just undo if you screw up.

w            # move forward one word
b            # move back one word

W            # move forward one WORD (skips past punctuation)
B            # move back one WORD

Ctrl-f       # jump forward one page
Ctrl-b       # jump backward one page

^            # move to first nonblank character in line
$            # move to end of line

gg or 1G     # go to first line
30G          # go to line 30
G            # go to last line

%            # jump to matching bracket

.            # repeat last command

o            # add new line, then enter insert mode
a            # enter insert mode one character to the right

Essentials:

Advanced:

Idea: Keep fingers on keyboard, no mouse, near home row.

Repeating Yourself

Lots of coding involves repetition.

Try typing the following in vim:

30ifoo<enter><esc>

What does it do?

How about the following?

i  This is text.<enter><esc>50.

You can also record key sequences and replay them:

qaThis is a text line.<esc>oThis is a new line.<enter><esc>q@a

qa : start recording to register a

<stuff goes here>

q : end recording

@a : replay recording in register a

Accessing Menu

In command mode, ':' opens the menu.

<esc> exits menu back to command mode.

<enter> executes menu command.

:w      # write file
:q      # quit
:q!     # quit without saving
:w!     # force write
:wq     # write and quit
:qall   # quit all panes
:<tab>  # see all possible menu commands

Probably this is what you will use most:

.vimrc Configuration

Configuration for vim is stored in the file ~/.vimrc

This is a hidden file in your home directory.

I recommend the following config options:

" Enable syntax-highlighting
syntax on

" Use custom colorscheme
colorscheme desert

" Remap window navigation to avoid Ctrl-w
" for use in MS Windows
nnoremap gh <C-w><C-h>
nnoremap gl <C-w><C-l>
nnoremap gj <C-w><C-j>
nnoremap gk <C-w><C-k>

" File-type specific settings
if has("autocmd")

  filetype plugin on

  "Python code : use 4 spaces, no tabs
  augroup python
    autocmd BufReadPre,FileReadPre    *.py set tabstop=4
    autocmd BufReadPre,FileReadPre    *.py set expandtab
  augroup END
endif

Note: " is a comment in .vimrc

Note: These pane navigation remappings are nice, but are essential when using CoCalc in MS Windows

Note: Never use tabs in Python

Split Windows

Important: In MS Windows, Ctrl-w closes the current window, so this slide uses the .vimrc suggestion in the previous slide for remapping split pane navigation.

Split the window horizontally with:

:split optionalfilename

(Without filename, it opens a split view on the same file.)

Split the window vertically with:

:vsplit optionalfilename

(Without filename, it opens a split view on the same file.)

Move between panes with:

gl   # Go to pane right
gh   # Go to pane left
gj   # Go one pane down
gk   # Go one pane up

Each pane acts independently. Close with:

:q

Further Reading

Practice makes perfect.

 

Keep references handy until you remember commands on command.

Vim Overview

By Justin Dressel

Vim Overview

These slides offer a condensed description of the essentials of using vim inside a bash shell.

  • 4,239