Created
October 1, 2024 19:54
-
-
Save fadookie/a62d29d4b9de47d8a2d252526d7ad134 to your computer and use it in GitHub Desktop.
Eliot's Vim Cheat Sheet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
vimcheatsheet.txt | |
Eliot's Vim Cheat Sheet | |
Last Updated 2011-08-11 | |
Pick up on vimtutor line 805. | |
[#] is a placeholder for a number, so an example of [#]w is 2w | |
[CHAR] is a placeholder for a character, so and example of t[CHAR] is te | |
invoking vim from the shell: | |
vim FILE - edit a file in vim | |
vim FILE1 FILE2 FILE3... - open several files in multiple buffers inside of a single vim instance | |
gvim -p FILE1 FILE2... - open files in multiple tabs in GVim | |
vimdiff FILE1 FILE2 - split-open two files showing only changes (highlighted) | |
view - open vim in read-only mode (useful for reading from STDIN, i.e. "somecommand|view -" ) | |
vimtutor - open the vim interactive tutorial | |
operators: | |
d - delete | |
c - change (delete and switch to insert mode) | |
i - switch to insert mode before selected character | |
a - append (switch to insert after character) | |
A - append at end of line (switch to insert after line) | |
r - replace character | |
R - switch to REPLACE MODE (like INSERT mode but you REPLACE characters instead of inserting them) | |
y - yank (copy) text into the "clipboard" | |
p - put/paste text AFTER the cursor or, BELOW the current line if you deleted a whole line. The text is either the last thing you deleted or yanked | |
P - put/paste text BEFORE the cursor or ABOVE the current line | |
]p - paste after but adjust indent | |
]P - paste before but adjust indent | |
movement | |
w - to beginning of next word | |
e - to end of current word | |
0 - to beginning of line | |
$ - to end of line | |
t[CHAR] - to just before the next occurance of [CHAR] | |
T[CHAR] - to just after the previous occurrence of [CHAR] | |
f[CHAR] - to the next occurrence of [CHAR] | |
F[CHAR] - to the previous occurrence of [CHAR] | |
% - to matching parentheses - ), ], or }. | |
gg - top of file | |
G - end of file | |
[#]G or [#]gg - jump to line in file | |
[#]% - jump to line at percentage length of file | |
^ - first non-blank character of the line | |
<CR> (carraige return/enter) - first non-blank character on the next line | |
- - first non-blank character on previous line | |
gi - Go to the last place you had inserted and switch to insert mode. | |
append a # to the movement command to repeat the movement # times, ex: | |
2w | |
append an operator to perform that action, eg: | |
d2w (delete 2 words) | |
ct< (change the text up until the next < character) | |
dt% (delete the text in between the matching parentheses | |
recording keystrokes: | |
q[CHAR] - begin recording keystrokes to buffer [CHAR] | |
q - end recording (if in recording mode) | |
@[CHAR] - refers to commands in recorded buffer, for instance: | |
10@[CHAR] - repeat the commands stored in (recording) across the next 10 lines | |
text objects: | |
Use after a command for movement, or in visual mode. | |
(try either THIS, or THAT command listed side-by-side for compactness) | |
aw, aW - ambient word or WORD (see docs) | |
iw, iW - inner word or WORD (see docs) | |
as, is - ambient or inner sentence | |
ap, ip - ambient or inner paragraph | |
a{, i{ - whole {..} block or text inside it | |
a(, i( - whole (..) block or just text inside it | |
a<, i< - whole <..> block or just text inside it | |
a’, i’ - single-quoted string or just the text inside | |
a”, i” - double-quoted string or just the text inside. note that these are smart about escaped quotes inside strings | |
at, it - whole tag block or just text inside (HTML and XML tags) | |
shortcuts: | |
dd - delete line | |
[#]dd - delete # lines | |
[#]r[CHAR] - replace characters with [CHAR], # times | |
o - insert a line BENEATH cursor and drop into INSERT mode | |
O - insert a line ABOVE cursor and drop into INSERT mode | |
u - undo a change | |
U - revert an entire line | |
CTRL+R - redo (why the hell is this one of the only commands in vim that requires a key chord? sigh.) | |
[#]G - go to line # in the file | |
CTRL+g - print info about file, current line, etc. | |
CTRL+o - Navigate backwards in jump history | |
CTRL+i - Navigate forwards in jump history | |
misc: | |
ga - Print the ascii value of the character under the cursor | |
visual mode: | |
v - enter visual mode (default - characterwise, single characters at a time) | |
V - enter linewise visual mode (select whole lines at once) | |
move cursor to select text. | |
enter any operator to perform that operation on the selected block of text. | |
: - enter command mode and auto-append '<,'> which represents the currently selected text | |
:'<,'>w FILENAME - write selection to any arbitrary file | |
= - Beautify selected code according to currently loaded syntax rules | |
visual blockwise mode: | |
CTRL-v - enter visual block selection mode (a rectangle) | |
I - insert mode. Type a string and then press ESC. The string will be inserted before each line of the beginning of the blockwise selection | |
A - append mode. Similar to insert mode, but start by appending a character, especially useful for mass appends to the end of a line. | |
Use any operator when in visual blockwise mode... | |
ex commands: | |
type : to enter a command quickly. | |
type q: to enter command-line mode which has a command history and is editable like a normal text buffer | |
combine commands on the same line with a | character | |
:help - open manual (in a horizontal split view) | |
:close - close a window (in a split view) | |
:[#] - go to line # in the file | |
:w - write (save) changes | |
:w FILENAME - save as to any arbitrary file | |
:wq - write changes and quit | |
SHIFT+ZZ - same as :wq | |
:q - quit (prompt if there are changes) | |
:q! - quit without saving | |
:e - open a file (in a new buffer) for editing | |
:r FILENAME - retrieve contents of FILENAME and insert BELOW the cursor | |
:r !command - retrieve the output of command and insert BELOW the cursor - fuckin' cool | |
:buffers - show open file buffers (think of them like old school tabbed windows) | |
:bn - switch to next buffer | |
:bp - switch to previous buffer | |
:b[#] - switch to buffer number # | |
:bd - delete (close) current buffer | |
:marks - view marks in file (file jump history, manual marks) | |
:!command - execute any arbitrary shell command, ex: ":!php myfile.php" | |
:syntax on - Enable syntax highlighting | |
:set syntax=[LANGUAGE] - Set programming language for syntax highlighting | |
CTRL-b - Beginning of line in command mode | |
CTRL-e - End of line in command mode | |
CTRL-r CTRL-w - Copy word under the cursor into the command line - see :help c_CTRL-R_CTRL-W | |
CTRL-r - Paste word from register. Type a number after to use a numbered register. See :help c_CTRL-R | |
window: | |
CTRL-W _ (set window to max height) | |
tabs: | |
vim -p - From the command line, -p opens all files in tabs instead of buffers | |
gt - Next tab | |
gT - Previous tab | |
:qa - Quit all tabs | |
:wqa - Save and quit all tabs | |
:tabdo - Run ANY command across ALL open tabs! ex ":tabdo w" to save all tabs, ":tabdo normal @a" to run macro A on all tabs, etc. | |
search: | |
type / to begin entering a search (that will look forwards from the cursor position) | |
type ? to begin entering a search that will look backwards from the cursor position | |
type a search term & enter to start the search | |
append \c for a case insensitive search, ex: /searchterm\c | |
append \C for a case sensitive search | |
* - find next instance of whole world under cursor | |
# - find previous instance of whole word under cursor | |
g* - find next instance of partial word under cursor | |
g# - find previous instance of partial word under cursor | |
:set ignorecase - set case-insensitive search as default | |
:vimgrep /pattern files - use vim to search over the specified files and display results in the quickfix window (see :help quickfix) | |
:grep /pattern files - use system rep to search, works the same as vimgrep except that it expects grep-style searches, etc. | |
commands for after starting a search: | |
n - snap the cursor to the next result. | |
N - snap the cursor to the previous result | |
CTRL-o - go back to where you last came from | |
CTRL-i - go forwards in history | |
search and replace: | |
:s - substitute command | |
:s/old/new - replace the first occurance of "old" with "new" on the current line | |
:s//new - replace the first occurance of whatever you last searched for with "new" | |
:s/old/new/g - replace all occurances of "old" with "new" on the current line ('global' replace) | |
:#,#s/old/new/g - replace all occurances of "old" with "new" in the line number range between #,#, ex: 10,25 | |
:'<,'>s/old/new - replace all occurances of "old" with "new" in the range selected in visual mode (this gets entered automatically when going into command mode from visual mode) | |
:%s/old/new/g - replace all occurances of "old" with "new" across the ENTIRE FILE | |
:%s/old/new/gc - replace all occurances of "old" with "new" across the ENTIRE FILE and prompt to CONFIRM each change | |
:%s'\(<old>\)'\1<new>'gc - the stuff between \( and \) (the "atom") gets grouped as a backreference you can use in your replace by number, eg. \1 will be replaced with atom 1. | |
CTRL-r / - Copy text from the last search into the command buffer! Whooooo! | |
Regex: | |
\%d[NUM] - Also x, o, u.. Match a character code, see :help /\%x and ga | |
\r - use in replace to insert a return character, regardless of file encoding | |
Global ex commands: | |
These can be used to run macros and all sorts of shit | |
:g/old/d - remove all lines matching pattern old | |
:%g/old/normal [somecommand] - run some normal vim keystrokes on all occurrences | |
:%g/.../normal @[CHAR] - run recorded macro in register [CHAR] on all occurrences | |
:v/old/ - like g, but execute command on all lines that DON'T match old | |
if the sequence you need to perform is complex, go ahead and make a macro, and to run it in the desired spots, do :%g/.../normal @q (assuming q is the macro register) | |
Plugins: | |
NERDTree: | |
fn-F2 - open NERDTree (custom shortcut) | |
o - open the file in a new buffer or open/close directory | |
t - open the file in a new tab | |
i - open the file in a new horizontal split | |
s - open the file in a new vertical split | |
p - go to parent directory | |
r - refresh the current directory | |
NERDTreeFind - Ex command to select current file in NERDTree | |
Other resources: | |
http://www.yolinux.com/TUTORIALS/LinuxTutorialAdvanced_vi.html | |
http://www.slideshare.net/ZendCon/vim-for-php-programmers-presentation | |
http://mwop.net/blog/134-exuberant-ctags-with-PHP-in-Vim | |
Tips: | |
http://dailyvim.blogspot.com/2008/03/delete-lines-matching-keyword.html | |
http://vim.1045645.n5.nabble.com/copy-paste-in-command-line-td1176926.html | |
http://durgaprasad.wordpress.com/2007/09/25/find-replace-non-printable-characters-in-vim/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment