Basic Operations#
Command Mode#
:q
Quit (close window):w
Save (write):wq
Save and quit:e {filename}
Open file for editing:ls
List open buffers:help {subject}
Open help documentation:help :w
Open help documentation for:w
command:help w
Open help documentation forw
motion
Motion - Mostly in Normal Mode#
- Basic motions:
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) - Screens:
H
(top of screen),M
(middle of screen),L
(bottom of screen) - Paging:
Ctrl-u
(up),Ctrl-d
(down) - Files:
gg
(beginning of file),G
(end of file) - Line numbers:
:{number}<CR>
or{number}G
(where {number} is a line number) - Miscellaneous:
%
(find matching pair, e.g. parentheses or /* */ comment pairs) - Searching:
f{char}
,t{char}
,F{char}
,T{char}
- Search/To forward/backward on the current line for {char}
,
/;
for navigating matches
- Searching:
/{regex}
,n
/N
for navigating matches
Selection - Visual Mode#
- Visual:
v
- Visual line:
V
- Visual block:
Ctrl+v
Editing#
i
Enter insert mode- But for manipulating/editing text, don't just use backspace
O
/o
Insert line above/belowd{motion}
Delete {motion}- e.g.,
dw
delete word,d$
delete to end of line,d0
delete to beginning of line
- e.g.,
c{motion}
Change {motion}- e.g.,
cw
change word - e.g.,
d{motion}
theni
- e.g.,
x
Delete character (same asdl
)s
Substitute character (same asxi
)- Visual mode + operation
- Select text, then
d
to delete orc
to change
- Select text, then
u
Undo,<C-r>
Redoy
Yank/copy (also copies with some other commands liked
)p
Paste- More advanced: e.g.,
~
change case of character
Counting#
3w
Move forward three words5j
Move down 5 lines7dw
Delete 7 words
Modifiers#
You can use modifiers to change the meaning of a "noun". Modifiers are i
, meaning "inside" or "in", and a
, meaning "around".
ci(
Change inside parenthesesci[
Change inside square bracketsda'
Delete a single-quoted string, including the surrounding quotes
Demo#
# fizzbuzz.py
import sys
def fizz_buzz(limit):
for i in range(1,limit+1):
if i % 3 == 0:
print('fizz', end="")
if i % 5 == 0:
print('buzz', end="")
if i % 3 and i % 5:
print(i)
else:
print()
def main():
fizz_buzz(int(sys.argv[1]))
if __name__ == '__main__':
main()
.
Repeat last changesys.argv[1]
First argument passed in
Customizing Vim#
- missing-semester
- Anish
- Jon (uses neovim)
- Jose
vim ~/.vimrc
vim /etc/vim/vimrc # debian
Extending Vim#
Common recommended plugins:
- ctrlp.vim: Fuzzy file finder
- ack.vim: Code search
- nerdtree: File browser
- vim-easymotion: Magic motions
Instructors' open source configuration files:
Vim Mode in Other Programs#
Shell#
Bash set -o vi
Zsh bindkey -v
Fish fish_vi_key_bindings
Generic export EDITOR=vim
Readline#
vim ~/.inputrc
vim /etc/inputrc # debian
set editing-mode vi # Enable vi editing mode, Python REPL supports Vim shortcuts
Advanced Vim#
Search and Replace#
:s
(substitute) command (documentation).
%s/foo/bar/g
- Replace foo with bar throughout the file
%s/\[.*\](\(.*\))/\1/g
- Replace named Markdown links with simple URLs
Multiple Windows#
- Use
:sp
/:vsp
to split windows - The same buffer can be displayed in multiple windows
Macros#
q{char}
to start recording a macro in register{char}
q
to stop recording@{char}
to replay the macro- Macros stop if there is an error
{count}@{char}
to execute a macro{count}
times- Macros can be recursive
- Clear the macro with
q{char}q
first - Record the macro, use
@{char}
to recursively call the macro (no operations until recording is complete)
- Clear the macro with
- Example: Convert XML to JSON (file)
- An array of objects with "name" / "email" keys
- With a Python program?
- With sed / regex
g/people/d
%s/<person>/{/g
%s/<name>\(.*\)<\/name>/"name": "\1",/g
- …
- Vim commands / macros
Gdd
,ggdd
to delete the first and last lines- Format the last element with a macro (register
e
)- Jump to the line with
<name>
qe^r"f>s": "<ESC>f<C"<ESC>q
- Jump to the line with
- Format one with a macro
- Jump to the line with
<person>
qpS{<ESC>j@eA,<ESC>j@ejS},<ESC>q
- Jump to the line with
- Format one tag and move to the next with a macro
- Jump to the line with
<person>
qq@pjq
- Jump to the line with
- Execute the macro to the end of the file
999@q
- Manually remove the trailing
,
and add[
and]
separators
Additional Resources#
vimtutor
is a tutorial that comes with Vim- Vim Adventures is a game for learning Vim
- Vim Tips Wiki
- Vim Advent Calendar has many Vim tips and tricks
- Vim Golf is code golf with a Vim interface
- Vi/Vim Stack Exchange
- Vim Screencasts
- Practical Vim (book)
Exercises#
- Complete
vimtutor
. Note: It looks best in an 80x24 (80 columns, 24 rows) terminal window. - Download the provided vimrc and save it as
~/.vimrc
. Read through the commented file (using Vim!) and observe the subtle differences in how Vim looks and behaves with this new configuration. - Install and configure a plugin: ctrlp.vim.
- Create the plugin folder with
mkdir -p ~/.vim/pack/vendor/start
- Download the plugin:
cd ~/.vim/pack/vendor/start; git clone https://github.com/ctrlpvim/ctrlp.vim
# Add the following configuration to vimrc after downloading set runtimepath^=~/.vim/pack/vendor/start/ctrlp.vim
3. Read the plugin's [documentation](https://github.com/ctrlpvim/ctrlp.vim/blob/master/readme.md). Try using CtrlP to locate a file within a project folder by opening Vim and using the Vim command-line to start `:CtrlP`. 4. Customize CtrlP: Add [configuration](https://github.com/ctrlpvim/ctrlp.vim/blob/master/readme.md#basic-options) to your `~/.vimrc` to open CtrlP with Ctrl-P.
- Create the plugin folder with
- Practice using Vim by redoing the demo on your own machine.
- Use Vim for all file editing for the next month. Whenever you find yourself being inefficient or thinking "there must be a better way," try searching the web, as there likely is a better way. If you encounter any roadblocks, come to our office hours or send us an email.
- Set up Vim shortcuts in other tools (see the guide above).
- Further customize your
~/.vimrc
and install more plugins.
Install plugins usingvim-plug
:- Install vim-plug
$ curl -fLo ~/.vim/autoload/plug.vim --create-dirs \ https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
- Modify ~/.vimrc
call plug#begin() Plug 'preservim/NERDTree' # Plugin NERDTree to be installed Plug 'wikitopian/hardmode' # Install hardmode ..... # More plugins call plug#end()
- Run
:PlugInstall
in the vim command line
- (Advanced) Use a Vim macro to convert XML to JSON (example file). Try doing it on your own first, but if you get stuck, you can refer to the macros section above.