codekingpro/portable-devtools
114k
1*usr_22.txt* For Vim version 9.2. Last change: 2026 Feb 142 3 4 VIM USER MANUAL by Bram Moolenaar5 6 7 Finding the file to edit8 9 10Files can be found everywhere. So how do you find them? Vim offers various11ways to browse the directory tree. There are commands to jump to a file that12is mentioned in another. And Vim remembers which files have been edited13before.14 15|22.1| The file browser16|22.2| The current directory17|22.3| Finding a file18|22.4| The buffer list19 20 Next chapter: |usr_23.txt| Editing other files21 Previous chapter: |usr_21.txt| Go away and come back22Table of contents: |usr_toc.txt|23 24==============================================================================25*22.1* The file browser26 27Vim has a plugin that makes it possible to edit a directory. Try this: >28 29 :edit .30 31Through the magic of autocommands and Vim scripts, the window will be filled32with the contents of the directory. It looks like this (slightly cleaned up33so that it fits within 78 chars): >34 35 " ==========================================================================36 " Netrw Directory Listing (netrw v184)37 " /path/to/vim/runtime/doc38 " Sorted by name39 " Sort sequence: [\/]$,*,\(\.bak\|\~\|\.o\|\.h\|\.info\|\.swp\)[*@]\=$40 " Quick Help: <F1>:help -:go up dir D:delete R:rename s:sort-by x:special41 " ==========================================================================42 ../43 ./44 check/45 Makefile46 autocmd.txt47 change.txt48 eval.txt49 filetype.txt50 help.txt.info51 52You can see these items:53 541. The name of the browsing tool and its version number552. The name of the browsing directory563. The method of sorting (may be by name, time, or size)574. How names are to be sorted (directories first, then by extension, etc.)585. How to get help (the <F1> key), and an abridged list of available commands596. A listing of files, including "../" (it will list the parent directory).60 61If you have syntax highlighting enabled, the different parts are highlighted62so as to make it easier to spot them.63 64You can use Normal mode Vim commands to move around in the text. For example,65move the cursor atop a file and press <Enter>; you will then be editing that66file. To go back to the browser use ":edit ." again, or use ":Explore".67CTRL-O also works.68 69Try using <Enter> while the cursor is atop a directory name. The result is70that the file browser moves into that directory and displays the items found71there. Pressing <Enter> on the first directory "../" moves you one level72higher. Pressing "-" does the same thing, without the need to move to the73"../" item first.74 75You can press <F1> to get help on the things you can do in the netrw file76browser. This is what you get:77>78 QUICK HELP netrw-quickhelp79 (Use ctrl-] to select a topic)80 Intro to Browsing...............................netrw-intro-browse81 Quick Reference: Maps.........................netrw-quickmap82 Quick Reference: Commands.....................netrw-browse-cmds83<84The <F1> key thus brings you to a netrw directory browsing contents help page.85It's a regular help page; use the usual |CTRL-]| to jump to tagged help items86and |CTRL-O| to jump back. So, if you CTRL-] on |netrw-quickmap| you will87jump to this:88>89 netrw-quickmap netrw-quickmaps90 QUICK REFERENCE: MAPS netrw-browse-maps91 92 --- ----------------- ----93 Map Quick Explanation Link94 --- ----------------- ----95 <F1> Causes Netrw to issue help96 <cr> Netrw will enter the directory or read the file netrw-cr97 <del> Netrw will attempt to remove the file/directory netrw-del98< (etc.)99 100To select files for display and editing (with the cursor atop a filename):101>102 o Enter the file/directory under the cursor in a new netrw-o103 browser window. A horizontal split is used.104 O Obtain a file specified by cursor netrw-O105 p Preview the file netrw-p106 P Browse in the previously used window netrw-P107 v Enter the file/directory under the cursor in a new netrw-v108 browser window. A vertical split is used.109<110The following normal-mode commands may be used to control the browser display:111>112 i Cycle between thin, long, wide, and tree listings netrw-i113 r Reverse sorting order netrw-r114 s Select sorting style: by name, time, or file size netrw-s115<116As a sampling of extra normal-mode commands:117>118 cd Make browsing directory the current directory netrw-cd119 D Attempt to remove the file(s)/directory(ies) netrw-D120 gb Go to previous bookmarked directory netrw-gb121 mb Bookmark current directory netrw-mb122 R Rename the designated file(s)/directory(ies) netrw-R123<124One may also use command mode; again, just a sampling:125>126 :Explore[!] [dir] Explore directory of current file......netrw-explore127 :Hexplore[!] [dir] Horizontal Split & Explore.............netrw-explore128<129The netrw browser is not limited to just your local machine; one may use URLs130such as: >131 132 :Explore ftp://somehost/path/to/dir/133 :e scp://somehost/path/to/dir/134<135 Note: The trailing "/" is important.136 137See |netrw-browse| for more.138 139==============================================================================140*22.2* The current directory141 142Just like the shell, Vim has the concept of a current directory. Suppose you143are in your home directory and want to edit several files in a directory144"VeryLongFileName". You could do: >145 146 :edit VeryLongFileName/file1.txt147 :edit VeryLongFileName/file2.txt148 :edit VeryLongFileName/file3.txt149 150To avoid much of the typing, do this: >151 152 :cd VeryLongFileName153 :edit file1.txt154 :edit file2.txt155 :edit file3.txt156 157The ":cd" command changes the current directory. You can see what the current158directory is with the ":pwd" command: >159 160 :pwd161 /home/Bram/VeryLongFileName162 163Vim remembers the last directory that you used. Use "cd -" to go back to it.164Example: >165 166 :pwd167 /home/Bram/VeryLongFileName168 :cd /etc169 :pwd170 /etc171 :cd -172 :pwd173 /home/Bram/VeryLongFileName174 :cd -175 :pwd176 /etc177 178 179WINDOW LOCAL DIRECTORY180 181When you split a window, both windows use the same current directory. When182you want to edit a number of files somewhere else in the new window, you can183make it use a different directory, without changing the current directory in184the other window. This is called a local directory. >185 186 :pwd187 /home/Bram/VeryLongFileName188 :split189 :lcd /etc190 :pwd191 /etc192 CTRL-W w193 :pwd194 /home/Bram/VeryLongFileName195 196So long as no `:lcd` command has been used, all windows share the same current197directory. Doing a `:cd` command in one window will also change the current198directory of the other window.199 For a window where `:lcd` has been used a different current directory is200remembered. Using `:cd` or `:lcd` in other windows will not change it.201 When using a `:cd` command in a window that uses a different current202directory, it will go back to using the shared directory.203 204 205TAB LOCAL DIRECTORY206 207When you open a new tab page, it uses the directory of the window in the208previous tab page from which the new tab page was opened. You can change the209directory of the current tab page using the `:tcd` command. All the windows210in a tab page share this directory except for windows with a window-local211directory. Any new windows opened in this tab page will use this directory as212the current working directory. Using a `:cd` command in a tab page will not213change the working directory of tab pages which have a tab local directory.214When the global working directory is changed using the `:cd` command in a tab215page, it will also change the current tab page working directory.216 217 218==============================================================================219*22.3* Finding a file220 221You are editing a C program that contains this line:222 223 #include "inits.h" ~224 225You want to see what is in that "inits.h" file. Move the cursor on the name226of the file and type: >227 228 gf229 230Vim will find the file and edit it.231 What if the file is not in the current directory? Vim will use the 'path'232option to find the file. This option is a list of directory names where to233look for your file.234 Suppose you have your include files located in "c:/prog/include". This235command will add it to the 'path' option: >236 237 :set path+=c:/prog/include238 239This directory is an absolute path. No matter where you are, it will be the240same place. What if you have located files in a subdirectory, below where the241file is? Then you can specify a relative path name. This starts with a dot:242>243 :set path+=./proto244 245This tells Vim to look in the directory "proto", below the directory where the246file in which you use "gf" is. Thus using "gf" on "inits.h" will make Vim247look for "proto/inits.h", starting in the directory of the file.248 Without the "./", thus "proto", Vim would look in the "proto" directory249below the current directory. And the current directory might not be where the250file that you are editing is located.251 252The 'path' option allows specifying the directories where to search for files253in many more ways. See the help on the 'path' option.254 The 'isfname' option is used to decide which characters are included in the255file name, and which ones are not (e.g., the " character in the example256above).257 258When you know the file name, but it's not to be found in the file, you can259type it: >260 261 :find inits.h262 263Vim will then use the 'path' option to try and locate the file. This is the264same as the ":edit" command, except for the use of 'path'.265 266To open the found file in a new window use CTRL-W f instead of "gf", or use267":sfind" instead of ":find".268 269 270A nice way to directly start Vim to edit a file somewhere in the 'path': >271 272 vim "+find stdio.h"273 274This finds the file "stdio.h" in your value of 'path'. The quotes are275necessary to have one argument |-+c|.276 277==============================================================================278*22.4* The buffer list279 280The Vim editor uses the term buffer to describe a file being edited.281Actually, a buffer is a copy of the file that you edit. When you finish282changing the buffer, you write the contents of the buffer to the file.283Buffers not only contain file contents, but also all the marks, settings, and284other stuff that goes with it.285 286 287HIDDEN BUFFERS288 289Suppose you are editing the file one.txt and need to edit the file two.txt.290You could simply use ":edit two.txt", but since you made changes to one.txt291that won't work. You also don't want to write one.txt yet. Vim has a292solution for you: >293 294 :hide edit two.txt295 296The buffer "one.txt" disappears from the screen, but Vim still knows that you297are editing this buffer, so it keeps the modified text. This is called a298hidden buffer: The buffer contains text, but you can't see it.299 The argument of ":hide" is another command. ":hide" makes that command300behave as if the 'hidden' option was set. You could also set this option301yourself. The effect is that when any buffer is abandoned, it becomes hidden.302 Be careful! When you have hidden buffers with changes, don't exit Vim303without making sure you have saved all the buffers.304 305 306INACTIVE BUFFERS307 308When a buffer has been used once, Vim remembers some information about it.309When it is not displayed in a window and it is not hidden, it is still in the310buffer list. This is called an inactive buffer. Overview:311 312 Active Appears in a window, text loaded.313 Hidden Not in a window, text loaded.314 Inactive Not in a window, no text loaded.315 316The inactive buffers are remembered, because Vim keeps information about them,317like marks. And remembering the file name is useful too, so that you can see318which files you have edited. And edit them again.319 320 321LISTING BUFFERS322 323View the buffer list with this command: >324 325 :buffers326 327A command which does the same, is not so obvious to list buffers, but is much328shorter to type: >329 330 :ls331 332The output could look like this:333 334 1 #h "help.txt" line 62 ~335 2 %a + "usr_21.txt" line 1 ~336 3 "usr_toc.txt" line 1 ~337 338The first column contains the buffer number. You can use this to edit the339buffer without having to type the name, see below.340 After the buffer number come the flags. Then the name of the file341and the line number where the cursor was the last time.342 The flags that can appear are these (from left to right):343 344 u Buffer is unlisted |unlisted-buffer|.345 % Current buffer.346 # Alternate buffer.347 a Buffer is loaded and displayed.348 h Buffer is loaded but hidden.349 = Buffer is read-only.350 - Buffer is not modifiable, the 'modifiable' option is off.351 + Buffer has been modified.352 353 354EDITING A BUFFER355 356You can edit a buffer by its number. That avoids having to type the file357name: >358 359 :buffer 2360 361But the only way to know the number is by looking in the buffer list. You can362use the name, or part of it, instead: >363 364 :buffer help365 366Vim will find the best match for the name you type. If there is only one367buffer that matches the name, it will be used. In this case "help.txt".368 To open a buffer in a new window: >369 370 :sbuffer 3371 372This works with a name as well.373 374 375USING THE BUFFER LIST376 377You can move around in the buffer list with these commands:378 379 :bnext go to next buffer380 :bprevious go to previous buffer381 :bfirst go to the first buffer382 :blast go to the last buffer383 384To remove a buffer from the list, use this command: >385 386 :bdelete 3387 388Again, this also works with a name.389 If you delete a buffer that was active (visible in a window), that window390will be closed. If you delete the current buffer, the current window will be391closed. If it was the last window, Vim will find another buffer to edit. You392can't be editing nothing!393 394 Note: Even after removing the buffer with ":bdelete" Vim still395 remembers it. It's actually made "unlisted", it no longer appears in396 the list from ":buffers". The ":buffers!" command will list unlisted397 buffers (yes, Vim can do the impossible). To really make Vim forget398 about a buffer, use ":bwipe". Also see the 'buflisted' option.399 400==============================================================================401 402Next chapter: |usr_23.txt| Editing other files403 404Copyright: see |manual-copyright| vim:tw=78:ts=8:noet:ft=help:norl:405 