Terminal:
OPTION+LEFT/RIGHTskips words backwards/forwards.CTRL+kdeletes everything at the right of the cursor.CTRL+udeletes everything at the left of the cursor.
Vim:
- in Command Mode:
-
gg/Ggoes to start/end of file.39ggmoves cursor to line 39.39Gmoves cursor to line 39.
-
H/M/Lmoves to top/middle/bottom of screen. -
f/Fcharacterfinds forwards/backwards a character.3ftlooks for the 3rd 't' after the cursor.dftdelete up to 't'
-
t/Tcharactergoes to cursor posititon adjacent to the character. -
;/,repeats command regardingfandtforwards and backwards. -
yanking:
yecopies until the end of word (doesn't copy extra whitespace)yt)copy from cursor until parenthesis.Y/yycopies the whole line.
-
text objects:
caw'change around word'cischange inner sentencecaschange around sentence (likecisbut also deletes extra whitespacediwdelete inside word. Deletes word under cursor (excluding whitespace).dawdelete around word. Deletes word under cursor (including whitespace).dGdelete until the end of file.dggdelete until the start of file.~change case of char(s) under cursor.Imove to the first non-blank character in the line, then go to insert mode.- Visual mode:
2asselects two sentences.
-
w/Wmoves to the beginning of the next word.3d2wwill repeatd2w3 times.
-
e/Emoves to the end of next word. -
g+e/Emoves to the last end of word/WORD. -
b/Bmoves backwards, with cursor at end of word. -
x/dldeletes what's under the cursor. -
X/dhbackspace. -
D/d$deletes from cursor until the end of line. -
C/c$deletes from cursor until the end of line and then goes to insert mode. -
s/cldeletes the character under cursor, and goes to insert mode. -
Sdeletes the whole line and then goes to insert mode. -
r some_characterreplaces char under cursor with some_characterr <Enter>will replace a character with a line break.4r <Enter>will replace 4 characters with a line break.
-
Rreplace mode. -
Aappends end of line. -
aappends after cursor. -
p/Ppastes before/after cursor (line, if in visual block mode).xpswaps two characters.
-
Vppastes to current line. -
.repeats the last change. Works for all changes except undo, redo, and:commands.- Use case example:
/some_search_stringsearches for the nearest occurence of the search string.r<Enter>replace char under cursor with some line break.ngoes to the next instance of the search..repeats the last change (i.e.r<Enter>).
- Use case example:
-
venter visual mode.o/Ogoes to start/end of selection (works for visual block mode, too).
-
<CTRL>vshifts to visual block mode.- Great for editing tabular data!
-
#/*searches backwards/forwards for word under cursor. -
%jumps between matching pairs([], etc.)50%moves cursor halfway to the end of file.
-
CTRL+Tgoes back to last buffer. -
:! + some_external_commandexecutes the external command. -
:r <Filename>inserts the file contents to buffer. -
:r !lsreads the output of the ls command and puts it below the cursor. -
:<some_partial_name_of_vim_command> <CTRL + D>will show the list of commands that start out with the some_partial_name_of_vim_command string -
Telling where you are:
:set rulerdisplays the absolute line number and horizontal position of cursor.:set numberdisplays absolute line numbers.CTRL+Gdisplays current file path.
-
Scrolling:
CTRL+f/bmoves up/down a whole screen.CTRL+u/dmoves up/down half a screen.CTRL+e/ymoves up/down 1 line.z+t/z/bpositions line where cursor exists to top/mid/bottom.
-
Searching:
:setic/noicturns on/off ignore case of the searched string.:/blah\cwill search for "blah" with ignored case./<Up>shows the previously searched item.
-
*/#goes to the next/last occurence of the item under the cursor.- can be prepended by a count.
- Searching for whole words
/thewill include words such as 'the', 'there', etc./\<the\>will include 'the', but not 'there', nor 'atheist'.
- Highlighting matches:
:sethls/nohlsturns on/off highlighting of the searched string.:nohlsdisables the highlighting (until the next search)
- Tuning search:
:setincsearch/noisdisplays the matches as you type the string to be searched/disables incremental search.:set nowrapscanstops going back to the top after the file reaches the end.
- Search patterns:
/^thewill match string 'the' that start out in the beginning of a line.the$will match string 'the' that end out in the beginning of a line./c.mwill match strings such as 'camera', 'community', etc.after\.will match strings 'after.'
- Marks:
''or``moves the cursor backwards and forwards.CTRL+o/ijumps cursor to even earlier/later positions (even across files), without setting marks!:jumpsgives you a list of lines to jump to.
- Named Marks:
m<some_char>marks the spot of the cursor.- if some_char is uppercase, mark becomes global (accessible in all files.)
`<some_char>goes to the mark associated to some character.:marksdisplays all the named marks.:marks MCPshows lines associated to marks 'M', 'C', 'P'.
- Special marks:
'was the previous location before the last jump."was the cursor position before leaving the file.[was the start of the last change.}was the end of the last change..position where you last made the change.
:marksshows list of marks.
-
Configuration:
:mapshows all mappings.- might be a good idea to add mappings for common stuff.
- Wrap word with parenthesis, curly braces, or '[]'
- might be a good idea to add mappings for common stuff.
-
Editing more than one file.
:edit <filename>opens the file in the current buffer.- Vim will close the file and open the new one.
:write/:wwrites the file.:edit! <some_file>discards the changes and edits the new file.:hide edit <some_file>edits some_file, but not write changes in the current file yet.- in the terminal,
vim file-a file-b file-c:next/:nor:previous/:pgo to the next/previous file.:next!/:n!discard changes of current file and go to the next.:wnext/:wpreviousto save current file and go to the next/previous file.
:argsshows you where you are.:args file1 file2 file3redefines files to be opened.<CTRL>-^lets you move quickly from file to file.- Note: Doesn't change the idea of where you are. Only commands like
:next,:previousdo.
- Note: Doesn't change the idea of where you are. Only commands like
- Using registers:
"fyassaves yanked-around-sentence to 'f' register."e3Ysaves 3 lines to 'e' register."fppastes whatever is saved onto the 'f' register.CTRL-V_jjwb"aygo Visual mode, do stuff, save to register 'a'."wdawdelete a word and save to register 'w'.
-
Writing to a file
:write >> logfileappends to file called logfile.SHIFT-Vjj:writeappends to file called logfile, the two selected lines.
-
Viewing a file
vim -R fileedits the file in read-only mode. Can save if:w!vim -M fileopens the file strictly in read-only mode. Unmodifiable.- While in viewer mode:
:set modifiable:set write
-
Changing the file name
:edit file:saveas filerenames the open file to 'file' and saves it.:file filerenames the file to 'file' without saving it.
-
Splitting Windows
- Split a window
:split/:sp:vsplit/:vsp
- Close a window
:close
- Closing all other windows
:only
- Split a window on another file
:split file:newwill open a horiz split on a new file.
- Window Size
:3sp fileopens the file with 3 lines as height.CTRL-W10+/-increases or decreases size by 10.{height}CTRL-W _sets the window height to a specified num of lines.
- Vertical splits
:vsplit:vsplit file:vertical new/:vnew
- Moving between windows
CTRL+ any one ofhjklto move around.CTRL-Wtmoves to the TOP window.CTRL-Wbmoves to the BOTTOM window.CTRL-Ww/Wcycles to the next/previous window.
- Moving windows
CTRL-WHJKL
- Commands for all windows
:qallquits all:qall!quits all (forcefully, if needed).:wallwrites all
- Opening a window for all arguments.
vim -o a b c dwill open filesa,b,c.donto horizontal splits.vim -O a b c dwill open filesa,b,c.donto vertical splits.
- vimdiff
- ...
- various
:leftabove {cmd}left or above the current window:aboveleft {cmd}idem:rightbelow {cmd}right or below the current window:belowright {cmd}idem:topleft {cmd}at the top or left of the Vim window:botright {cmd}at the bottom or right of the Vim window
- Tab pages
:tabedit/:tabefilegt/Tgo to next/previous tab.tab splitmakes a new tab page with one window that is editing the same buffer as the window we were in.:tab help gtopens a newhelp gttab.:tabonlycloses all tab pages except the current one. Unless there are unsaved changes in other tab pages.
- Split a window
-
Making big changes
- Record and playback commands
q{register}starts recording keystrokes.{register}must be in the range of 'a-z' * Type your commands.@{register}to execute the commands.3@l, for example, executes the command(s) associated with the register 'l'.
- Record and playback commands
-