-
Notifications
You must be signed in to change notification settings - Fork 2
Useful VIM Commands Shortcut
TengWeiWen edited this page Apr 24, 2021
·
16 revisions
- vim -p file1.java file2.java ...
- vim -p *.java (Opens all java files in tabs)
- Use gt/gT in vim to move to next/previous tab
- Alternatively, :tabn/:tabq to move to next/previous table
- :tabnew newfile.java (Open new tab of existing or new file)
- :split/:sp file.java (Horizontal window split)
- :vsplit/:vsp file.java (Vertical window split)
- Ctrl-W + arrow keys to change windows
Work sessions is a useful, time-saving feature that an user can use if he is working on a project with many tabs. For example, let's say a user has Cruise.java, BigCruise.java and SmallCruise.java open as multiple tabs in a vim window. By saving the 3 tabs to a work session, the user can reopen all 3 files immediately by calling on the work session
-
:mksession work-session.vim
(this will save all tabs in the window under "work-session")
-
vim -S work-session.vim
(this will open all tabs saved under "work-session")
:mks!
- :w - write/save file
- :w! - force write/save file
- :q - quit/exit file
- :wq - write and quit file (save and close)
- :wqa - write/save all files and quit/exit all files
- :q! - force quit file (force close)
- :sav newFileName - save as "newFileName" file , closes original file and opens "newFileName" file
- :w newFileName - save as "newFileName" file while original file remains open
Can use a number before these movements to repeat the movement that many times (eg. 10w - move to start of next word 10 times)
- Arrow keys/ HJKL - move cursor character by character
- w - move to start of next word
- e - move to end of current word/next word
- b - move to beginning of current word/previous word
- f[character] or F[character] - move to next/previous [character]
- /[regex or text] search for text (n - next occurrence| N - previous occurrence)
- 0 - move to start of line
- $ - move to end of line
- gg - move to start of file
- G - move to end of file (gg=G - fix indentation of whole file)
- :line# - move to line number
Any form of deleting also copies the deleted text so be careful when you have a large amount of text copied
- d[movement command] - delete by movement (eg. dd - delete line | d$ or D - delete to rest of line | d0 - delete from start of line | diw - delete word)
- r[character] - replace with [character] under cursor
- p - paste copied text under cursor
- yy - copy line
- u - undo action
- Ctrl-R - redo action
- :s/[Old_String]/[New_String] - Replace first instance (in line under cursor) of [Old_String] with [New_String].
- :s/[Old_String]/[New_String]/g - Replace all instances (in line under cursor) of [Old_String] with [New_String].
- :%s/[Old_String]/[New_String] - Replace first instance (in every line of the file) of [Old_String] with [New_String].
- :%s/[Old_String]/[New_String]/g - Replace all instances (in the file) of [Old_String] with [New_String].
- i - enter insert mode at cursor
- Shift-I - enter insert mode at start
- a - enter insert mode after cursor (useful when at end of line in normal mode since cursor doesn't go past last character in normal mode)
- o - add line below and enter insert mode
- O - add line above and enter insert mode
- s - delete character under cursor and enter insert mode
- S - delete line and enter insert mode
- ciw - delete word and enter insert mode
- Right-click - paste from clipboard
- v - enter visual mode (highlight character by character)
- Shift-V - enter visual-line mode (highlight row by row)
- Ctrl-V - enter visual-block mode (highlight column by column)
- d - delete highlighted
- y - copy highlighted
Typing anything instead of // will add that to the front of all highlighted lines
- Enter visual-block mode (Ctrl-V)
- Press down/J for however many more lines to be commented
- Shift-I into insert mode and type // (only first line will be commented)
- Press Esc
- Enter visual-block mode
- Press down/J for however many more lines to be indented
- Press > to indent right and < to indent left once (highlight will disappear)
- Press . to continue indenting in that direction
- u to undo