11  Vim

11.1 Exit and Save

  • :x exits and saves
  • :w saves without exit
  • :w filename saves file as filename
  • :q! exits without save

11.2 Run commands

  • :!CMD where CMD is command. e.g.., !ls

11.4 Inserting

  • i Insert text before the cursor

  • a Insert text after cursor

  • A Append text to end of current line. - I Insert text at beginning of line.

  • o Open blank line below cursor for text. - O Open blank line above cursor for text.

  • p puts the Vim register (from delete) after cursor (if full line, on next line).

  • rx replaces the character at the cursor with ‘x’

  • R replaces multiple characters. Type ESC when done.

  • ce deletes until end of word and places you in insert. Press ESC when done

  • c$ deletes until end of line and places you in insert. Press ESC when done

  • :%s/old/new/gc - replaces old with new in full doc with prompt for replace.

  • :r filename to insert text in filename above cursor

11.5 Deleting

  • x deletes the character under the cursor
  • dw deletes from cursor to start of next word (including space spearator). d2w deletes from cursor to end of second word, etc.
  • de delets from curors to end of current for
  • dd deletes the line. 2dd deletes two lines.
  • d$ deletes from cursor to end of line.
  • u to undo the last command. CTRL-R for redo -U to undo all fixes on a line.

11.6 Spell check

  • :set spell – Turn on spell checking
  • :set nospell – Turn off spell checking
  • ]s – Jump to the next misspelled word
  • [s – Jump to the previous misspelled word
  • z= – Bring up the suggested replacements
  • zg – Good word: Add the word under the cursor to the dictionary
  • zw – Woops! Undo and remove the word from the dictionary

11.7 Search in VIM

  • /text to search forward for text. Type ENTER to exit to that text.
  • ?text to search backward for text.
  • n to find next instance of text (after ENTER)
  • N to find previous instance of text (after ENTER)

For search and replace

  • `%s///options
  • options include g for replace all, c for confirmation and i for case insensitive

11.8 Visual selection

  • v to select text. Move cursor around with hjkl keys
  • then d to delete (can later p(ut) it somewhere if needed)
  • or y to yank (copy) so you can later p(ut) it somewhere
  • or w to write to separate file

11.9 Copy

  • y to yank (copy) selction (use with v for visual selection)
  • yw to yank a word
  • yy to yank a line

11.10 Working with multiple files

Vim can open multiple files in separate buffers. This can be done at the start by providing multiple filenames to vim (e.g., vim file1.md file2.md)

Alternatively, additional files can be opened for editting later (:e file2.txt).

Moving between buffers

  • bn moves to next buffer
  • bp moves to previous buffer
  • :buffers displays all open buffers
  • : buffer 1 allows switch to buffer 1 (or any other number) if there are many buffers open
  • :bd deletes the current buffer. Will fail unless buffer has first been saved (or no changes made)

yank/delete/put will work between buffers

11.11 Command line in VIM

Switch to command line from normal mode by typing :

You can then use:

  • pwd to print the current working director
  • cd to change the working directory for the vim shell
  • lcd to change the working directory for the specific buffer you are in

11.12 Render files

We tend to use either md or qmd files for writing, code, etc. Our .vimrc file turns on markdown highlighting for these files if saved with .md or ..qmd extensions. qmd is preferred because it works with RStudio as well.

These files can be rendered to html or pdf if desired.

  • quarto render file.qmd to render to html
  • quarto render file.qmd --to pdf to render to pdf

We can use a full yaml header for more fancy output. We have templates for apa manuscripts, letterhead, and a generally nice format pdf output if desired. Just paste the yaml at the top of the file.

11.13 Installing plugins

If the plugin is on github, simply clone it using the github url into the vim plugin folder: /home/jjcurtin/.vim/pack/vendor/start

And you are done!

11.14 Learing VIM

  • run vimtutor

  • books: https://iccf-holland.org/vim_books.html