- 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...
:e <filename>
- edits a file:w
- writes a file to disk:qa
- quits vim:qa!
- really quits vim!
When in normal mode:
i
- switch into insert mode
When in insert mode:
<esc>
- switch into normal mode
:h
- start up the help system:h <cmd>
- get help on a command (or keystroke etc - try:h CTRL-w_q
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.
-
- 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`
- Declare and configure plugins
- Disable arrow keys - force learning of
hjkl
- Set up some basic non-default settings
- Custom functions and behaviors
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.
: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.
h
,j
,k
,l
- move cursor left, down, up, right one character/line0
- move to start of line$
- move to end of linenG
- go to line ngg
- go to start of fileG
- go to end of file
b
- (back) move to start of previous wordw
- move to start of next worde
- move to end of next word
u
- undo last editC-r
- redo last undo.
- redo the last operationx
- remove character under cursor (c.f. delete)X
- remove character previous to cursor (c.f. backspace)i
- enter insert mode at the current positiona
- enter insert mode at the character after the current positionr<c>
- replace the current character with the character cR
- enter replace (overtype) modes
- remove the current character and enter insert mode (substituting the current character)J
- join this line with the nextc<movement>
- change the textd<movement>
- deletedd
- delete liney<movement>
- yank (copy)yy
- yank linep
- put (paste) previously deleted or yanked text after cursorP
- put previously deleted or yanked text before cursorv
- enter visual selection modeV
- enter visual selection mode for complete lines
w
- rest of wordb
- previous part of wordiw
- inner word - the whole word the cursor is withinaw
- all of the word, including the trailing spacest<char>
- to next instance ofib
- inner block - everything within a set of parens
xp
- transposes two characters3dd
- delete three lines2ciw
- replace the word the cursor is currently in, and the word after with whatever gets typed nextdt)
- delete up to the next ")" character in the line
/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 cursorn
- jump to the next match for the last searchN
- jump to the previous match for the last search
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 / ...)
C-w s
- split window horizontallyC-w v
- split window verticallyC-w C-w
- move focus to the next windowC-w j
- move focus a window down (similarly for all ofhjkl
)C-w J
- move current window to the bottom (all ofHJKL
)C-w +
- make window 1 line tallerC-w -
- make window 1 line shorterC-w _
- make window full heightC-w >
- make window 1 column widerC-w <
- make window 1 column narrowerC-w |
- make window full widthC-w =
- make all windows equally(ish) sized
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"
- 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
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
- Compile the code with
- 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 splitgf
(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