1*usr_22.txt* For Vim version 8.2. Last change: 2020 Mar 28 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 browser 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 browser 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" ============================================================================ ~ 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./ ~ 40check/ ~ 41Makefile ~ 42autocmd.txt ~ 43change.txt ~ 44eval.txt~ ~ 45filetype.txt~ ~ 46help.txt.info ~ 47 48You can see these items: 49 501. The name of the browsing tool and its version number 512. The name of the browsing directory 523. The method of sorting (may be by name, time, or size) 534. How names are to be sorted (directories first, then *.h files, 54 *.c files, etc) 555. How to get help (use the <F1> key), and an abbreviated listing 56 of available commands 576. A listing of files, including "../", which allows one to list 58 the parent directory. 59 60If you have syntax highlighting enabled, the different parts are highlighted 61so as to make it easier to spot them. 62 63You can use Normal mode Vim commands to move around in the text. For example, 64move the cursor atop a file and press <Enter>; you will then be editing that 65file. To go back to the browser use ":edit ." again, or use ":Explore". 66CTRL-O also works. 67 68Try using <Enter> while the cursor is atop a directory name. The result is 69that the file browser moves into that directory and displays the items found 70there. Pressing <Enter> on the first directory "../" moves you one level 71higher. Pressing "-" does the same thing, without the need to move to the 72"../" item first. 73 74You can press <F1> to get help on the things you can do in the netrw file 75browser. This is what you get: > 76 77 9. Directory Browsing netrw-browse netrw-dir netrw-list netrw-help 78 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 cd...............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-qb| 97 r................Reversing Sorting Order....................|netrw-r| 98< (etc) 99 100The <F1> key thus brings you to a netrw directory browsing contents help page. 101It's a regular help page; use the usual |CTRL-]| to jump to tagged help items 102and |CTRL-O| to jump back. 103 104To select files for display and editing: (with the cursor is atop a filename) 105 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| 112 113The following normal-mode commands may be used to control the browser display: 114 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. 121 122As a sampling of extra normal-mode commands: 123 124 cd 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 132 133 134One may also use command mode; again, just a sampling: 135 136 :Explore [directory] Browse specified/current directory 137 :NetrwSettings A comprehensive list of your current netrw 138 settings with help linkage. 139 140The netrw browser is not limited to just your local machine; one may use 141urls such as: (that trailing / is important) 142 143 :Explore ftp://somehost/path/to/dir/ 144 :e scp://somehost/path/to/dir/ 145 146See |netrw-browse| for more. 147 148============================================================================== 149*22.2* The current directory 150 151Just like the shell, Vim has the concept of a current directory. Suppose you 152are in your home directory and want to edit several files in a directory 153"VeryLongFileName". You could do: > 154 155 :edit VeryLongFileName/file1.txt 156 :edit VeryLongFileName/file2.txt 157 :edit VeryLongFileName/file3.txt 158 159To avoid much of the typing, do this: > 160 161 :cd VeryLongFileName 162 :edit file1.txt 163 :edit file2.txt 164 :edit file3.txt 165 166The ":cd" command changes the current directory. You can see what the current 167directory is with the ":pwd" command: > 168 169 :pwd 170 /home/Bram/VeryLongFileName 171 172Vim remembers the last directory that you used. Use "cd -" to go back to it. 173Example: > 174 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 186 187 188WINDOW LOCAL DIRECTORY 189 190When you split a window, both windows use the same current directory. When 191you want to edit a number of files somewhere else in the new window, you can 192make it use a different directory, without changing the current directory in 193the other window. This is called a local directory. > 194 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 204 205So long as no `:lcd` command has been used, all windows share the same current 206directory. Doing a `:cd` command in one window will also change the current 207directory of the other window. 208 For a window where `:lcd` has been used a different current directory is 209remembered. 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 211directory, it will go back to using the shared directory. 212 213 214TAB LOCAL DIRECTORY 215 216When you open a new tab page, it uses the directory of the window in the 217previous tab page from which the new tab page was opened. You can change the 218directory of the current tab page using the `:tcd` command. All the windows in 219a tab page share this directory except for windows with a window-local 220directory. Any new windows opened in this tab page will use this directory as 221the current working directory. Using a `:cd` command in a tab page will not 222change the working directory of tab pages which have a tab local directory. 223When the global working directory is changed using the `:cd` command in a tab 224page, it will also change the current tab page working directory. 225 226 227============================================================================== 228*22.3* Finding a file 229 230You are editing a C program that contains this line: 231 232 #include "inits.h" ~ 233 234You want to see what is in that "inits.h" file. Move the cursor on the name 235of the file and type: > 236 237 gf 238 239Vim will find the file and edit it. 240 What if the file is not in the current directory? Vim will use the 'path' 241option to find the file. This option is a list of directory names where to 242look for your file. 243 Suppose you have your include files located in "c:/prog/include". This 244command will add it to the 'path' option: > 245 246 :set path+=c:/prog/include 247 248This directory is an absolute path. No matter where you are, it will be the 249same place. What if you have located files in a subdirectory, below where the 250file is? Then you can specify a relative path name. This starts with a dot: 251> 252 :set path+=./proto 253 254This tells Vim to look in the directory "proto", below the directory where the 255file in which you use "gf" is. Thus using "gf" on "inits.h" will make Vim 256look for "proto/inits.h", starting in the directory of the file. 257 Without the "./", thus "proto", Vim would look in the "proto" directory 258below the current directory. And the current directory might not be where the 259file that you are editing is located. 260 261The 'path' option allows specifying the directories where to search for files 262in many more ways. See the help on the 'path' option. 263 The 'isfname' option is used to decide which characters are included in the 264file name, and which ones are not (e.g., the " character in the example 265above). 266 267When you know the file name, but it's not to be found in the file, you can 268type it: > 269 270 :find inits.h 271 272Vim will then use the 'path' option to try and locate the file. This is the 273same as the ":edit" command, except for the use of 'path'. 274 275To open the found file in a new window use CTRL-W f instead of "gf", or use 276":sfind" instead of ":find". 277 278 279A nice way to directly start Vim to edit a file somewhere in the 'path': > 280 281 vim "+find stdio.h" 282 283This finds the file "stdio.h" in your value of 'path'. The quotes are 284necessary to have one argument |-+c|. 285 286============================================================================== 287*22.4* The buffer list 288 289The Vim editor uses the term buffer to describe a file being edited. 290Actually, a buffer is a copy of the file that you edit. When you finish 291changing the buffer, you write the contents of the buffer to the file. 292Buffers not only contain file contents, but also all the marks, settings, and 293other stuff that goes with it. 294 295 296HIDDEN BUFFERS 297 298Suppose you are editing the file one.txt and need to edit the file two.txt. 299You could simply use ":edit two.txt", but since you made changes to one.txt 300that won't work. You also don't want to write one.txt yet. Vim has a 301solution for you: > 302 303 :hide edit two.txt 304 305The buffer "one.txt" disappears from the screen, but Vim still knows that you 306are editing this buffer, so it keeps the modified text. This is called a 307hidden buffer: The buffer contains text, but you can't see it. 308 The argument of ":hide" is another command. ":hide" makes that command 309behave as if the 'hidden' option was set. You could also set this option 310yourself. The effect is that when any buffer is abandoned, it becomes hidden. 311 Be careful! When you have hidden buffers with changes, don't exit Vim 312without making sure you have saved all the buffers. 313 314 315INACTIVE BUFFERS 316 317 When a buffer has been used once, Vim remembers some information about it. 318When it is not displayed in a window and it is not hidden, it is still in the 319buffer list. This is called an inactive buffer. Overview: 320 321 Active Appears in a window, text loaded. 322 Hidden Not in a window, text loaded. 323 Inactive Not in a window, no text loaded. 324 325The inactive buffers are remembered, because Vim keeps information about them, 326like marks. And remembering the file name is useful too, so that you can see 327which files you have edited. And edit them again. 328 329 330LISTING BUFFERS 331 332View the buffer list with this command: > 333 334 :buffers 335 336A command which does the same, is not so obvious to list buffers, but is much 337shorter to type: > 338 339 :ls 340 341The output could look like this: 342 343 1 #h "help.txt" line 62 ~ 344 2 %a + "usr_21.txt" line 1 ~ 345 3 "usr_toc.txt" line 1 ~ 346 347The first column contains the buffer number. You can use this to edit the 348buffer without having to type the name, see below. 349 After the buffer number come the flags. Then the name of the file 350and the line number where the cursor was the last time. 351 The flags that can appear are these (from left to right): 352 353 u Buffer is unlisted |unlisted-buffer|. 354 % Current buffer. 355 # Alternate buffer. 356 a Buffer is loaded and displayed. 357 h Buffer is loaded but hidden. 358 = Buffer is read-only. 359 - Buffer is not modifiable, the 'modifiable' option is off. 360 + Buffer has been modified. 361 362 363EDITING A BUFFER 364 365You can edit a buffer by its number. That avoids having to type the file 366name: > 367 368 :buffer 2 369 370But the only way to know the number is by looking in the buffer list. You can 371use the name, or part of it, instead: > 372 373 :buffer help 374 375Vim will find the best match for the name you type. If there is only one 376buffer that matches the name, it will be used. In this case "help.txt". 377 To open a buffer in a new window: > 378 379 :sbuffer 3 380 381This works with a name as well. 382 383 384USING THE BUFFER LIST 385 386You can move around in the buffer list with these commands: 387 388 :bnext go to next buffer 389 :bprevious go to previous buffer 390 :bfirst go to the first buffer 391 :blast go to the last buffer 392 393To remove a buffer from the list, use this command: > 394 395 :bdelete 3 396 397Again, this also works with a name. 398 If you delete a buffer that was active (visible in a window), that window 399will be closed. If you delete the current buffer, the current window will be 400closed. If it was the last window, Vim will find another buffer to edit. You 401can't be editing nothing! 402 403 Note: 404 Even after removing the buffer with ":bdelete" Vim still remembers it. 405 It's actually made "unlisted", it no longer appears in the list from 406 ":buffers". The ":buffers!" command will list unlisted buffers (yes, 407 Vim can do the impossible). To really make Vim forget about a buffer, 408 use ":bwipe". Also see the 'buflisted' option. 409 410============================================================================== 411 412Next chapter: |usr_23.txt| Editing other files 413 414Copyright: see |manual-copyright| vim:tw=78:ts=8:noet:ft=help:norl: 415