| 
									
										
										
										
											2018-10-12 15:37:13 -04:00
										 |  |  | --- | 
					
						
							|  |  |  | title: Motions | 
					
						
							|  |  |  | --- | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Vim Basic Motions
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ### Cursors motions
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | First in VIM we can use the arrow keys if we like to move around the text file, but it is not the best way to do so, | 
					
						
							| 
									
										
										
										
											2018-11-17 05:15:25 -05:00
										 |  |  | and we lost the advantage of combined commands that VIM provides. | 
					
						
							| 
									
										
										
										
											2018-10-12 15:37:13 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-17 05:15:25 -05:00
										 |  |  | Instead of that, the default and best way to move through the text is using the keys `h`(left), `j`(down), `k`(up) and `l`(right). | 
					
						
							| 
									
										
										
										
											2018-10-12 15:37:13 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  |      ^  | 
					
						
							|  |  |  |      | | 
					
						
							|  |  |  |      k | 
					
						
							|  |  |  | <- h   l -> | 
					
						
							|  |  |  |      j | 
					
						
							|  |  |  |      | | 
					
						
							|  |  |  |      v | 
					
						
							|  |  |  | ``` | 
					
						
							| 
									
										
										
										
											2018-11-17 05:15:25 -05:00
										 |  |  | The advantage in using these keys instead of the arrow keys is you can combine motions with other commands, like: | 
					
						
							| 
									
										
										
										
											2018-10-12 15:37:13 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   - `d2j` -> delete 2 lines down | 
					
						
							|  |  |  |   - `y10k` -> copy 10 lines up | 
					
						
							| 
									
										
										
										
											2018-11-17 05:15:25 -05:00
										 |  |  |   - `10l` -> move 10 characters right | 
					
						
							| 
									
										
										
										
											2018-10-12 15:37:13 -04:00
										 |  |  |   - `2h`  -> move 2 characters left | 
					
						
							|  |  |  |   | 
					
						
							|  |  |  | ### Words Motions
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | A word consists of a sequence of letters, digits and underscores, or a | 
					
						
							|  |  |  | sequence of other non-blank characters, separated with white space (spaces, | 
					
						
							| 
									
										
										
										
											2018-11-17 05:15:25 -05:00
										 |  |  | tabs, end of line). An empty line is also considered to be a word. | 
					
						
							| 
									
										
										
										
											2018-10-12 15:37:13 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-17 05:15:25 -05:00
										 |  |  | It is possible move through words with these commands: | 
					
						
							| 
									
										
										
										
											2018-10-12 15:37:13 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |   - `w` -> move to next word | 
					
						
							|  |  |  |   - `W` -> move to next **WORD***  | 
					
						
							|  |  |  |   - `e` -> move to the end of the next word | 
					
						
							|  |  |  |   - `E` -> move to the end of the next **WORD** | 
					
						
							|  |  |  |   - `b` -> move to previous word  | 
					
						
							|  |  |  |   - `B` -> move to previous **WORD** | 
					
						
							|  |  |  |   - `ge` -> move to the end of previous word | 
					
						
							|  |  |  |   - `gE` -> move to the end of previous **WORD** | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  | *A **WORD** consists of a sequence of non-blank characters, separated with white | 
					
						
							| 
									
										
										
										
											2018-11-17 05:15:25 -05:00
										 |  |  | space. An empty line is also considered to be a **WORD**, ie: `quux(foo,` `bar,` `foo);` | 
					
						
							| 
									
										
										
										
											2018-10-12 15:37:13 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-17 05:15:25 -05:00
										 |  |  | When these motions are combined with other commands you can do things like: | 
					
						
							| 
									
										
										
										
											2018-10-12 15:37:13 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | - `dw` -> delete the word | 
					
						
							|  |  |  | - `5e` -> move to the end of the 5th word from here | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ### Search motions
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-17 05:15:25 -05:00
										 |  |  | Another way to move to the position you like is using search motions. The search motions consist of the  | 
					
						
							| 
									
										
										
										
											2018-10-12 15:37:13 -04:00
										 |  |  | motion + a character to search | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   - `fx` -> move to the next `x` | 
					
						
							|  |  |  |   - `tx` -> move to first position before the next `x` | 
					
						
							|  |  |  |   - `;` -> next x | 
					
						
							|  |  |  |   - `Fx` -> move to the previous `x` | 
					
						
							|  |  |  |   - `Tx` -> move to first position after the previous `x` | 
					
						
							|  |  |  |   - `,` -> previous x | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  | ### Begin and End of lines
 | 
					
						
							|  |  |  |    | 
					
						
							| 
									
										
										
										
											2018-11-17 05:15:25 -05:00
										 |  |  |   You can also move to beginning or end of line with VIM, with these commands: | 
					
						
							| 
									
										
										
										
											2018-10-12 15:37:13 -04:00
										 |  |  |    | 
					
						
							| 
									
										
										
										
											2018-11-17 05:15:25 -05:00
										 |  |  |    - `0` -> Beginning of the line | 
					
						
							| 
									
										
										
										
											2018-11-03 23:54:47 +08:00
										 |  |  |    - `^` -> First non-blank character of the line | 
					
						
							| 
									
										
										
										
											2018-10-12 15:37:13 -04:00
										 |  |  |    - `$` -> End of the line | 
					
						
							|  |  |  |     | 
					
						
							|  |  |  | ### File motions
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-17 05:15:25 -05:00
										 |  |  | In VIM you can move through the file using these commands: | 
					
						
							| 
									
										
										
										
											2018-10-12 15:37:13 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |  - `gg` -> move to the first line of the file | 
					
						
							|  |  |  |  - `G` -> move to the last line of the file | 
					
						
							|  |  |  |  - `<ctrl> + f` -> move one page down | 
					
						
							|  |  |  |  - `<ctrl> + b` -> move one page up | 
					
						
							|  |  |  |  - `/text` -> find `text` | 
					
						
							|  |  |  |  - `n` -> move to the next occurrence of the `text` (previous command) | 
					
						
							|  |  |  |  - `?text` -> find previous `text`  | 
					
						
							|  |  |  |  - `N` -> move to the previous occurrence of `text` | 
					
						
							|  |  |  |  - `{` -> move one paragraph up | 
					
						
							|  |  |  |  - `}` -> move one paragraph down | 
					
						
							|  |  |  |  - `(` -> move one sentence up | 
					
						
							| 
									
										
										
										
											2018-11-17 05:15:25 -05:00
										 |  |  |  - `)` -> move one sentence down | 
					
						
							| 
									
										
										
										
											2018-10-12 15:37:13 -04:00
										 |  |  |  - `#` -> find word under cursor up | 
					
						
							|  |  |  |  - `*` -> find word under cursor down | 
					
						
							|  |  |  |   | 
					
						
							|  |  |  |   | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  | 
 |