1*usr_22.txt* For Vim version 7.0aa. Last change: 2004 Jun 16 2 3 VIM USER MANUAL - by Bram Moolenaar 4 5 Finding the file to edit 6 7 8Files can be found everywhere. So how do you find them? Vim offers various 9ways to browse the directory tree. There are commands to jump to a file that 10is mentioned in another. And Vim remembers which files have been edited 11before. 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 20Table of contents: |usr_toc.txt| 21 22============================================================================== 23*22.1* The file explorer 24 25Vim has a plugin that makes it possible to edit a directory. Try this: > 26 27 :edit . 28 29Through the magic of autocommands and Vim scripts, the window will be filled 30with 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 44You can see these items: 451. A comment about using ? to get help for the functionality of the file 46 explorer. 472. The second line mentions how the items in the directory are listed. They 48 can be sorted in several ways. 493. The third line is the name of the current directory. 504. The "../" directory item. This is the parent directory. 515. The directory names. 526. The ordinary file names. As mentioned in the second line, some are not 53 here but "at the end of the list". 547. The less ordinary file names. You are expected to use these less often, 55 therefore they have been moved to the end. 56 57If you have syntax highlighting enabled, the different parts are highlighted 58to make it easier to spot them. 59 60You can use Normal mode Vim commands to move around in the text. For example, 61move to a file and press <Enter>. Now you are editing that file. To go back 62to the explorer use ":edit ." again. CTRL-O also works. 63 Try using <Enter> while the cursor is on a directory name. The result is 64that the explorer moves into that directory and displays the items found 65there. Pressing <Enter> on the first directory "../" moves you one level 66higher. Pressing "-" does the same thing, without the need to move to the 67"../" item first. 68 69You can press ? to get short help on the things you can do in the explorer. 70This 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 82The first few commands are for selecting a file to display. Depending on what 83command 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 91The 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 98 will now be sorted on size. Press s while the cursor 99 is on a date and the items will be sorted on date. 100 r reverse the sorting order (either size or date) 101 102There 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 115Just like the shell, Vim has the concept of a current directory. Suppose you 116are 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 123To avoid much of the typing, do this: > 124 125 :cd VeryLongFileName 126 :edit file1.txt 127 :edit file2.txt 128 :edit file3.txt 129 130The ":cd" command changes the current directory. You can see what the current 131directory is with the ":pwd" command: > 132 133 :pwd 134 /home/Bram/VeryLongFileName 135 136Vim remembers the last directory that you used. Use "cd -" to go back to it. 137Example: > 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 152WINDOW LOCAL DIRECTORY 153 154When you split a window, both windows use the same current directory. When 155you want to edit a number of files somewhere else in the new window, you can 156make it use a different directory, without changing the current directory in 157the 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 169So long as no ":lcd" command has been used, all windows share the same current 170directory. Doing a ":cd" command in one window will also change the current 171directory of the other window. 172 For a window where ":lcd" has been used a different current directory is 173remembered. 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 175directory, it will go back to using the shared directory. 176 177============================================================================== 178*22.3* Finding a file 179 180You are editing a C program that contains this line: 181 182 #include "inits.h" ~ 183 184You want to see what is in that "inits.h" file. Move the cursor on the name 185of the file and type: > 186 187 gf 188 189Vim will find the file and edit it. 190 What if the file is not in the current directory? Vim will use the 'path' 191option to find the file. This option is a list of directory names where to 192look for your file. 193 Suppose you have your include files located in "c:/prog/include". This 194command will add it to the 'path' option: > 195 196 :set path+=c:/prog/include 197 198This directory is an absolute path. No matter where you are, it will be the 199same place. What if you have located files in a subdirectory, below where the 200file is? Then you can specify a relative path name. This starts with a dot: 201> 202 :set path+=./proto 203 204This tells Vim to look in the directory "proto", below the directory where the 205file in which you use "gf" is. Thus using "gf" on "inits.h" will make Vim 206look for "proto/inits.h", starting in the directory of the file. 207 Without the "./", thus "proto", Vim would look in the "proto" directory 208below the current directory. And the current directory might not be where the 209file that you are editing is located. 210 211The 'path' option allows specifying the directories where to search for files 212in many more ways. See the help on the 'path' option. 213 The 'isfname' option is used to decide which characters are included in the 214file name, and which ones are not (e.g., the " character in the example 215above). 216 217When you know the file name, but it's not to be found in the file, you can 218type it: > 219 220 :find inits.h 221 222Vim will then use the 'path' option to try and locate the file. This is the 223same as the ":edit" command, except for the use of 'path'. 224 225To open the found file in a new window use CTRL-W f instead of "gf", or use 226":sfind" instead of ":find". 227 228 229A nice way to directly start Vim to edit a file somewhere in the 'path': > 230 231 vim "+find stdio.h" 232 233This finds the file "stdio.h" in your value of 'path'. The quotes are 234necessary to have one argument |-+c|. 235 236============================================================================== 237*22.4* The buffer list 238 239The Vim editor uses the term buffer to describe a file being edited. 240Actually, a buffer is a copy of the file that you edit. When you finish 241changing the buffer, you write the contents of the buffer to the file. 242Buffers not only contain file contents, but also all the marks, settings, and 243other stuff that goes with it. 244 245 246HIDDEN BUFFERS 247 248Suppose you are editing the file one.txt and need to edit the file two.txt. 249You could simply use ":edit two.txt", but since you made changes to one.txt 250that won't work. You also don't want to write one.txt yet. Vim has a 251solution for you: > 252 253 :hide edit two.txt 254 255The buffer "one.txt" disappears from the screen, but Vim still knows that you 256are editing this buffer, so it keeps the modified text. This is called a 257hidden buffer: The buffer contains text, but you can't see it. 258 The ":hide" command argument is another command. It makes that command 259behave like the 'hidden' option was set. You could also set this option 260yourself. 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 262without making sure you have saved all the buffers. 263 264 265INACTIVE BUFFERS 266 267 When a buffer has been used once, Vim remembers some information about it. 268When it is not displayed in a window and it is not hidden, it is still in the 269buffer 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 275The inactive buffers are remembered, because Vim keeps information about them, 276like marks. And remembering the file name is useful too, so that you can see 277which files you have edited. And edit them again. 278 279 280LISTING BUFFERS 281 282View the buffer list with this command: > 283 284 :buffers 285 286A command which does the same, is not so obvious to list buffers, but is much 287shorter to type: > 288 289 :ls 290 291The output could look like this: 292 293 1 #h "help.txt" line 62 ~ 294 2 %l+ "usr_21.txt" line 1 ~ 295 3 "usr_toc.txt" line 1 ~ 296 297The first column contains the buffer number. You can use this to edit the 298buffer without having to type the name, see below. 299 After the buffer number come the flags. Then the name of the file 300and 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. 306 l Buffer is loaded and displayed. 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 313EDITING A BUFFER 314 315You can edit a buffer by its number. That avoids having to type the file 316name: > 317 318 :buffer 2 319 320But the only way to know the number is by looking in the buffer list. You can 321use the name, or part of it, instead: > 322 323 :buffer help 324 325Vim will find a best match for the name you type. If there is only one 326buffer 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 331This works with a name as well. 332 333 334USING THE BUFFER LIST 335 336You 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 343To remove a buffer from the list, use this command: > 344 345 :bdelete 3 346 347Again, this also works with a name. 348 If you delete a buffer that was active (visible in a window), that window 349will be closed. If you delete the current buffer, the current window will be 350closed. If it was the last window, Vim will find another buffer to edit. You 351can'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 362Next chapter: |usr_23.txt| Editing other files 363 364Copyright: see |manual-copyright| vim:tw=78:ts=8:ft=help:norl: 365