Difference between revisions of "Vim keys"
m (→Modes) |
|||
| (30 intermediate revisions by the same user not shown) | |||
| Line 13: | Line 13: | ||
|- | |- | ||
|Insert|| ||A|| ||This will enter Insert mode and move the cursor to the end of the current line. | |Insert|| ||A|| ||This will enter Insert mode and move the cursor to the end of the current line. | ||
| + | |- | ||
| + | |Insert|| ||c<motion>|| ||This will delete in the same way as the delete operator and then enter Insert mode. This is known as the change operator. | ||
|} | |} | ||
| Line 26: | Line 28: | ||
|style="background-color:#ffffcc;"|Character|| ||l or →|| ||Move one character backward|| ||h or ←|| ||Move one character forward | |style="background-color:#ffffcc;"|Character|| ||l or →|| ||Move one character backward|| ||h or ←|| ||Move one character forward | ||
|- | |- | ||
| − | |style="background-color:#ffffcc;"|Word|| ||w|| ||Move | + | |style="background-color:#ffffcc;"|Word|| ||w|| ||Move to start of next word|| ||b|| ||Move one word backward |
| + | |- | ||
| + | |style="background-color:#ffffcc;"|Word|| ||e|| ||Move to end of current word|| || || || | ||
|- | |- | ||
|style="background-color:#ffffcc;"|Line|| ||j or ↓|| ||Move to next line|| ||k or ↑|| ||Move to previous line | |style="background-color:#ffffcc;"|Line|| ||j or ↓|| ||Move to next line|| ||k or ↑|| ||Move to previous line | ||
| Line 35: | Line 39: | ||
|- | |- | ||
|style="background-color:#ffffcc;"|Document|| ||G|| ||Move to the end of the document|| ||gg|| ||Move to the start of the document | |style="background-color:#ffffcc;"|Document|| ||G|| ||Move to the end of the document|| ||gg|| ||Move to the start of the document | ||
| + | |} | ||
| + | |||
| + | ===Other movement keys=== | ||
| + | |||
| + | {| | ||
| + | !align="left"|Key!! !!align="left"|Function | ||
| + | |- | ||
| + | |%|| ||Jump to corresponding parenthesis | ||
|} | |} | ||
| Line 46: | Line 58: | ||
|- | |- | ||
|x|| ||Delete the character under the cursor | |x|| ||Delete the character under the cursor | ||
| + | |- | ||
| + | |dw|| ||Delete until the start of the next word, ''excluding'' its first character. | ||
| + | |- | ||
| + | |de|| ||Delete to the end of the current word, ''including'' the last character. | ||
| + | |- | ||
| + | |d$|| ||Delete to end of line (not including the newline). When given a count parameter d$ will kill to end of line including the newline for each count. | ||
| + | |- | ||
| + | |D|| ||Alias for d$. | ||
| + | |- | ||
| + | |dd|| ||Delete current line. | ||
| + | |- | ||
| + | |r<character>|| ||Replace the character under the cursor with the following character, i.e. typing <tt>rD</tt> will insert a D instead of the character under the cursor. | ||
| + | |- | ||
| + | |p|| ||Put previously deleted text after the cursor. | ||
| + | |- | ||
| + | |P|| ||Put previously deleted text before the cursor. | ||
| + | |- | ||
| + | |u|| ||Undo last command. | ||
| + | |- | ||
| + | |U|| ||Undo all changes in last modified line. | ||
| + | |- | ||
| + | |Ctrl+R|| ||Redo command. | ||
<!-- | <!-- | ||
|- | |- | ||
| Line 65: | Line 99: | ||
--> | --> | ||
|} | |} | ||
| + | |||
| + | ==Search and Replace== | ||
| + | A. Find all occurrences of old and replace with new in current line only: | ||
| + | :s/old/new/g | ||
| + | The g-flag means global and changes all occurrences in a line, as opposed to only the first occurrence, when the g_flag is omitted. | ||
| + | |||
| + | B. Find all occurrences of old and replace with new in all lines: | ||
| + | :%s/old/new/g | ||
| + | |||
| + | C. Same as B, but ask for confirmation: | ||
| + | :%s/old/new/gc | ||
| + | |||
| + | D. Same as C, but but change only whole words: | ||
| + | :%s/\<old\>/new/gc | ||
| + | |||
| + | E. Same as C, but search case insensitively: | ||
| + | :%s/old/new/gci | ||
| + | :%s/old\c/new/gc | ||
| + | |||
| + | F. Same as C, but search case sensitively: | ||
| + | :%s/old/new/gcI | ||
| + | :%s/old\C/new/gc | ||
| + | |||
| + | For a more in-depth discussion of Search and Replace in vim see [http://vim.wikia.com/wiki/Search_and_replace this page]. | ||
| + | |||
| + | ==Using count== | ||
| + | |||
| + | Typing a number with an operator repeats the operator. I.e. typing <tt>3w</tt> wil move the cursor 3 words forward. When using counts with operators like the delete oprerator, you type the count in between the operator and the motion, like this: <tt>d2w</tt> (to delete 2 words). When using the delete line operator (<tt>dd</tt>) you have to type the count before the operator, like this: <tt>2dd</tt>. | ||
| + | |||
| + | ==File handling, buffers and exiting== | ||
| + | {| | ||
| + | !align="left"|Key!! !!align="left"|Function | ||
| + | <!-- | ||
| + | |- | ||
| + | |C-x C-f|| ||find (open or create) file (prompted for filename) | ||
| + | --> | ||
| + | |- | ||
| + | |:w|| ||save file (in Normal mode) | ||
| + | |- | ||
| + | |:w path/to/file/filename|| ||save file as ... | ||
| + | |- | ||
| + | | || ||save all files | ||
| + | |- | ||
| + | |:wq|| ||save current file and exit vim | ||
| + | <!-- | ||
| + | |- | ||
| + | |C-x k|| ||kill current buffer | ||
| + | --> | ||
| + | |- | ||
| + | | || ||move to or create another buffer (prompt for buffer name) | ||
| + | |- | ||
| + | |:n|| ||Go to next buffer | ||
| + | |- | ||
| + | |:prev|| ||Go to previous buffer | ||
| + | <!-- | ||
| + | |- | ||
| + | |C-x C-b|| ||show buffer list | ||
| + | |- | ||
| + | |C-x i|| ||insert file at cursor position | ||
| + | --> | ||
| + | |} | ||
| + | |||
| + | ==Code folding== | ||
| + | {| | ||
| + | !align="left"|Key!! !!align="left"|Function | ||
| + | |- | ||
| + | |z r|| ||Unfold current fold | ||
| + | |- | ||
| + | |z R|| ||Unfold all folds | ||
| + | |- | ||
| + | |z c|| ||Refold current fold | ||
| + | |} | ||
| + | |||
[[Category:Værktøjer, Programmering]][[Category:Keyboard shortcuts]] | [[Category:Værktøjer, Programmering]][[Category:Keyboard shortcuts]] | ||
Latest revision as of 13:14, 11 January 2019
Ressources to check
- https://coderwall.com/p/adv71w/basic-vim-commands-for-getting-started
- http://vim.wikia.com/wiki/Copy,_cut_and_paste
- vimtutor
Modes
| Mode | Key | Comment | ||
|---|---|---|---|---|
| Normal | <ESC> | Normal is the default mode when vim starts | ||
| Insert | i | This is the normal way of entering Insert mode. | ||
| Insert | A | This will enter Insert mode and move the cursor to the end of the current line. | ||
| Insert | c<motion> | This will delete in the same way as the delete operator and then enter Insert mode. This is known as the change operator. |
Movement (Normal mode)
| Forward | Backward | |||||||
| Unit | Key | Function | Key | Function | ||||
|---|---|---|---|---|---|---|---|---|
| Character | l or → | Move one character backward | h or ← | Move one character forward | ||||
| Word | w | Move to start of next word | b | Move one word backward | ||||
| Word | e | Move to end of current word | ||||||
| Line | j or ↓ | Move to next line | k or ↑ | Move to previous line | ||||
| Line | Move to end of line | 0 (zero) | Move to start of line | |||||
| Sentence | Move forward to end of sentence | Move back to beginning of sentence | ||||||
| Document | G | Move to the end of the document | gg | Move to the start of the document | ||||
Other movement keys
| Key | Function | |
|---|---|---|
| % | Jump to corresponding parenthesis |
Editing (Normal mode)
| Key | Function | |
|---|---|---|
| x | Delete the character under the cursor | |
| dw | Delete until the start of the next word, excluding its first character. | |
| de | Delete to the end of the current word, including the last character. | |
| d$ | Delete to end of line (not including the newline). When given a count parameter d$ will kill to end of line including the newline for each count. | |
| D | Alias for d$. | |
| dd | Delete current line. | |
| r<character> | Replace the character under the cursor with the following character, i.e. typing rD will insert a D instead of the character under the cursor. | |
| p | Put previously deleted text after the cursor. | |
| P | Put previously deleted text before the cursor. | |
| u | Undo last command. | |
| U | Undo all changes in last modified line. | |
| Ctrl+R | Redo command. |
Search and Replace
A. Find all occurrences of old and replace with new in current line only:
:s/old/new/g
The g-flag means global and changes all occurrences in a line, as opposed to only the first occurrence, when the g_flag is omitted.
B. Find all occurrences of old and replace with new in all lines:
:%s/old/new/g
C. Same as B, but ask for confirmation:
:%s/old/new/gc
D. Same as C, but but change only whole words:
:%s/\<old\>/new/gc
E. Same as C, but search case insensitively:
:%s/old/new/gci :%s/old\c/new/gc
F. Same as C, but search case sensitively:
:%s/old/new/gcI :%s/old\C/new/gc
For a more in-depth discussion of Search and Replace in vim see this page.
Using count
Typing a number with an operator repeats the operator. I.e. typing 3w wil move the cursor 3 words forward. When using counts with operators like the delete oprerator, you type the count in between the operator and the motion, like this: d2w (to delete 2 words). When using the delete line operator (dd) you have to type the count before the operator, like this: 2dd.
File handling, buffers and exiting
| Key | Function | |
|---|---|---|
| :w | save file (in Normal mode) | |
| :w path/to/file/filename | save file as ... | |
| save all files | ||
| :wq | save current file and exit vim | |
| move to or create another buffer (prompt for buffer name) | ||
| :n | Go to next buffer | |
| :prev | Go to previous buffer |
Code folding
| Key | Function | |
|---|---|---|
| z r | Unfold current fold | |
| z R | Unfold all folds | |
| z c | Refold current fold |