Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Learn Vim in Vim
" copy all this into a vim buffer, save it, then...
" source the file by typing :so %
" Now vim acts like a specialized application for learning vim
" type ,c (that's comma c)
" You don't really know the command at all and want to see it again soon.
" ,c moves the current command down a several positions in the 'Study' stack
" so you'll see it again soon.
nmap ,c ^v/^$<cr>dma/^$<cr>/^$<cr>/^$<cr>/^$<cr>jP'azt<c-y><c-l>
" type ,, (that's comma comma)
" You know the command pretty well, but not enough to move it to 'Known'.
" ,, moves the current command to the bottom of the 'Study' stack.
nmap ,, ^v/^$<cr>dma/^= Known<cr>P'azt<c-y><c-l>
" type ,k (that's comma k)
" You have the command down cold. Move it to the 'Known' stack.
" ,k moves the current command into the 'Known' stack.
nmap ,k ^v/^$<cr>dma/^= Known<cr>jjP'azt<c-y><c-l>
" type ,h (that's comma h)
" or
" type ,j (that's comma j)
" Hide or show the commands by changing their background color. If you hide
" the command it's much harder to cheat by looking ;)
highlight CommandsHidden guifg=white guibg=white
nmap ,h :match CommandsHidden /^\n.*$/<cr>:highlight Cursor guifg=white guibg=white<cr>
nmap ,j :match CommandsHidden //<cr>:highlight Cursor guifg=white guibg=black<cr>
" Ok, it's time to get this party started. Move to the top of the study stack
" and go for it!
/^= Study
normal jztj
" Note: this line keeps the rest of the file from being treated as vimscript
= Study
reverse the characters in a visual selection
exchange the window with the next window (like if you split a new buffer into the wrong window location)
:echo line('.')
in command mode, echo the current line number
visually select *around* a set of parentheses. Try it by moving the cursor (somewhere in here) and trying it
rm /tmp/clip.txt ; vim -c "normal \"+p" -c "wq" /tmp/clip.txt
Save the contents of the clipboard to a file by opening, pasting into, and closing vim.
move the cursor backward to right before the previous occurrence of the character x on the current line.
move the cursor backward to the previous occurrence of the character x on the current line.
scroll back one page
move the cursor forward to the next occurrence of the character x on the current line
move current line to top of page
same as fx, but moves the cursor to right before the character, not all the way to it.
scroll forward one page
move current line to middle of page
repeat the last f command you gave
move current line to bottom of page
execute the vim code in the current line. To execute it in the shell, type :! at the beginning of the line
mark: set a mark in the 'A' register (globally)
make the selected text lower case
make the selected text upper case
paste yanked text into the vim command line
mark: return to a globally set mark, even if in another buffer
line completion
move forward in the jump list
move backward in the jump list
open file under the cursor
remove all those nasty ^M characters from the end of each line in a file
autoindent lines already selected in visual mode
autoindent current line
in insert mode switch to normal mode for one command
format the current paragraph
jumps to last modified line
jumps to exact position of last modification
:h slash< CTRL-d >
list all help topics containing the word "slash"
go backward in the change list in a file
go forward in the change list in a file
yank the current line into register "a"
fold: moves the cursor to the next fold
fold: moves the cursor to the previous fold
:map <f6>
show what is currently mapped to <f6>
show all the mappings
show the content of all registers
fold: decrease the fold level by one
fold: increase the fold level by one
toggle between last two buffers
in the vim command line, insert the result of a 5*5 calculation
qQ ... added commands ... q
append more commands to a pre-existing @q register macro
:Rextract _partial_name.erb
rails.vim: extract some functionality into a partial
open the cucumber feature with that name [tag:setup_specific:gem]
(while searching or ex mode) do previous search or command
(while searching or ex mode) do next search or command
(while searching or ex mode) see previous searches or commands
count the number of occurrences of "forward" in a file
see previous commands
see previous searches
back a paragraph
forward a paragraph
back a sentence
forward a sentence
find matching parenthesis
join two lines
reformat the selected text
transpose two letters (delete and paste, technically)
move to the end of the word
append at end of word
move the cursor forward by a word
move the cursor backward by a word
in insert or command-line mode this turns the next thing typed into a literal
:set spell
Switch on spell checking
in insert mode correct the spelling of the current word
block comment the next three JavaScript lines
copy the current selection to a clipboard where other programs can use it
change all the words in between two quotes
switch to search command mode, then copy in the word under the cursor
Go to the next item in the quickfix list
Go to the previous item in the quickfix list
insert last command line command
insert last search command
:w !sudo tee %
save the current file as root (in case you opened it up without sudo accidentally and made changes to it)
in Ex mode, insert the last command
In insert mode, insert the character right above the cursor
In insert mode, delete the current line from the cursor position to the beginning of the line. [tag:learn:gem]
In insert mode, re-insert the text inserted in the previous insert session.
in Ex mode, insert the last search
When typing something into the command line, switch to the editable command-line mode where the command line becomes a fully vim-compatible text area. [tag:learn:gem]
when in a visual selection, which key will toggle to the other end of the selection?
:h i_CTRL-R
get help for how control r is used in insert mode
:h c_CTRL-R
get help for how control r is used in command mode
:s/\%V //g
remove all the spaces from the current visual selection, which is only a partial line, not a full line
if expandtab is set, this will change all the tabs to spaces, expanding them as appropriate
maximize size of window split
insert at the beginning of the line
remark area that was just marked
same as :wq
redraw the screen
completes using filenames from the current directory.
block selection (column editing)
fold: open a fold at the cursor
delete to the end of the line
change to the end of the line
reload the vimrc file (or ":so %" if you happen to be editing the file)
append at the end of the line
decrement a number when in normal mode
increment a number when in normal mode
NERDTree: opens the filesystem menu for a file, allowing you to remove, rename, etc
mark: set a mark in the 'a' register in the current buffer
mark: return to the 'a' mark in the current buffer
uppercase or lowercase the character under the cursor
repeat the last command
switch between windows
show lines containing the word under the cursor
redir @a
redirect the output of an Ex command into buffer a
= Known
write the file and quit. This is basically here just so that there's something in the "Known" stack.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment