45bd2f4aa5bbbf71d06610077766612ee2b8012c
[MacVim.git] / runtime / doc / usr_22.txt
blob45bd2f4aa5bbbf71d06610077766612ee2b8012c
1 *usr_22.txt*    For Vim version 7.2.  Last change: 2007 Aug 14
3                      VIM USER MANUAL - by Bram Moolenaar
5                            Finding the file to edit
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.
13 |22.1|  The file browser
14 |22.2|  The current directory
15 |22.3|  Finding a file
16 |22.4|  The buffer list
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|
22 ==============================================================================
23 *22.1*  The file browser
25 Vim has a plugin that makes it possible to edit a directory.  Try this: >
27         :edit .
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:
32 " ============================================================================ ~
33 " Netrw Directory Listing                                        (netrw v109) ~
34 "   Sorted by      name ~
35 "   Sort sequence: [\/]$,\.h$,\.c$,\.cpp$,*,\.info$,\.swp$,\.o$\.obj$,\.bak$ ~
36 "   Quick Help: <F1>:help  -:go up dir  D:delete  R:rename  s:sort-by  x:exec ~
37 " ============================================================================ ~
38 ../ ~
39 ./ ~
40 check/ ~
41 Makefile ~
42 autocmd.txt ~
43 change.txt ~
44 eval.txt~ ~
45 filetype.txt~ ~
46 help.txt.info ~
48 You can see these items:
50 1.  The name of the browsing tool and its version number
51 2.  The name of the browsing directory
52 3.  The method of sorting (may be by name, time, or size)
53 4.  How names are to be sorted (directories first, then *.h files,
54     *.c files, etc)
55 5.  How to get help (use the <F1> key), and an abbreviated listing
56     of available commands
57 6.  A listing of files, including "../", which allows one to to list
58     the parent directory.
60 If you have syntax highlighting enabled, the different parts are highlighted
61 so as to make it easier to spot them.
63 You can use Normal mode Vim commands to move around in the text.  For example,
64 move the cursor atop a file and press <Enter>; you will then be editing that
65 file.  To go back to the browser use ":edit ." again, or use ":Explore".
66 CTRL-O also works.
68 Try using <Enter> while the cursor is atop a directory name.  The result is
69 that the file browser moves into that directory and displays the items found
70 there.  Pressing <Enter> on the first directory "../" moves you one level
71 higher.  Pressing "-" does the same thing, without the need to move to the
72 "../" item first.
74 You can press <F1> to get help on the things you can do in the netrw file
75 browser.  This is what you get: >
77     9. Directory Browsing        netrw-browse   netrw-dir   netrw-list   netrw-help 
79     MAPS                                                                 netrw-maps 
80          <F1>.............Help.......................................|netrw-help|
81          <cr>.............Browsing...................................|netrw-cr|
82          <del>............Deleting Files or Directories..............|netrw-delete|
83          -................Going Up...................................|netrw--|
84          a................Hiding Files or Directories................|netrw-a|
85          mb...............Bookmarking a Directory....................|netrw-mb|
86          gb...............Changing to a Bookmarked Directory.........|netrw-gb|
87          c................Make Browsing Directory The Current Dir....|netrw-c|
88          d................Make A New Directory.......................|netrw-d|
89          D................Deleting Files or Directories..............|netrw-D|
90          <c-h>............Edit File/Directory Hiding List............|netrw-ctrl-h|
91          i................Change Listing Style.......................|netrw-i|
92          <c-l>............Refreshing the Listing.....................|netrw-ctrl-l|
93          o................Browsing with a Horizontal Split...........|netrw-o|
94          p................Use Preview Window.........................|netrw-p|
95          P................Edit in Previous Window....................|netrw-p|
96          q................Listing Bookmarks and History..............|netrw-q|
97          r................Reversing Sorting Order....................|netrw-r|
98 <       (etc)
100 The <F1> key thus brings you to a netrw directory browsing contents help page.
101 Its a regular help page; use the usual |CTRL-]| to jump to tagged help items
102 and |CTRL-O| to jump back.
104 To select files for display and editing: (with the cursor is atop a filename)
106         <enter>         Open the file in the current window.       |netrw-cr|
107         o               Horizontally split window and display file |netrw-o|
108         v               Vertically split window and display file   |netrw-v|
109         p               Use the |preview-window|                   |netrw-p|
110         P               Edit in the previous window                |netrw-P|
111         t               Open file in a new tab                     |netrw-t|
113 The following normal-mode commands may be used to control the browser display:
115         i               Controls listing style (thin, long, wide, and tree).
116                         The long listing includes size and date information.
117         s               Repeatedly pressing s will change the way the files
118                         are sorted; one may sort on name, modification time,
119                         or size.
120         r               Reverse the sorting order.
122 As a sampling of extra normal-mode commands:
124         c               Change Vim's notion of the current directory to be
125                         the same as the browser directory.  (see
126                         |g:netrw_keepdir| to control this, too)
127         R               Rename the file or directory under the cursor; a
128                         prompt will be issued for the new name.
129         D               Delete the file or directory under the cursor; a
130                         confirmation request will be issued.
131         mb gb           Make bookmark/goto bookmark
134 One may also use command mode; again, just a sampling:
136         :Explore [directory]    Browse specified/current directory
137         :NetrwSettings          A comprehensive list of your current netrw
138                                 settings with help linkage.
140 The netrw browser is not limited to just your local machine; one may use
141 urls such as:    (that trailing / is important)
143         :Explore ftp://somehost/path/to/dir/
144         :e scp://somehost/path/to/dir/
146 See |netrw-browse| for more.
148 ==============================================================================
149 *22.2*  The current directory
151 Just like the shell, Vim has the concept of a current directory.  Suppose you
152 are in your home directory and want to edit several files in a directory
153 "VeryLongFileName".  You could do: >
155         :edit VeryLongFileName/file1.txt
156         :edit VeryLongFileName/file2.txt
157         :edit VeryLongFileName/file3.txt
159 To avoid much of the typing, do this: >
161         :cd VeryLongFileName
162         :edit file1.txt
163         :edit file2.txt
164         :edit file3.txt
166 The ":cd" command changes the current directory.  You can see what the current
167 directory is with the ":pwd" command: >
169         :pwd
170         /home/Bram/VeryLongFileName
172 Vim remembers the last directory that you used.  Use "cd -" to go back to it.
173 Example: >
175         :pwd
176         /home/Bram/VeryLongFileName
177         :cd /etc
178         :pwd
179         /etc
180         :cd -
181         :pwd
182         /home/Bram/VeryLongFileName
183         :cd -
184         :pwd
185         /etc
188 WINDOW LOCAL DIRECTORY
190 When you split a window, both windows use the same current directory.  When
191 you want to edit a number of files somewhere else in the new window, you can
192 make it use a different directory, without changing the current directory in
193 the other window.  This is called a local directory. >
195         :pwd
196         /home/Bram/VeryLongFileName
197         :split
198         :lcd /etc
199         :pwd
200         /etc
201         CTRL-W w
202         :pwd
203         /home/Bram/VeryLongFileName
205 So long as no ":lcd" command has been used, all windows share the same current
206 directory.  Doing a ":cd" command in one window will also change the current
207 directory of the other window.
208    For a window where ":lcd" has been used a different current directory is
209 remembered.  Using ":cd" or ":lcd" in other windows will not change it.
210    When using a ":cd" command in a window that uses a different current
211 directory, it will go back to using the shared directory.
213 ==============================================================================
214 *22.3*  Finding a file
216 You are editing a C program that contains this line:
218         #include "inits.h" ~
220 You want to see what is in that "inits.h" file.  Move the cursor on the name
221 of the file and type: >
223         gf
225 Vim will find the file and edit it.
226    What if the file is not in the current directory?  Vim will use the 'path'
227 option to find the file.  This option is a list of directory names where to
228 look for your file.
229    Suppose you have your include files located in "c:/prog/include".  This
230 command will add it to the 'path' option: >
232         :set path+=c:/prog/include
234 This directory is an absolute path.  No matter where you are, it will be the
235 same place.  What if you have located files in a subdirectory, below where the
236 file is?  Then you can specify a relative path name.  This starts with a dot:
238         :set path+=./proto
240 This tells Vim to look in the directory "proto", below the directory where the
241 file in which you use "gf" is.  Thus using "gf" on "inits.h" will make Vim
242 look for "proto/inits.h", starting in the directory of the file.
243    Without the "./", thus "proto", Vim would look in the "proto" directory
244 below the current directory.  And the current directory might not be where the
245 file that you are editing is located.
247 The 'path' option allows specifying the directories where to search for files
248 in many more ways.  See the help on the 'path' option.
249    The 'isfname' option is used to decide which characters are included in the
250 file name, and which ones are not (e.g., the " character in the example
251 above).
253 When you know the file name, but it's not to be found in the file, you can
254 type it: >
256         :find inits.h
258 Vim will then use the 'path' option to try and locate the file.  This is the
259 same as the ":edit" command, except for the use of 'path'.
261 To open the found file in a new window use CTRL-W f instead of "gf", or use
262 ":sfind" instead of ":find".
265 A nice way to directly start Vim to edit a file somewhere in the 'path': >
267         vim "+find stdio.h"
269 This finds the file "stdio.h" in your value of 'path'.  The quotes are
270 necessary to have one argument |-+c|.
272 ==============================================================================
273 *22.4*  The buffer list
275 The Vim editor uses the term buffer to describe a file being edited.
276 Actually, a buffer is a copy of the file that you edit.  When you finish
277 changing the buffer, you write the contents of the buffer to the file.
278 Buffers not only contain file contents, but also all the marks, settings, and
279 other stuff that goes with it.
282 HIDDEN BUFFERS
284 Suppose you are editing the file one.txt and need to edit the file two.txt.
285 You could simply use ":edit two.txt", but since you made changes to one.txt
286 that won't work.  You also don't want to write one.txt yet.  Vim has a
287 solution for you: >
289         :hide edit two.txt
291 The buffer "one.txt" disappears from the screen, but Vim still knows that you
292 are editing this buffer, so it keeps the modified text.  This is called a
293 hidden buffer: The buffer contains text, but you can't see it.
294    The argument of ":hide" is another command.  ":hide" makes that command
295 behave as if the 'hidden' option was set.  You could also set this option
296 yourself.  The effect is that when any buffer is abandoned, it becomes hidden.
297    Be careful!  When you have hidden buffers with changes, don't exit Vim
298 without making sure you have saved all the buffers.
301 INACTIVE BUFFERS
303    When a buffer has been used once, Vim remembers some information about it.
304 When it is not displayed in a window and it is not hidden, it is still in the
305 buffer list.  This is called an inactive buffer.  Overview:
307    Active               Appears in a window, text loaded.
308    Hidden               Not in a window, text loaded.
309    Inactive             Not in a window, no text loaded.
311 The inactive buffers are remembered, because Vim keeps information about them,
312 like marks.  And remembering the file name is useful too, so that you can see
313 which files you have edited.  And edit them again.
316 LISTING BUFFERS
318 View the buffer list with this command: >
320         :buffers
322 A command which does the same, is not so obvious to list buffers, but is much
323 shorter to type: >
325         :ls
327 The output could look like this:
329   1 #h  "help.txt"                      line 62 ~
330   2 %a+ "usr_21.txt"                    line 1 ~
331   3     "usr_toc.txt"                   line 1 ~
333 The first column contains the buffer number.  You can use this to edit the
334 buffer without having to type the name, see below.
335    After the buffer number come the flags.  Then the name of the file
336 and the line number where the cursor was the last time.
337    The flags that can appear are these (from left to right):
339         u       Buffer is unlisted |unlisted-buffer|.
340          %      Current buffer.
341          #      Alternate buffer.
342           a     Buffer is loaded and displayed.
343           h     Buffer is loaded but hidden.
344            =    Buffer is read-only.
345            -    Buffer is not modifiable, the 'modifiable' option is off.
346             +   Buffer has been modified.
349 EDITING A BUFFER
351 You can edit a buffer by its number.  That avoids having to type the file
352 name: >
354         :buffer 2
356 But the only way to know the number is by looking in the buffer list.  You can
357 use the name, or part of it, instead: >
359         :buffer help
361 Vim will find a best match for the name you type.  If there is only one
362 buffer that matches the name, it will be used.  In this case "help.txt".
363    To open a buffer in a new window: >
365         :sbuffer 3
367 This works with a name as well.
370 USING THE BUFFER LIST
372 You can move around in the buffer list with these commands:
374         :bnext          go to next buffer
375         :bprevious      go to previous buffer
376         :bfirst         go to the first buffer
377         :blast          go to the last buffer
379 To remove a buffer from the list, use this command: >
381         :bdelete 3
383 Again, this also works with a name.
384    If you delete a buffer that was active (visible in a window), that window
385 will be closed.  If you delete the current buffer, the current window will be
386 closed.  If it was the last window, Vim will find another buffer to edit.  You
387 can't be editing nothing!
389         Note:
390         Even after removing the buffer with ":bdelete" Vim still remembers it.
391         It's actually made "unlisted", it no longer appears in the list from
392         ":buffers".  The ":buffers!" command will list unlisted buffers (yes,
393         Vim can do the impossible).  To really make Vim forget about a buffer,
394         use ":bwipe".  Also see the 'buflisted' option.
396 ==============================================================================
398 Next chapter: |usr_23.txt|  Editing other files
400 Copyright: see |manual-copyright|  vim:tw=78:ts=8:ft=help:norl: