├── .vimrc └── README.md /.vimrc: -------------------------------------------------------------------------------- 1 | syntax on 2 | set number 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Vim Basics 2 | 3 | > Just the basics to get you up and running with [Vim](https://www.vim.org) 4 | 5 | - [Quick start](#quick-start) 6 | - [Commands](#commands) 7 | - [Settings](#settings) 8 | - [The `` + `z` “problem”](#the-ctrl--z-problem) 9 | - [See also](#see-also) 10 | 11 | This guide assumes some familiarity with the terminal. (See [Unix Basics](https://github.com/yuanqing/shell-basics/blob/master/README.md#readme) for a quick overview.) 12 | 13 | ## Quick start 14 | 15 | 1. Launch Vim via the terminal: 16 | 17 | ```sh 18 | $ vim Main.java 19 | ``` 20 | 21 | You are now in Vim’s ***Command*** mode. 22 | 23 | (You should see the contents of `Main.java` if the file already exists. Otherwise you will see an empty text editor window.) 24 | 25 | 2. You cannot type into or edit your file while in ***Command*** mode. To start typing into the file, you must switch to Vim’s ***Insert*** mode. Press `i` to do so. You should see -- INSERT -- on the bottom-left hand corner of your window. Now you can type as you would with any other text editor. 26 | 27 | 3. When you’re done typing, press `` to go back to ***Command*** mode. (The `` key is your friend! Hitting `` will bring you back to ***Command*** mode.) 28 | 29 | 4. And… that’s about it, really! Now type `:w` and `` to save the file, followed by `:q!` and `` to quit Vim. 30 | 31 | ## Commands 32 | 33 | Almost all operations in Vim occur while in ***Command*** mode. Listed here are the more important commands that you should know. 34 | 35 | ### Switching between ***Command*** and ***Insert*** mode 36 | 37 | Action | Keys 38 | :--|:-- 39 | Switch to ***Command*** mode | `` 40 | Switch to ***Insert*** mode | `i` 41 | Switch to ***Insert*** mode, moving the cursor to the end of the current line | `A` 42 | Switch to ***Insert*** mode, adding a new line under the current line, and moving the cursor to the new line | `o` 43 | 44 | ### Moving the cursor around the file 45 | 46 | Action | Keys 47 | :--|:-- 48 | Page up | `` + `u` 49 | Page down | `` + `d` 50 | Move the cursor to the next word | `W` 51 | Move the cursor to the previous word | `b` 52 | Move the cursor to the start of the current line | `0` 53 | Move the cursor to the end of the current line | `$` 54 | Move the cursor to a particular line of the file (eg. line number 9) | `9G` 55 | 56 | Of course, you can also move the cursor using any of the arrow keys (, , , ), but it will be a lot faster to use the above commands. 57 | 58 | ### Delete/cut 59 | 60 | Action | Keys 61 | :--|:-- 62 | Delete the line under the cursor | `dd` 63 | Delete the word under the cursor | `dw` 64 | Delete the character under the cursor | `x` 65 | 66 | Because whatever you delete is copied into Vim’s clipboard, these 3 commands are akin to performing a cut. 67 | 68 | ### Copy and paste 69 | 70 | Action | Keys 71 | :--|:-- 72 | Copy the current line | `yy` 73 | Copy a particular number of lines, starting from the current line (eg. 3 lines) | `3yy` 74 | Paste | `p` 75 | 76 | You can also specify the text to be copied using a text selection. It is a bit more involved, though: 77 | 78 | 1. While in ***Command*** mode, press `v` to switch to ***Visual*** mode. You should see -- VISUAL -- on the bottom-left hand corner of your window. 79 | 2. [Move the cursor](#moving-the-cursor-around-the-file) to adjust the text selection. 80 | 3. When you have selected the text that you want to copy, press `y`. This will bring you back to ***Command*** mode, and the selected text will have been copied into Vim’s clipboard. 81 | 82 | ### Undo and redo 83 | 84 | Action | Keys 85 | :--|:-- 86 | Undo | `u` 87 | Redo | `` + `r` 88 | 89 | ### Fixing code indentation 90 | 91 | Action | Keys 92 | :--|:-- 93 | Fix the code indentation of the file | `gg=G` 94 | 95 | ### Save and quit 96 | 97 | Action | Keys 98 | :--|:-- 99 | Save the file | `:w` then `` 100 | Save the file then quit | `:wq` then `` 101 | Quit without saving the file | `:q!` then `` 102 | 103 | ## Settings 104 | 105 | These are some of the commands to customise the display: 106 | 107 | Action | Keys 108 | :--|:-- 109 | Enable syntax highlighting | `:syntax on` then `` 110 | Enable line numbers | `:set number` then `` 111 | 112 | Vim will look for a settings file named `.vimrc` in your home directory. You can avoid having to type the above settings for every new Vim session by putting the following in your `~/.vimrc`: 113 | 114 | ```viml 115 | syntax on 116 | set number 117 | ``` 118 | 119 | ## The `` + `z` “problem” 120 | 121 | Remember that the command for undo is simply `u`, *not* `Ctrl` + `z`! 122 | 123 | If you’d accidentally pressed `Ctrl` + `z`, you will find yourself back in your terminal, where you will see something like the following: 124 | 125 | ```sh 126 | [1]+ Stopped vim Main.java 127 | ``` 128 | 129 | A quick fix is to issue the `fg` command: 130 | 131 | ```sh 132 | $ fg 133 | ``` 134 | 135 | This will bring us back to Vim, and all is well with the world. 136 | 137 | (Explanation: Pressing `` + `z` [places the currently-running program in the background](http://en.wikibooks.org/wiki/A_Quick_Introduction_to_Unix/Job_Control#Controlling_Jobs_in_Unix). Here, the currently-running program is Vim. The `fg` program simply brings the most recent “backgrounded” program back to the foreground.) 138 | 139 | ## See also 140 | 141 | - [Shell Basics](https://github.com/yuanqing/shell-basics/blob/master/README.md#readme) 142 | - [Vim Commands Cheatsheet](http://www.fprintf.net/vimCheatSheet.html) 143 | --------------------------------------------------------------------------------