Skip to content

Instantly share code, notes, and snippets.

@iantruslove
Last active December 16, 2015 01:29
Show Gist options
  • Save iantruslove/5355490 to your computer and use it in GitHub Desktop.
Save iantruslove/5355490 to your computer and use it in GitHub Desktop.
Vim Smackdown! for Den of Clojure

Vim Smackdown

Intro / Ethos

  • Vim vs Emacs: seems like comparing a text editor to the JVM. Apples to oranges?
  • Where Emacs is about providing the user powerful tools, Vim is a powerful text editor focussed on efficiency
  • Emacs has nice tooling for clj dev - can Vim match it?

Keep the mnemonics in mind...

File management

  • :e <filename> - edits a file
  • :w - writes a file to disk
  • :qa - quits vim
  • :qa! - really quits vim!

Editing

When in normal mode:

  • i - switch into insert mode

When in insert mode:

  • <esc> - switch into normal mode

Help

  • :h - start up the help system
  • :h <cmd> - get help on a command (or keystroke etc - try :h CTRL-w_q

Package Management

Noob tip: Check your .vimrc into github

Aside from the built-in method of dumping plugins into a subdirectory somewhere, there are a couple of nice plugin management tools.

  • Pathogen

    • I used to use this
    • Use git submodules - slightly awkward, more typing
  • Vundle - I switched to this to put this talk together

    • My .vimrc on Github:
git clone git://github.com/iantruslove/vimrc_vundle.git .git
cd .vim
git submodule update --init
ln -s .vimrc ../.vimrc
* Launch Vim, and `:BundleInstall`

.vimrc runthrough

  • Declare and configure plugins
  • Disable arrow keys - force learning of hjkl
  • Set up some basic non-default settings
  • Custom functions and behaviors

Vim Basics

Referring to the "you don't grok vi" stackoverflow answer, there's a Vim way. There is a vocabulary to learn, and the commands are largely composable.

File operations

  • :e <filename> - opens the file for editing
  • :w - writes changes for the current buffer to disk
  • :q - closes the current window. If this is the last window, tries to quit Vim. Will warn if closing unsaved files.
  • :q! - same as previous, but will disregard any unsaved changes.
  • :qa - closes all windows, being mindful of unsaved changes.
  • :qa - closes all windows, and screw any unsaved changes, they're gone.

Basic navigation

  • h,j,k,l - move cursor left, down, up, right one character/line
  • 0 - move to start of line
  • $ - move to end of line
  • nG - go to line n
    • gg - go to start of file
    • G - go to end of file
  • b - (back) move to start of previous word
  • w - move to start of next word
  • e - move to end of next word

Basic operations

  • u - undo last edit
  • C-r - redo last undo
  • . - redo the last operation
  • x - remove character under cursor (c.f. delete)
  • X - remove character previous to cursor (c.f. backspace)
  • i - enter insert mode at the current position
  • a - enter insert mode at the character after the current position
  • r<c> - replace the current character with the character c
  • R - enter replace (overtype) mode
  • s - remove the current character and enter insert mode (substituting the current character)
  • J - join this line with the next
  • c<movement> - change the text
  • d<movement> - delete
  • dd - delete line
  • y<movement> - yank (copy)
  • yy - yank line
  • p - put (paste) previously deleted or yanked text after cursor
  • P - put previously deleted or yanked text before cursor
  • v - enter visual selection mode
  • V - enter visual selection mode for complete lines

A Few Movements

  • w - rest of word
  • b - previous part of word
  • iw - inner word - the whole word the cursor is within
  • aw - all of the word, including the trailing spaces
  • t<char> - to next instance of
  • ib - inner block - everything within a set of parens

Combined examples

  • xp - transposes two characters
  • 3dd - delete three lines
  • 2ciw - replace the word the cursor is currently in, and the word after with whatever gets typed next
  • dt) - delete up to the next ")" character in the line

Searching and Replacing

  • /abc - search for "abc". Can use vim regexes
  • :%s/abc/123/g - for every line in the file, replace all instances of "abc" with "123".
  • :s//xyz/ - for the current line, replace the first instance of whatever was last searched for with "xyz"
  • * - search for the next instance of the word under the cursor
  • # - search backwards for the next instance of the word under the cursor
  • n - jump to the next match for the last search
  • N - jump to the previous match for the last search

Scrolling

  • C-d - move half a screen downwards. Cursor is moved too.
  • C-u - move half a screen upwards. Cursor is moved too.
  • C-e - scroll a line downwards. Cursor is not moved if possible.
  • C-y - scroll a line upwards. Cursor is not moved if possible.
  • % - jump to the matching paren / brace ( / HTML element / XML element / ...)

Window Management

  • C-w s - split window horizontally
  • C-w v - split window vertically
  • C-w C-w - move focus to the next window
  • C-w j - move focus a window down (similarly for all of hjkl)
  • C-w J - move current window to the bottom (all of HJKL)
  • C-w + - make window 1 line taller
  • C-w - - make window 1 line shorter
  • C-w _ - make window full height
  • C-w > - make window 1 column wider
  • C-w < - make window 1 column narrower
  • C-w | - make window full width
  • C-w = - make all windows equally(ish) sized

Plugins

Really depends on what you have installed. Compare with Emacs major and minor modes.

Some things I use:

  • Fugitive - Git plugin
    • :Gstatus - interactive git status
    • :Gdiff - diff from HEAD
  • CtrlP plugin
    • C-p enters a fuzzy file search mode
  • Nerd commenter
    • \cc toggles comments on the selection
  • Supertab
    • tab - tries to complete the word based on all kinds of inputs. Usually very effective
  • Sparkup
    • HTML Zen editing. Very cool, watch the video.
    • E.g. in insert mode, type "div.section > ul#the-list > li*3e"

Clojure Plugins

  • vim-slime
    • Sending commands from vim to another tmux (or screen) window
  • Fireplace
    • akin to clojure-mode
    • Decent walkthrough on clojure-doc
  • vim-clojure-static
    • Code editing and syntax highlghting for clojure
  • vim-classpath
    • For running clj code without a repl

Clojure Walkthrough

Just scratching at the fireplace functionality here. Check out :h fireplace.txt

  • Fire up tmux
  • Create a new lein project: lein new vimclj
  • Start the lein repl, and open vim in a new tmux window
  • Open the test file, add some new code to make it fail
    • Compile the code with cpr (actually, it does (require :reload) for the current file)
    • Verify the test fails (i.e. running Clojure code from within Vim):
      • :Eval (clojure.test/run-tests)
      • cqp to open a repl prompt, type (clojure.test/run-tests)
      • cqc to open an interactive command editor, type (clojure.test/run-tests)<enter> to run the last line of code
      • Add (comment (clojure.test/run-tests)) at the end of the file
      • cqq with the cursor within the inner s-exp takes the expression into the command window ready for editing and executing
      • Again, with cursor within the inner s-exp, cpp runs that s-exp directly
  • Add some code to make the tests pass.
    • :A jumps from the test file to the implementation
    • In insert mode, <C-x> <C-o> starts up ommicomplete. <tab> runs through the choices. Editing the current selection changes the options available.
    • [d on a symbol shows the documentation for that
    • [<C-d> jumps to the definition of a symbol
    • <C-w>d jumps to the definition of a symbol in a new split
    • gf (usually opens a file) works on namespace names also
    • Use cpr to compile and load the new code, or use :Require! from the test file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment