Bram Moolenaar | b4b6a56 | 2007-05-12 13:00:40 +0000 | [diff] [blame] | 1 | *usr_22.txt* For Vim version 7.1. Last change: 2006 Apr 24 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2 | |
| 3 | VIM USER MANUAL - by Bram Moolenaar |
| 4 | |
| 5 | Finding the file to edit |
| 6 | |
| 7 | |
| 8 | Files can be found everywhere. So how do you find them? Vim offers various |
| 9 | ways to browse the directory tree. There are commands to jump to a file that |
| 10 | is mentioned in another. And Vim remembers which files have been edited |
| 11 | before. |
| 12 | |
| 13 | |22.1| The file explorer |
| 14 | |22.2| The current directory |
| 15 | |22.3| Finding a file |
| 16 | |22.4| The buffer list |
| 17 | |
| 18 | Next chapter: |usr_23.txt| Editing other files |
| 19 | Previous chapter: |usr_21.txt| Go away and come back |
| 20 | Table of contents: |usr_toc.txt| |
| 21 | |
| 22 | ============================================================================== |
| 23 | *22.1* The file explorer |
| 24 | |
| 25 | Vim has a plugin that makes it possible to edit a directory. Try this: > |
| 26 | |
| 27 | :edit . |
| 28 | |
| 29 | Through the magic of autocommands and Vim scripts, the window will be filled |
| 30 | with the contents of the directory. It looks like this: |
| 31 | |
| 32 | " Press ? for keyboard shortcuts ~ |
| 33 | " Sorted by name (.bak,~,.o,.h,.info,.swp,.obj,.orig,.rej at end of list) ~ |
| 34 | "= /home/mool/vim/vim6/runtime/doc/ ~ |
| 35 | ../ ~ |
| 36 | check/ ~ |
| 37 | Makefile ~ |
| 38 | autocmd.txt ~ |
| 39 | change.txt ~ |
| 40 | eval.txt~ ~ |
| 41 | filetype.txt~ ~ |
| 42 | help.txt.info ~ |
| 43 | |
| 44 | You can see these items: |
| 45 | 1. A comment about using ? to get help for the functionality of the file |
| 46 | explorer. |
| 47 | 2. The second line mentions how the items in the directory are listed. They |
| 48 | can be sorted in several ways. |
| 49 | 3. The third line is the name of the current directory. |
| 50 | 4. The "../" directory item. This is the parent directory. |
| 51 | 5. The directory names. |
| 52 | 6. The ordinary file names. As mentioned in the second line, some are not |
| 53 | here but "at the end of the list". |
| 54 | 7. The less ordinary file names. You are expected to use these less often, |
| 55 | therefore they have been moved to the end. |
| 56 | |
| 57 | If you have syntax highlighting enabled, the different parts are highlighted |
| 58 | to make it easier to spot them. |
| 59 | |
| 60 | You can use Normal mode Vim commands to move around in the text. For example, |
| 61 | move to a file and press <Enter>. Now you are editing that file. To go back |
| 62 | to the explorer use ":edit ." again. CTRL-O also works. |
| 63 | Try using <Enter> while the cursor is on a directory name. The result is |
| 64 | that the explorer moves into that directory and displays the items found |
| 65 | there. Pressing <Enter> on the first directory "../" moves you one level |
| 66 | higher. Pressing "-" does the same thing, without the need to move to the |
| 67 | "../" item first. |
| 68 | |
| 69 | You can press ? to get short help on the things you can do in the explorer. |
| 70 | This is what you get: |
| 71 | |
| 72 | " <enter> : open file or directory ~ |
| 73 | " o : open new window for file/directory ~ |
| 74 | " O : open file in previously visited window ~ |
| 75 | " p : preview the file ~ |
| 76 | " i : toggle size/date listing ~ |
| 77 | " s : select sort field r : reverse sort ~ |
| 78 | " - : go up one level c : cd to this dir ~ |
| 79 | " R : rename file D : delete file ~ |
| 80 | " :help file-explorer for detailed help ~ |
| 81 | |
| 82 | The first few commands are for selecting a file to display. Depending on what |
| 83 | command you use, the file appears somewhere: |
| 84 | |
| 85 | <Enter> Uses the current window. |
| 86 | o Opens a new window. |
| 87 | O Uses the previously visited window. |
| 88 | p Uses the preview window, and moves the cursor back |
| 89 | into the explorer window. |preview-window| |
| 90 | |
| 91 | The following commands are used to display other information: |
| 92 | |
| 93 | i Display the size and date for the file. Using i again |
| 94 | will hide the information. |
| 95 | s Use the field the cursor is in to sort on. First |
| 96 | display the size and date with i. Then Move the |
| 97 | cursor to the size of any file and press s. The files |
Bram Moolenaar | 69a7cb4 | 2004-06-20 12:51:53 +0000 | [diff] [blame] | 98 | will now be sorted on size. Press s while the cursor |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 99 | is on a date and the items will be sorted on date. |
| 100 | r reverse the sorting order (either size or date) |
| 101 | |
| 102 | There are a few extra commands: |
| 103 | |
| 104 | c Change the current directory to the displayed |
| 105 | directory. You can then type an ":edit" command for |
| 106 | one of the files without prepending the path. |
| 107 | R Rename the file under the cursor. You will be |
| 108 | prompted for the new name. |
| 109 | D Delete the file under the cursor. You will get a |
| 110 | prompt to confirm this. |
| 111 | |
| 112 | ============================================================================== |
| 113 | *22.2* The current directory |
| 114 | |
| 115 | Just like the shell, Vim has the concept of a current directory. Suppose you |
| 116 | are in your home directory and want to edit several files in a directory |
| 117 | "VeryLongFileName". You could do: > |
| 118 | |
| 119 | :edit VeryLongFileName/file1.txt |
| 120 | :edit VeryLongFileName/file2.txt |
| 121 | :edit VeryLongFileName/file3.txt |
| 122 | |
| 123 | To avoid much of the typing, do this: > |
| 124 | |
| 125 | :cd VeryLongFileName |
| 126 | :edit file1.txt |
| 127 | :edit file2.txt |
| 128 | :edit file3.txt |
| 129 | |
| 130 | The ":cd" command changes the current directory. You can see what the current |
| 131 | directory is with the ":pwd" command: > |
| 132 | |
| 133 | :pwd |
| 134 | /home/Bram/VeryLongFileName |
| 135 | |
| 136 | Vim remembers the last directory that you used. Use "cd -" to go back to it. |
| 137 | Example: > |
| 138 | |
| 139 | :pwd |
| 140 | /home/Bram/VeryLongFileName |
| 141 | :cd /etc |
| 142 | :pwd |
| 143 | /etc |
| 144 | :cd - |
| 145 | :pwd |
| 146 | /home/Bram/VeryLongFileName |
| 147 | :cd - |
| 148 | :pwd |
| 149 | /etc |
| 150 | |
| 151 | |
| 152 | WINDOW LOCAL DIRECTORY |
| 153 | |
| 154 | When you split a window, both windows use the same current directory. When |
| 155 | you want to edit a number of files somewhere else in the new window, you can |
| 156 | make it use a different directory, without changing the current directory in |
| 157 | the other window. This is called a local directory. > |
| 158 | |
| 159 | :pwd |
| 160 | /home/Bram/VeryLongFileName |
| 161 | :split |
| 162 | :lcd /etc |
| 163 | :pwd |
| 164 | /etc |
| 165 | CTRL-W w |
| 166 | :pwd |
| 167 | /home/Bram/VeryLongFileName |
| 168 | |
| 169 | So long as no ":lcd" command has been used, all windows share the same current |
| 170 | directory. Doing a ":cd" command in one window will also change the current |
| 171 | directory of the other window. |
| 172 | For a window where ":lcd" has been used a different current directory is |
| 173 | remembered. Using ":cd" or ":lcd" in other windows will not change it. |
| 174 | When using a ":cd" command in a window that uses a different current |
| 175 | directory, it will go back to using the shared directory. |
| 176 | |
| 177 | ============================================================================== |
| 178 | *22.3* Finding a file |
| 179 | |
| 180 | You are editing a C program that contains this line: |
| 181 | |
| 182 | #include "inits.h" ~ |
| 183 | |
| 184 | You want to see what is in that "inits.h" file. Move the cursor on the name |
| 185 | of the file and type: > |
| 186 | |
| 187 | gf |
| 188 | |
| 189 | Vim will find the file and edit it. |
| 190 | What if the file is not in the current directory? Vim will use the 'path' |
| 191 | option to find the file. This option is a list of directory names where to |
| 192 | look for your file. |
| 193 | Suppose you have your include files located in "c:/prog/include". This |
| 194 | command will add it to the 'path' option: > |
| 195 | |
| 196 | :set path+=c:/prog/include |
| 197 | |
| 198 | This directory is an absolute path. No matter where you are, it will be the |
| 199 | same place. What if you have located files in a subdirectory, below where the |
| 200 | file is? Then you can specify a relative path name. This starts with a dot: |
| 201 | > |
| 202 | :set path+=./proto |
| 203 | |
| 204 | This tells Vim to look in the directory "proto", below the directory where the |
| 205 | file in which you use "gf" is. Thus using "gf" on "inits.h" will make Vim |
| 206 | look for "proto/inits.h", starting in the directory of the file. |
| 207 | Without the "./", thus "proto", Vim would look in the "proto" directory |
| 208 | below the current directory. And the current directory might not be where the |
| 209 | file that you are editing is located. |
| 210 | |
| 211 | The 'path' option allows specifying the directories where to search for files |
| 212 | in many more ways. See the help on the 'path' option. |
| 213 | The 'isfname' option is used to decide which characters are included in the |
| 214 | file name, and which ones are not (e.g., the " character in the example |
| 215 | above). |
| 216 | |
| 217 | When you know the file name, but it's not to be found in the file, you can |
| 218 | type it: > |
| 219 | |
| 220 | :find inits.h |
| 221 | |
| 222 | Vim will then use the 'path' option to try and locate the file. This is the |
| 223 | same as the ":edit" command, except for the use of 'path'. |
| 224 | |
| 225 | To open the found file in a new window use CTRL-W f instead of "gf", or use |
| 226 | ":sfind" instead of ":find". |
| 227 | |
| 228 | |
| 229 | A nice way to directly start Vim to edit a file somewhere in the 'path': > |
| 230 | |
| 231 | vim "+find stdio.h" |
| 232 | |
| 233 | This finds the file "stdio.h" in your value of 'path'. The quotes are |
| 234 | necessary to have one argument |-+c|. |
| 235 | |
| 236 | ============================================================================== |
| 237 | *22.4* The buffer list |
| 238 | |
| 239 | The Vim editor uses the term buffer to describe a file being edited. |
| 240 | Actually, a buffer is a copy of the file that you edit. When you finish |
| 241 | changing the buffer, you write the contents of the buffer to the file. |
| 242 | Buffers not only contain file contents, but also all the marks, settings, and |
| 243 | other stuff that goes with it. |
| 244 | |
| 245 | |
| 246 | HIDDEN BUFFERS |
| 247 | |
| 248 | Suppose you are editing the file one.txt and need to edit the file two.txt. |
| 249 | You could simply use ":edit two.txt", but since you made changes to one.txt |
| 250 | that won't work. You also don't want to write one.txt yet. Vim has a |
| 251 | solution for you: > |
| 252 | |
| 253 | :hide edit two.txt |
| 254 | |
| 255 | The buffer "one.txt" disappears from the screen, but Vim still knows that you |
| 256 | are editing this buffer, so it keeps the modified text. This is called a |
| 257 | hidden buffer: The buffer contains text, but you can't see it. |
| 258 | The ":hide" command argument is another command. It makes that command |
| 259 | behave like the 'hidden' option was set. You could also set this option |
| 260 | yourself. The effect is that when any buffer is abandoned, it becomes hidden. |
| 261 | Be careful! When you have hidden buffers with changes, don't exit Vim |
| 262 | without making sure you have saved all the buffers. |
| 263 | |
| 264 | |
| 265 | INACTIVE BUFFERS |
| 266 | |
| 267 | When a buffer has been used once, Vim remembers some information about it. |
| 268 | When it is not displayed in a window and it is not hidden, it is still in the |
| 269 | buffer list. This is called an inactive buffer. Overview: |
| 270 | |
| 271 | Active Appears in a window, text loaded. |
| 272 | Hidden Not in a window, text loaded. |
| 273 | Inactive Not in a window, no text loaded. |
| 274 | |
| 275 | The inactive buffers are remembered, because Vim keeps information about them, |
| 276 | like marks. And remembering the file name is useful too, so that you can see |
| 277 | which files you have edited. And edit them again. |
| 278 | |
| 279 | |
| 280 | LISTING BUFFERS |
| 281 | |
| 282 | View the buffer list with this command: > |
| 283 | |
| 284 | :buffers |
| 285 | |
| 286 | A command which does the same, is not so obvious to list buffers, but is much |
| 287 | shorter to type: > |
| 288 | |
| 289 | :ls |
| 290 | |
| 291 | The output could look like this: |
| 292 | |
| 293 | 1 #h "help.txt" line 62 ~ |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 294 | 2 %a+ "usr_21.txt" line 1 ~ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 295 | 3 "usr_toc.txt" line 1 ~ |
| 296 | |
| 297 | The first column contains the buffer number. You can use this to edit the |
| 298 | buffer without having to type the name, see below. |
| 299 | After the buffer number come the flags. Then the name of the file |
| 300 | and the line number where the cursor was the last time. |
| 301 | The flags that can appear are these (from left to right): |
| 302 | |
| 303 | u Buffer is unlisted |unlisted-buffer|. |
| 304 | % Current buffer. |
| 305 | # Alternate buffer. |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 306 | a Buffer is loaded and displayed. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 307 | h Buffer is loaded but hidden. |
| 308 | = Buffer is read-only. |
| 309 | - Buffer is not modifiable, the 'modifiable' option is off. |
| 310 | + Buffer has been modified. |
| 311 | |
| 312 | |
| 313 | EDITING A BUFFER |
| 314 | |
| 315 | You can edit a buffer by its number. That avoids having to type the file |
| 316 | name: > |
| 317 | |
| 318 | :buffer 2 |
| 319 | |
| 320 | But the only way to know the number is by looking in the buffer list. You can |
| 321 | use the name, or part of it, instead: > |
| 322 | |
| 323 | :buffer help |
| 324 | |
| 325 | Vim will find a best match for the name you type. If there is only one |
| 326 | buffer that matches the name, it will be used. In this case "help.txt". |
| 327 | To open a buffer in a new window: > |
| 328 | |
| 329 | :sbuffer 3 |
| 330 | |
| 331 | This works with a name as well. |
| 332 | |
| 333 | |
| 334 | USING THE BUFFER LIST |
| 335 | |
| 336 | You can move around in the buffer list with these commands: |
| 337 | |
| 338 | :bnext go to next buffer |
| 339 | :bprevious go to previous buffer |
| 340 | :bfirst go to the first buffer |
| 341 | :blast go to the last buffer |
| 342 | |
| 343 | To remove a buffer from the list, use this command: > |
| 344 | |
| 345 | :bdelete 3 |
| 346 | |
| 347 | Again, this also works with a name. |
| 348 | If you delete a buffer that was active (visible in a window), that window |
| 349 | will be closed. If you delete the current buffer, the current window will be |
| 350 | closed. If it was the last window, Vim will find another buffer to edit. You |
| 351 | can't be editing nothing! |
| 352 | |
| 353 | Note: |
| 354 | Even after removing the buffer with ":bdelete" Vim still remembers it. |
| 355 | It's actually made "unlisted", it no longer appears in the list from |
| 356 | ":buffers". The ":buffers!" command will list unlisted buffers (yes, |
| 357 | Vim can do the impossible). To really make Vim forget about a buffer, |
| 358 | use ":bwipe". Also see the 'buflisted' option. |
| 359 | |
| 360 | ============================================================================== |
| 361 | |
| 362 | Next chapter: |usr_23.txt| Editing other files |
| 363 | |
| 364 | Copyright: see |manual-copyright| vim:tw=78:ts=8:ft=help:norl: |