blob: c0ab47af811ca1bc608e5245b7b013f9ec8a8d67 [file] [log] [blame]
Christian Brabandtdfed0772025-06-04 21:40:21 +02001*usr_22.txt* For Vim version 9.1. Last change: 2025 Jun 04
Bram Moolenaar071d4272004-06-13 20:20:40 +00002
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
Bram Moolenaarf2330482008-06-24 20:19:36 +000013|22.1| The file browser
Bram Moolenaar071d4272004-06-13 20:20:40 +000014|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==============================================================================
Bram Moolenaarf2330482008-06-24 20:19:36 +000023*22.1* The file browser
Bram Moolenaar071d4272004-06-13 20:20:40 +000024
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
Christian Brabandtdfed0772025-06-04 21:40:21 +020030with the contents of the directory. It looks like this (slightly cleaned up
31so that it fits within 80 chars): >
Bram Moolenaar071d4272004-06-13 20:20:40 +000032
Yochem van Rosmalen20904052025-06-03 20:54:33 +020033 " ===========================================================================
Christian Brabandtdfed0772025-06-04 21:40:21 +020034 " Netrw Directory Listing (netrw v180)
35 " /path/to/vim/runtime/doc
Yochem van Rosmalen20904052025-06-03 20:54:33 +020036 " Sorted by name
Christian Brabandtdfed0772025-06-04 21:40:21 +020037 " Sort sequence: [\/]$,*,\(\.bak\|\~\|\.o\|\.h\|\.info\|\.swp\)[*@]\=$
38 " Quick Help: <F1>:help -:go up dir D:delete R:rename s:sort-by x:special
Yochem van Rosmalen20904052025-06-03 20:54:33 +020039 " ===========================================================================
40 ../
41 ./
42 check/
43 Makefile
44 autocmd.txt
45 change.txt
46 eval.txt
47 filetype.txt
48 help.txt.info
Bram Moolenaar071d4272004-06-13 20:20:40 +000049
50You can see these items:
Bram Moolenaarf2330482008-06-24 20:19:36 +000051
521. The name of the browsing tool and its version number
532. The name of the browsing directory
543. The method of sorting (may be by name, time, or size)
554. How names are to be sorted (directories first, then *.h files,
56 *.c files, etc)
575. How to get help (use the <F1> key), and an abbreviated listing
58 of available commands
Bram Moolenaar5e3dae82010-03-02 16:19:40 +0100596. A listing of files, including "../", which allows one to list
Bram Moolenaarf2330482008-06-24 20:19:36 +000060 the parent directory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000061
62If you have syntax highlighting enabled, the different parts are highlighted
Bram Moolenaarf2330482008-06-24 20:19:36 +000063so as to make it easier to spot them.
Bram Moolenaar071d4272004-06-13 20:20:40 +000064
65You can use Normal mode Vim commands to move around in the text. For example,
Bram Moolenaarf2330482008-06-24 20:19:36 +000066move the cursor atop a file and press <Enter>; you will then be editing that
67file. To go back to the browser use ":edit ." again, or use ":Explore".
68CTRL-O also works.
69
70Try using <Enter> while the cursor is atop a directory name. The result is
71that the file browser moves into that directory and displays the items found
Bram Moolenaar071d4272004-06-13 20:20:40 +000072there. Pressing <Enter> on the first directory "../" moves you one level
73higher. Pressing "-" does the same thing, without the need to move to the
74"../" item first.
75
Bram Moolenaarf2330482008-06-24 20:19:36 +000076You can press <F1> to get help on the things you can do in the netrw file
77browser. This is what you get: >
Bram Moolenaar071d4272004-06-13 20:20:40 +000078
Bram Moolenaar664f3cf2019-12-07 16:03:51 +010079 9. Directory Browsing netrw-browse netrw-dir netrw-list netrw-help
Bram Moolenaar071d4272004-06-13 20:20:40 +000080
Bram Moolenaar664f3cf2019-12-07 16:03:51 +010081 MAPS netrw-maps
Bram Moolenaar938ae282023-02-20 20:44:55 +000082 <F1>.............Help.......................................|netrw-help|
83 <cr>.............Browsing...................................|netrw-cr|
84 <del>............Deleting Files or Directories..............|netrw-delete|
85 -................Going Up...................................|netrw--|
86 a................Hiding Files or Directories................|netrw-a|
87 mb...............Bookmarking a Directory....................|netrw-mb|
88 gb...............Changing to a Bookmarked Directory.........|netrw-gb|
89 cd...............Make Browsing Directory The Current Dir....|netrw-c|
90 d................Make A New Directory.......................|netrw-d|
91 D................Deleting Files or Directories..............|netrw-D|
92 <c-h>............Edit File/Directory Hiding List............|netrw-ctrl-h|
93 i................Change Listing Style.......................|netrw-i|
94 <c-l>............Refreshing the Listing.....................|netrw-ctrl-l|
95 o................Browsing with a Horizontal Split...........|netrw-o|
96 p................Use Preview Window.........................|netrw-p|
97 P................Edit in Previous Window....................|netrw-p|
98 q................Listing Bookmarks and History..............|netrw-qb|
99 r................Reversing Sorting Order....................|netrw-r|
100< (etc)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000101
Bram Moolenaarf2330482008-06-24 20:19:36 +0000102The <F1> key thus brings you to a netrw directory browsing contents help page.
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200103It's a regular help page; use the usual |CTRL-]| to jump to tagged help items
Bram Moolenaarf2330482008-06-24 20:19:36 +0000104and |CTRL-O| to jump back.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105
Bram Moolenaarf2330482008-06-24 20:19:36 +0000106To select files for display and editing: (with the cursor is atop a filename)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107
Bram Moolenaarf2330482008-06-24 20:19:36 +0000108 <enter> Open the file in the current window. |netrw-cr|
109 o Horizontally split window and display file |netrw-o|
110 v Vertically split window and display file |netrw-v|
Bram Moolenaar938ae282023-02-20 20:44:55 +0000111 p Use the |preview-window| |netrw-p|
Bram Moolenaarf2330482008-06-24 20:19:36 +0000112 P Edit in the previous window |netrw-P|
113 t Open file in a new tab |netrw-t|
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114
Bram Moolenaarf2330482008-06-24 20:19:36 +0000115The following normal-mode commands may be used to control the browser display:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116
Bram Moolenaarf2330482008-06-24 20:19:36 +0000117 i Controls listing style (thin, long, wide, and tree).
118 The long listing includes size and date information.
119 s Repeatedly pressing s will change the way the files
120 are sorted; one may sort on name, modification time,
121 or size.
122 r Reverse the sorting order.
123
124As a sampling of extra normal-mode commands:
125
Bram Moolenaard1caa942020-04-10 22:10:56 +0200126 cd Change Vim's notion of the current directory to be
Bram Moolenaarf2330482008-06-24 20:19:36 +0000127 the same as the browser directory. (see
128 |g:netrw_keepdir| to control this, too)
129 R Rename the file or directory under the cursor; a
130 prompt will be issued for the new name.
131 D Delete the file or directory under the cursor; a
132 confirmation request will be issued.
133 mb gb Make bookmark/goto bookmark
134
135
136One may also use command mode; again, just a sampling:
137
138 :Explore [directory] Browse specified/current directory
139 :NetrwSettings A comprehensive list of your current netrw
140 settings with help linkage.
141
142The netrw browser is not limited to just your local machine; one may use
143urls such as: (that trailing / is important)
144
145 :Explore ftp://somehost/path/to/dir/
146 :e scp://somehost/path/to/dir/
147
148See |netrw-browse| for more.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149
150==============================================================================
151*22.2* The current directory
152
153Just like the shell, Vim has the concept of a current directory. Suppose you
154are in your home directory and want to edit several files in a directory
155"VeryLongFileName". You could do: >
156
157 :edit VeryLongFileName/file1.txt
158 :edit VeryLongFileName/file2.txt
159 :edit VeryLongFileName/file3.txt
160
161To avoid much of the typing, do this: >
162
163 :cd VeryLongFileName
164 :edit file1.txt
165 :edit file2.txt
166 :edit file3.txt
167
168The ":cd" command changes the current directory. You can see what the current
169directory is with the ":pwd" command: >
170
171 :pwd
172 /home/Bram/VeryLongFileName
173
174Vim remembers the last directory that you used. Use "cd -" to go back to it.
175Example: >
176
177 :pwd
178 /home/Bram/VeryLongFileName
179 :cd /etc
180 :pwd
181 /etc
182 :cd -
183 :pwd
184 /home/Bram/VeryLongFileName
185 :cd -
186 :pwd
187 /etc
188
189
190WINDOW LOCAL DIRECTORY
191
192When you split a window, both windows use the same current directory. When
193you want to edit a number of files somewhere else in the new window, you can
194make it use a different directory, without changing the current directory in
195the other window. This is called a local directory. >
196
197 :pwd
198 /home/Bram/VeryLongFileName
199 :split
200 :lcd /etc
201 :pwd
202 /etc
203 CTRL-W w
204 :pwd
205 /home/Bram/VeryLongFileName
206
Bram Moolenaar00aa0692019-04-27 20:37:57 +0200207So long as no `:lcd` command has been used, all windows share the same current
208directory. Doing a `:cd` command in one window will also change the current
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209directory of the other window.
Bram Moolenaar00aa0692019-04-27 20:37:57 +0200210 For a window where `:lcd` has been used a different current directory is
211remembered. Using `:cd` or `:lcd` in other windows will not change it.
212 When using a `:cd` command in a window that uses a different current
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213directory, it will go back to using the shared directory.
214
Bram Moolenaar00aa0692019-04-27 20:37:57 +0200215
216TAB LOCAL DIRECTORY
217
218When you open a new tab page, it uses the directory of the window in the
h-east8ee0e0b2024-10-05 16:44:27 +0200219previous tab page from which the new tab page was opened. You can change the
220directory of the current tab page using the `:tcd` command. All the windows in
Bram Moolenaar00aa0692019-04-27 20:37:57 +0200221a tab page share this directory except for windows with a window-local
h-east8ee0e0b2024-10-05 16:44:27 +0200222directory. Any new windows opened in this tab page will use this directory as
223the current working directory. Using a `:cd` command in a tab page will not
Bram Moolenaar00aa0692019-04-27 20:37:57 +0200224change the working directory of tab pages which have a tab local directory.
Bram Moolenaar68e65602019-05-26 21:33:31 +0200225When the global working directory is changed using the `:cd` command in a tab
Bram Moolenaar00aa0692019-04-27 20:37:57 +0200226page, it will also change the current tab page working directory.
227
228
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229==============================================================================
230*22.3* Finding a file
231
232You are editing a C program that contains this line:
233
234 #include "inits.h" ~
235
236You want to see what is in that "inits.h" file. Move the cursor on the name
237of the file and type: >
238
239 gf
240
241Vim will find the file and edit it.
242 What if the file is not in the current directory? Vim will use the 'path'
243option to find the file. This option is a list of directory names where to
244look for your file.
245 Suppose you have your include files located in "c:/prog/include". This
246command will add it to the 'path' option: >
247
248 :set path+=c:/prog/include
249
250This directory is an absolute path. No matter where you are, it will be the
251same place. What if you have located files in a subdirectory, below where the
252file is? Then you can specify a relative path name. This starts with a dot:
253>
254 :set path+=./proto
255
256This tells Vim to look in the directory "proto", below the directory where the
257file in which you use "gf" is. Thus using "gf" on "inits.h" will make Vim
258look for "proto/inits.h", starting in the directory of the file.
259 Without the "./", thus "proto", Vim would look in the "proto" directory
260below the current directory. And the current directory might not be where the
261file that you are editing is located.
262
263The 'path' option allows specifying the directories where to search for files
264in many more ways. See the help on the 'path' option.
265 The 'isfname' option is used to decide which characters are included in the
266file name, and which ones are not (e.g., the " character in the example
267above).
268
269When you know the file name, but it's not to be found in the file, you can
270type it: >
271
272 :find inits.h
273
274Vim will then use the 'path' option to try and locate the file. This is the
275same as the ":edit" command, except for the use of 'path'.
276
277To open the found file in a new window use CTRL-W f instead of "gf", or use
278":sfind" instead of ":find".
279
280
281A nice way to directly start Vim to edit a file somewhere in the 'path': >
282
283 vim "+find stdio.h"
284
285This finds the file "stdio.h" in your value of 'path'. The quotes are
286necessary to have one argument |-+c|.
287
288==============================================================================
289*22.4* The buffer list
290
291The Vim editor uses the term buffer to describe a file being edited.
292Actually, a buffer is a copy of the file that you edit. When you finish
293changing the buffer, you write the contents of the buffer to the file.
294Buffers not only contain file contents, but also all the marks, settings, and
295other stuff that goes with it.
296
297
298HIDDEN BUFFERS
299
300Suppose you are editing the file one.txt and need to edit the file two.txt.
301You could simply use ":edit two.txt", but since you made changes to one.txt
302that won't work. You also don't want to write one.txt yet. Vim has a
303solution for you: >
304
305 :hide edit two.txt
306
307The buffer "one.txt" disappears from the screen, but Vim still knows that you
308are editing this buffer, so it keeps the modified text. This is called a
309hidden buffer: The buffer contains text, but you can't see it.
Bram Moolenaarf2330482008-06-24 20:19:36 +0000310 The argument of ":hide" is another command. ":hide" makes that command
311behave as if the 'hidden' option was set. You could also set this option
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312yourself. The effect is that when any buffer is abandoned, it becomes hidden.
313 Be careful! When you have hidden buffers with changes, don't exit Vim
314without making sure you have saved all the buffers.
315
316
317INACTIVE BUFFERS
318
319 When a buffer has been used once, Vim remembers some information about it.
320When it is not displayed in a window and it is not hidden, it is still in the
321buffer list. This is called an inactive buffer. Overview:
322
323 Active Appears in a window, text loaded.
324 Hidden Not in a window, text loaded.
325 Inactive Not in a window, no text loaded.
326
327The inactive buffers are remembered, because Vim keeps information about them,
328like marks. And remembering the file name is useful too, so that you can see
329which files you have edited. And edit them again.
330
331
332LISTING BUFFERS
333
334View the buffer list with this command: >
335
336 :buffers
337
338A command which does the same, is not so obvious to list buffers, but is much
339shorter to type: >
340
341 :ls
342
343The output could look like this:
344
Bram Moolenaar97d62492012-11-15 21:28:22 +0100345 1 #h "help.txt" line 62 ~
346 2 %a + "usr_21.txt" line 1 ~
347 3 "usr_toc.txt" line 1 ~
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348
349The first column contains the buffer number. You can use this to edit the
350buffer without having to type the name, see below.
351 After the buffer number come the flags. Then the name of the file
352and the line number where the cursor was the last time.
353 The flags that can appear are these (from left to right):
354
355 u Buffer is unlisted |unlisted-buffer|.
356 % Current buffer.
357 # Alternate buffer.
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000358 a Buffer is loaded and displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359 h Buffer is loaded but hidden.
360 = Buffer is read-only.
361 - Buffer is not modifiable, the 'modifiable' option is off.
362 + Buffer has been modified.
363
364
365EDITING A BUFFER
366
367You can edit a buffer by its number. That avoids having to type the file
368name: >
369
370 :buffer 2
371
372But the only way to know the number is by looking in the buffer list. You can
373use the name, or part of it, instead: >
374
375 :buffer help
376
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200377Vim will find the best match for the name you type. If there is only one
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378buffer that matches the name, it will be used. In this case "help.txt".
379 To open a buffer in a new window: >
380
381 :sbuffer 3
382
383This works with a name as well.
384
385
386USING THE BUFFER LIST
387
388You can move around in the buffer list with these commands:
389
390 :bnext go to next buffer
391 :bprevious go to previous buffer
392 :bfirst go to the first buffer
393 :blast go to the last buffer
394
395To remove a buffer from the list, use this command: >
396
397 :bdelete 3
398
399Again, this also works with a name.
400 If you delete a buffer that was active (visible in a window), that window
401will be closed. If you delete the current buffer, the current window will be
402closed. If it was the last window, Vim will find another buffer to edit. You
403can't be editing nothing!
404
405 Note:
406 Even after removing the buffer with ":bdelete" Vim still remembers it.
407 It's actually made "unlisted", it no longer appears in the list from
408 ":buffers". The ":buffers!" command will list unlisted buffers (yes,
409 Vim can do the impossible). To really make Vim forget about a buffer,
410 use ":bwipe". Also see the 'buflisted' option.
411
412==============================================================================
413
414Next chapter: |usr_23.txt| Editing other files
415
Bram Moolenaard473c8c2018-08-11 18:00:22 +0200416Copyright: see |manual-copyright| vim:tw=78:ts=8:noet:ft=help:norl: