#Vim #TextEditing #Programming #Tutorial #Resources #Plugins #AdvancedVim
Learning Editors (Vim)
Source link: Editors (Vim) · Missing Semester
How to Learn a New Editor
- Start with a tutorial (i.e. this lecture, plus resources that we point out)
- Stick with using the editor for all your text editing needs (even if it slows you down initially)
- Look things up as you go: if it seems like there should be a better way to do something, there probably is
Philosophy of Vim
- Vim is a modal editor: it has different modes for inserting text vs manipulating text.
- Vim is programmable (with Vim script and also other languages like Python).
- Vim’s interface itself is a programming language: keystrokes (with mnemonic names) are commands, and these commands are composable.
- Vim avoids the use of the mouse, because it’s too slow; Vim even avoids using the arrow keys because it requires too much movement.
Modal Editing
- Vim has multiple operating modes:
- Normal: for moving around a file and making edits
- Insert: for inserting text
- Replace: for replacing text
- Visual (plain, line, or block): for selecting blocks of text
- Command-line: for running a command
- Change modes by pressing
<ESC>(the escape key) to switch from any mode back to Normal mode. - From Normal mode:
- Enter Insert mode with
i - Replace mode with
R - Visual mode with
v - Visual Line mode with
V - Visual Block mode with
<C-v>(Ctrl-V, sometimes also written^V) - Command-line mode with
:
Basics
- Inserting text: Though not particularly efficient if you’re spending all your time editing from Insert mode.
- Buffers, tabs, and windows
- Command-line:
:qquit (close window):wsave (“write”):wqsave and quit:e {name of file}open file for editing:lsshow open buffers:help {topic}open help:help :wopens help for the:wcommand:help wopens help for thewmovement
Vim’s Interface is a Programming Language
Movement ("nouns")
- Basic movement:
hjkl(left, down, up, right) - Words:
w(next word),b(beginning of word),e(end of word) - Lines:
0(beginning of line),^(first non-blank character),$(end of line) - Screen:
H(top of screen),M(middle of screen),L(bottom of screen) - Scroll:
Ctrl-u(up),Ctrl-d(down) - File:
gg(beginning of file),G(end of file) - Line numbers:
:{number}<CR>or{number}G(line {number}) - Misc:
%(corresponding item) - Find:
f{character},t{character},F{character},T{character} - Find/to forward/backward {character} on the current line
,/;for navigating matches- Search:
/{regex},n/Nfor navigating matches
Selection
- Visual modes:
- Visual:
v - Visual Line:
V - Visual Block:
Ctrl-v - Can use movement keys to make selection.
Edits ("verbs")
ienter Insert mode- For manipulating/deleting text, want to use something more than backspace
o/Oinsert line below / aboved{motion}delete- e.g.
dwis delete word,d$is delete to end of line,d0is delete to beginning of line c{motion}change- e.g.
cwis change word - Like
d{motion}followed byi xdelete character (equal todl)ssubstitute character (equal toxi)- Visual mode + manipulation:
- Select text,
dto delete it orcto change it uto undo,<C-r>to redoyto copy / “yank” (some other commands likedalso copy)pto paste- Lots more to learn: e.g.
~flips the case of a character
Counts
- Combine nouns and verbs with a count:
3wmove 3 words forward5jmove 5 lines down7dwdelete 7 words
Modifiers
- Use modifiers to change the meaning of a noun. Some modifiers are
i, which means “inner” or “inside”, anda, which means “around”. ci(change the contents inside the current pair of parenthesesci[change the contents inside the current pair of square bracketsda'delete a single-quoted string, including the surrounding single quotes
Customizing Vim
- Vim is customized through a plain-text configuration file in
~/.vimrc - Download our config here and save it to
~/.vimrc. - Instructors’ Vim configs:
- Anish
- Jon (uses neovim)
- Jose
- Try not to copy-and-paste people’s full configuration, but read it, understand it, and take what you need.
Extending Vim
- Simply create the directory
~/.vim/pack/vendor/start/, and put plugins in there - Here are some of our favorite plugins:
- ctrlp.vim: fuzzy file finder
- ack.vim: code search
- nerdtree: file explorer
- vim-easymotion: magic motions
- Check out Vim Awesome for more awesome Vim plugins
Vim-mode in Other Programs
- Shell:
- If you’re a Bash user, use
set -o vi. - If you use Zsh,
bindkey -v. - For Fish,
fish_vi_key_bindings. - You can
export EDITOR=vim. - Readline:
- The GNU Readline library supports (basic) Vim emulation too, which can be enabled by adding the following line to the
~/.inputrcfile:set editing-mode vi - Others:
- Vimium for Google Chrome
- A long list of software with vim-like keybindings.
Advanced Vim
Search and Replace
:s(substitute) command (documentation).%s/foo/bar/g: replace foo with bar globally in file%s/\[.*\](\(.*\))/\1/g: replace named Markdown links with plain URLs
Multiple Windows
:sp/:vspto split windows- Can have multiple views of the same buffer.
Macros (More Advanced)
- // To be continued
Resources
vimtutoris a tutorial that comes installed with Vim - if Vim is installed, you should be able to runvimtutorfrom your shell- Vim Adventures is a game to learn Vim
- Vim Tips Wiki
- Vim Advent Calendar has various Vim tips
- Vim Golf is code golf, but where the programming language is Vim’s UI
- Vi/Vim Stack Exchange
- Vim Screencasts
- Practical Vim (book)