##vim
Vim is a text editor available on most unix systems, and can easily be installed via package managers if not already installed.
- Great for working in the terminal.
- Somewhat steep learning curve, but improves your speed over time.
- Terminal-based text editor is the tool of choice for most professional coders (vim or emacs).
My reasoning for using Vim over other text editors and IDEs:
- You look way cooler
- Time = $$
- While you should use what works for you, for me I did not want to invest time and energy in an IDE or editor that may not be so popular or supported in the future. It's a big investment, but it pays off with the increased speed, knowledge and flexibility with development environment
- Become a unix boss
- By committing to using Vim, you are learning a core component of the unix ecosystem. Knowledge of this tool increases knowledge of other tools, which isn't always true with other text editors.
- Vim provides inspiration for other tools, and is inspired by other tools. Just like a long running TV sitcom, Vim will always be there, always has been there, and is always moving forward. It's available on all systems, to all devs around the world.
- Learning Vim helps increase literacy on unix machines, like web servers that you may have to configure from time to time. When you ssh into your server, you can use the text editor available with ease, Vi or Vim.
- It's free and open source
- by committing to an open source tool, you are supporting the open source ecosystem
- Did I mention you look cooler?
###Goals:
This is a simple read to test the Vim waters. There are more references listed at the bottom of the gist to continue learning, and a quick DuckDuckGo search will return hundreds of resources on vim for whatever question you may have.
The basic goals here are:
- Learn enough Vim to be dangerious
- Discuss why you would want to use Vim?
- Increase speed when writing code
- Increase unix knowledge
###Basic commands
Getting Started:
To start, let’s edit ~/.vimrc
(this hidden config file is called a dotfile. It's a configuration file in the home dir)
- the vimrc file is where you put options. Here's my vimrc.
type vim ~/.vimrc
from your terminal to edit this file in Vim
Within Vim:
type :e someFileName
to open and edit a different file
type :vsplit
to split the window vertically
type :vsplit filename
to open a file in a new vertical split
Indent the line by pressing >
twice. Un-indent with <
twice.
press .
to redo the last action (deleting a line, indenting, etc etc)
u
to undo last change
<Ctrl-r>
to redo last change
type i
to start typing (Insert Mode), and hit <Esc>
to go back to the previous Normal Mode
type :q
to quit this file, or :wq
to write the file, then quit, or simply :w
to write the file without quitting.
if you don't have to save changes (likely in this case), type :q!
Up - k
Down - j
Left - h
Right - l
or use arrows
Normal Mode is what you are in when Vim starts, and what you return to when you escape other modes. Example operation in normal mode:
r
- replace the text under the cursor with the next text input.
x
- delete text under cursor
p
to paste from the clipboard
i
to enter Insert Mode
Exit modes by pressing Escape, denoted as <Esc>
Type commands in bottom left.
<S-;>
(this reads as Shift + ;) to enter command mode, then type your command, then press enter:
:help
to access help
:w
to write file
:q!
to quit without writing file
Get to previous command by pressing up arrow
Press <Esc>
to exit.
press i
, then edit text with the keyboard like other text editors. Press <Esc>
to exit.
o
to enter Insert Mode and go to a new line below cursor
<S-o>
to enter Insert mode and go to a new line above cursor
press v
, then select text. Use Visual mode to perform operations on the selected text.
Example use case may be highlighting a word and copying it to the clipboard.
$
to move to end of line
^
to move to beginning of line
(look familiar? These are regex commands…learning vim helps with other tools)
<S-g>
move to end of file (read as Shift + g)
gg
move to beginning of file:
hover over a file name, gf
goes to file
Split windows:
<Ctrl-w> v
- splits vertical
:split
also works for horizontal
:vsplit
works for horizontal
:split another***file
opens split from another file
<Ctrl-w> w
- jumps between splits
<Ctrl-w> arrow
- jumps to certain direction
increase size of window: <Ctrl-w> +
:close
or :q will close a window
:new
opens a new window
Editing text:
from normal mode:
w
- go to next word
b
- go to previous word
x
- delete char under cursor and copy to clipboard
y
- copies to clipboard
You can often chain vim commands together, for example by pressing a number x and then an operation, that operation is performed x times.
Copy and Pasting:
yw
copies word;
y5w
copies 5 words;
yy
copies entire line
p
- paste from clipboard from default registry
change registry with “somekey
“
Registries:
Type "
followed by any key to use a registry saved to that key.
*
is the system registry, so to copy into the system clipboard, use "*y
and to paste: "*p
ex: "ayy
puts the current line in the registry "a"
"ap
pastes from registry "a"
"byy
puts current line in registry "b"
Deleting:
d
- delete modifier - it needs something entered after it to perform the delete on.
dd
deletes entire line
dw
deletes word going forward
db
delete word going back
Nnumbers, like 5, are modifiers too:
5dw
deletes the next 5 words
5b
moves back 5 words
5 + down arrow
moves down 5 lines
u
- undo
<Ctrl r>
- redo
Search:
/
(forward search)
?
(backward search)
n
- next result
Find and replace
This is done in command mode, and takes a term, a new term, and some options at the end, such as gc
:
:%s/term/newterm/gc
here,
gc
means global and confirm. leave offgc
to just find and replace the first term. leave ofc
to not confirm your replacements.
recording:
To start recording, press q
in normal mode followed by a letter (a to z). That starts recording keystrokes to the specified register. Vim displays recording in the status line. Type any normal mode commands, or enter insert mode and type text. To stop recording, again press q
while in normal mode.
To playback your keystrokes, press @
followed by the letter previously chosen. Typing @@
repeats the last playback.
mapping keys to commands: Vim is a Modal editor, meaning you need to know which mode you are in to perform a certain operation correctly.
####Mode letters:
n: normal only
v: visual and select
o: operator-pending
x: visual only
s: select only
i: insert
c: command-line
l: insert, command-line, regexp-search (and others. Collectively called "Lang-Arg" pseudo-mode)
More on mapping Difference between remap, noremap, etc
plugins:
Vim has various plugin managers that you can use to handle using and installing plugings. You can use Pathogen, Vundle, Janus, and probably 10 more to do the job. I prefer Vundle as it is similar to the bundle command: Vundle Docs
With Vundle, you place your plugins in your ~/.vimrc
file, and install them by typing :PluginInstall
or :VundleInstall
from within Vim.
For example, to start using Vundle and to install the vim-rails
plugin, which gives you all sorts of useful commands when working with Rails projects, add this to your ~/.vimrc
:
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
" let Vundle manage Vundle, required
Plugin 'gmarik/Vundle.vim'
Bundle 'tpope/vim-rails'
Bundle 'scrooloose/nerdtree'
" All of your Plugins must be added before the following line
call vundle#end() " required
Cursor movement:
h - move left
j - move down
k - move up
l - move right
w - jump by start of words (punctuation considered words)
W - jump by words (spaces separate words)
e - jump to end of words (punctuation considered words)
E - jump to end of words (no punctuation)
b - jump backward by words (punctuation considered words)
B - jump backward by words (no punctuation)
0 - (zero) start of line
^ - first non-blank character of line
$ - end of line
G - Go To command (prefix with number - 5G goes to line 5)
Note: Prefix a cursor movement command with a number to repeat it. For example, 4j moves down 4 lines.
Insert Mode - Inserting/Appending text
i - start insert mode at cursor
I - insert at the beginning of the line
a - append after the cursor
A - append at the end of the line
o - open (append) blank line below current line (no need to press return)
O - open blank line above current line
ea - append at end of word
Esc - exit insert mode
Editing
r - replace a single character (does not use insert mode)
J - join line below to the current one
cc - change (replace) an entire line
cw - change (replace) to the end of word
c$ - change (replace) to the end of line
s - delete character at cursor and subsitute text
S - delete line at cursor and substitute text (same as cc)
xp - transpose two letters (delete and paste, technically)
u - undo
. - repeat last command
Marking text (visual mode)
v - start visual mode, mark lines, then do command (such as y-yank)
V - start Linewise visual mode
o - move to other end of marked area
Ctrl+v - start visual block mode
O - move to Other corner of block
aw - mark a word
ab - a () block (with braces)
aB - a {} block (with brackets)
ib - inner () block
iB - inner {} block
Esc - exit visual mode
Visual commands
> - shift right
< - shift left
y - yank (copy) marked text
d - delete marked text
~ - switch case
Cut and Paste
yy - yank (copy) a line
2yy - yank 2 lines
yw - yank word
y$ - yank to end of line
p - put (paste) the clipboard after cursor
P - put (paste) before cursor
dd - delete (cut) a line
dw - delete (cut) the current word
x - delete (cut) current character
Exiting
:w - write (save) the file, but don't exit
:wq - write (save) and quit
:q - quit (fails if anything has changed)
:q! - quit and throw away changes
Search/Replace
/pattern - search for pattern
?pattern - search backward for pattern
n - repeat search in same direction
N - repeat search in opposite direction
:%s/old/new/g - replace all old with new throughout file
:%s/old/new/gc - replace all old with new throughout file with confirmations
Working with multiple windows and files
https://www.cs.oberlin.edu/~kuperman/help/vim/windows.html
If you want, you can probably do everything from one vim session! :) Here are some commands to turn one vim session (inside one xterm) into multiple windows.
:e filename - edit another file
:split filename - split window and load another file
:ctrl-w s - splits window horizontally (same as :split)
ctrl-w up arrow - move cursor up a window
ctrl-w ctrl-w - move cursor to another window (cycle)
ctrl-w_ - maximize current window (horizontal)
ctrl-w= - make all equal size
10 ctrl-w+ - increase window size by 10 lines
ctrl-w v - split vertially (same as :vsplit)
ctrl-w < or > - increase size left or right (vertical split)
ctrl-w 5< or 10> - increase size by number modifier left or right (vertical split)
:vsplit file - vertical split
:sview file - same as split, but readonly
:hide - close current window
:only - keep only this window open
Buffers:
:b someOpenBuffer - switch to open buffer
:bnext (or :bn) - go to next buffer
:bprev (of :bp) - go to previous buffer
:bd - delete a buffer (close a file)
:sp filename - Open a file in a new buffer and split window
:ls - show current buffers
:b 2 - open buffer #2 in this window
Another good vim commands cheatsheet