usr_22.txt (14214B)
1 *usr_22.txt* Nvim 2 3 4 VIM USER MANUAL by Bram Moolenaar 5 6 7 Finding the file to edit 8 9 10 Files can be found everywhere. So how do you find them? Vim offers various 11 ways to browse the directory tree. There are commands to jump to a file that 12 is mentioned in another. And Vim remembers which files have been edited 13 before. 14 15 |22.1| The file browser 16 |22.2| The current directory 17 |22.3| Finding a file 18 |22.4| The buffer list 19 20 Next chapter: |usr_23.txt| Editing other files 21 Previous chapter: |usr_21.txt| Go away and come back 22 Table of contents: |usr_toc.txt| 23 24 ============================================================================== 25 *22.1* The file browser 26 27 Vim has a plugin that makes it possible to edit a directory. Try this: > 28 29 :edit . 30 31 Through the magic of autocommands and Vim scripts, the window will be filled 32 with the contents of the directory. It looks like this (slightly cleaned up 33 so that it fits within 78 chars): > 34 35 " ========================================================================== 36 " Netrw Directory Listing (netrw v184) 37 " /path/to/vim/runtime/doc 38 " Sorted by name 39 " Sort sequence: [\/]$,*,\(\.bak\|\~\|\.o\|\.h\|\.info\|\.swp\)[*@]\=$ 40 " Quick Help: <F1>:help -:go up dir D:delete R:rename s:sort-by x:special 41 " ========================================================================== 42 ../ 43 ./ 44 check/ 45 Makefile 46 autocmd.txt 47 change.txt 48 eval.txt 49 filetype.txt 50 help.txt.info 51 < 52 53 You can see these items: 54 55 1. The name of the browsing tool and its version number 56 2. The name of the browsing directory 57 3. The method of sorting (may be by name, time, or size) 58 4. How names are to be sorted (directories first, then by extension, etc.) 59 5. How to get help (the <F1> key), and an abridged list of available commands 60 6. A listing of files, including "../" (it will list the parent directory). 61 62 If you have syntax highlighting enabled, the different parts are highlighted 63 so as to make it easier to spot them. 64 65 You can use Normal mode Vim commands to move around in the text. For example, 66 move the cursor atop a file and press <Enter>; you will then be editing that 67 file. To go back to the browser use ":edit ." again, or use ":Explore". 68 CTRL-O also works. 69 70 Try using <Enter> while the cursor is atop a directory name. The result is 71 that the file browser moves into that directory and displays the items found 72 there. Pressing <Enter> on the first directory "../" moves you one level 73 higher. Pressing "-" does the same thing, without the need to move to the 74 "../" item first. 75 76 You can press <F1> to get help on the things you can do in the netrw file 77 browser. This is what you get: 78 > 79 QUICK HELP netrw-quickhelp 80 (Use ctrl-] to select a topic) 81 Intro to Browsing...............................netrw-intro-browse 82 Quick Reference: Maps.........................netrw-quickmap 83 Quick Reference: Commands.....................netrw-browse-cmds 84 < 85 The <F1> key thus brings you to a netrw directory browsing contents help page. 86 It's a regular help page; use the usual |CTRL-]| to jump to tagged help items 87 and |CTRL-O| to jump back. So, if you CTRL-] on |netrw-quickmap| you will 88 jump to this: 89 > 90 netrw-quickmap netrw-quickmaps 91 QUICK REFERENCE: MAPS netrw-browse-maps 92 93 --- ----------------- ---- 94 Map Quick Explanation Link 95 --- ----------------- ---- 96 <F1> Causes Netrw to issue help 97 <cr> Netrw will enter the directory or read the file netrw-cr 98 <del> Netrw will attempt to remove the file/directory netrw-del 99 < (etc.) 100 101 To select files for display and editing (with the cursor atop a filename): 102 > 103 o Enter the file/directory under the cursor in a new netrw-o 104 browser window. A horizontal split is used. 105 O Obtain a file specified by cursor netrw-O 106 p Preview the file netrw-p 107 P Browse in the previously used window netrw-P 108 v Enter the file/directory under the cursor in a new netrw-v 109 browser window. A vertical split is used. 110 < 111 The following normal-mode commands may be used to control the browser display: 112 > 113 i Cycle between thin, long, wide, and tree listings netrw-i 114 r Reverse sorting order netrw-r 115 s Select sorting style: by name, time, or file size netrw-s 116 < 117 As a sampling of extra normal-mode commands: 118 > 119 cd Make browsing directory the current directory netrw-cd 120 D Attempt to remove the file(s)/directory(ies) netrw-D 121 gb Go to previous bookmarked directory netrw-gb 122 mb Bookmark current directory netrw-mb 123 R Rename the designated file(s)/directory(ies) netrw-R 124 < 125 One may also use command mode; again, just a sampling: 126 > 127 :Explore[!] [dir] Explore directory of current file......netrw-explore 128 :Hexplore[!] [dir] Horizontal Split & Explore.............netrw-explore 129 < 130 The netrw browser is not limited to just your local machine; one may use URLs 131 such as: > 132 133 :Explore ftp://somehost/path/to/dir/ 134 :e scp://somehost/path/to/dir/ 135 < 136 Note: The trailing "/" is important. 137 138 See |netrw-browse| for more. 139 140 ============================================================================== 141 *22.2* The current directory 142 143 Just like the shell, Vim has the concept of a current directory. Suppose you 144 are in your home directory and want to edit several files in a directory 145 "VeryLongFileName". You could do: > 146 147 :edit VeryLongFileName/file1.txt 148 :edit VeryLongFileName/file2.txt 149 :edit VeryLongFileName/file3.txt 150 151 To avoid much of the typing, do this: > 152 153 :cd VeryLongFileName 154 :edit file1.txt 155 :edit file2.txt 156 :edit file3.txt 157 158 The ":cd" command changes the current directory. You can see what the current 159 directory is with the ":pwd" command: > 160 161 :pwd 162 /home/Bram/VeryLongFileName 163 164 Vim remembers the last directory that you used. Use "cd -" to go back to it. 165 Example: > 166 167 :pwd 168 /home/Bram/VeryLongFileName 169 :cd /etc 170 :pwd 171 /etc 172 :cd - 173 :pwd 174 /home/Bram/VeryLongFileName 175 :cd - 176 :pwd 177 /etc 178 179 180 WINDOW LOCAL DIRECTORY 181 182 When you split a window, both windows use the same current directory. When 183 you want to edit a number of files somewhere else in the new window, you can 184 make it use a different directory, without changing the current directory in 185 the other window. This is called a local directory. > 186 187 :pwd 188 /home/Bram/VeryLongFileName 189 :split 190 :lcd /etc 191 :pwd 192 /etc 193 CTRL-W w 194 :pwd 195 /home/Bram/VeryLongFileName 196 197 So long as no `:lcd` command has been used, all windows share the same current 198 directory. Doing a `:cd` command in one window will also change the current 199 directory of the other window. 200 For a window where `:lcd` has been used a different current directory is 201 remembered. Using `:cd` or `:lcd` in other windows will not change it. 202 When using a `:cd` command in a window that uses a different current 203 directory, it will go back to using the shared directory. 204 205 206 TAB LOCAL DIRECTORY 207 208 When you open a new tab page, it uses the directory of the window in the 209 previous tab page from which the new tab page was opened. You can change the 210 directory of the current tab page using the `:tcd` command. All the windows 211 in a tab page share this directory except for windows with a window-local 212 directory. Any new windows opened in this tab page will use this directory as 213 the current working directory. Using a `:cd` command in a tab page will not 214 change the working directory of tab pages which have a tab local directory. 215 When the global working directory is changed using the `:cd` command in a tab 216 page, it will also change the current tab page working directory. 217 218 219 ============================================================================== 220 *22.3* Finding a file 221 222 You are editing a C program that contains this line: 223 224 #include "inits.h" ~ 225 226 You want to see what is in that "inits.h" file. Move the cursor on the name 227 of the file and type: > 228 229 gf 230 231 Vim will find the file and edit it. 232 What if the file is not in the current directory? Vim will use the 'path' 233 option to find the file. This option is a list of directory names where to 234 look for your file. 235 Suppose you have your include files located in "c:/prog/include". This 236 command will add it to the 'path' option: > 237 238 :set path+=c:/prog/include 239 240 This directory is an absolute path. No matter where you are, it will be the 241 same place. What if you have located files in a subdirectory, below where the 242 file is? Then you can specify a relative path name. This starts with a dot: 243 > 244 :set path+=./proto 245 246 This tells Vim to look in the directory "proto", below the directory where the 247 file in which you use "gf" is. Thus using "gf" on "inits.h" will make Vim 248 look for "proto/inits.h", starting in the directory of the file. 249 Without the "./", thus "proto", Vim would look in the "proto" directory 250 below the current directory. And the current directory might not be where the 251 file that you are editing is located. 252 253 The 'path' option allows specifying the directories where to search for files 254 in many more ways. See the help on the 'path' option. 255 The 'isfname' option is used to decide which characters are included in the 256 file name, and which ones are not (e.g., the " character in the example 257 above). 258 259 When you know the file name, but it's not to be found in the file, you can 260 type it: > 261 262 :find inits.h 263 264 Vim will then use the 'path' option to try and locate the file. This is the 265 same as the ":edit" command, except for the use of 'path'. 266 267 To open the found file in a new window use CTRL-W f instead of "gf", or use 268 ":sfind" instead of ":find". 269 270 271 A nice way to directly start Vim to edit a file somewhere in the 'path': > 272 273 vim "+find stdio.h" 274 275 This finds the file "stdio.h" in your value of 'path'. The quotes are 276 necessary to have one argument |-+c|. 277 278 ============================================================================== 279 *22.4* The buffer list 280 281 The Vim editor uses the term buffer to describe a file being edited. 282 Actually, a buffer is a copy of the file that you edit. When you finish 283 changing the buffer, you write the contents of the buffer to the file. 284 Buffers not only contain file contents, but also all the marks, settings, and 285 other stuff that goes with it. 286 287 288 HIDDEN BUFFERS 289 290 Suppose you are editing the file one.txt and need to edit the file two.txt. 291 You could simply use ":edit two.txt", but since you made changes to one.txt 292 that won't work. You also don't want to write one.txt yet. Vim has a 293 solution for you: > 294 295 :hide edit two.txt 296 297 The buffer "one.txt" disappears from the screen, but Vim still knows that you 298 are editing this buffer, so it keeps the modified text. This is called a 299 hidden buffer: The buffer contains text, but you can't see it. 300 The argument of ":hide" is another command. ":hide" makes that command 301 behave as if the 'hidden' option was set. You could also set this option 302 yourself. The effect is that when any buffer is abandoned, it becomes hidden. 303 Be careful! When you have hidden buffers with changes, don't exit Vim 304 without making sure you have saved all the buffers. 305 306 307 INACTIVE BUFFERS 308 309 When a buffer has been used once, Vim remembers some information about it. 310 When it is not displayed in a window and it is not hidden, it is still in the 311 buffer list. This is called an inactive buffer. Overview: 312 313 Active Appears in a window, text loaded. 314 Hidden Not in a window, text loaded. 315 Inactive Not in a window, no text loaded. 316 317 The inactive buffers are remembered, because Vim keeps information about them, 318 like marks. And remembering the file name is useful too, so that you can see 319 which files you have edited. And edit them again. 320 321 322 LISTING BUFFERS 323 324 View the buffer list with this command: > 325 326 :buffers 327 328 A command which does the same, is not so obvious to list buffers, but is much 329 shorter to type: > 330 331 :ls 332 333 The output could look like this: 334 335 1 #h "help.txt" line 62 ~ 336 2 %a + "usr_21.txt" line 1 ~ 337 3 "usr_toc.txt" line 1 ~ 338 339 The first column contains the buffer number. You can use this to edit the 340 buffer without having to type the name, see below. 341 After the buffer number come the flags. Then the name of the file 342 and the line number where the cursor was the last time. 343 The flags that can appear are these (from left to right): 344 345 u Buffer is unlisted |unlisted-buffer|. 346 % Current buffer. 347 # Alternate buffer. 348 a Buffer is loaded and displayed. 349 h Buffer is loaded but hidden. 350 = Buffer is read-only. 351 - Buffer is not modifiable, the 'modifiable' option is off. 352 + Buffer has been modified. 353 354 355 EDITING A BUFFER 356 357 You can edit a buffer by its number. That avoids having to type the file 358 name: > 359 360 :buffer 2 361 362 But the only way to know the number is by looking in the buffer list. You can 363 use the name, or part of it, instead: > 364 365 :buffer help 366 367 Vim will find the best match for the name you type. If there is only one 368 buffer that matches the name, it will be used. In this case "help.txt". 369 To open a buffer in a new window: > 370 371 :sbuffer 3 372 373 This works with a name as well. 374 375 376 USING THE BUFFER LIST 377 378 You can move around in the buffer list with these commands: 379 380 :bnext go to next buffer 381 :bprevious go to previous buffer 382 :bfirst go to the first buffer 383 :blast go to the last buffer 384 385 To remove a buffer from the list, use this command: > 386 387 :bdelete 3 388 389 Again, this also works with a name. 390 If you delete a buffer that was active (visible in a window), that window 391 will be closed. If you delete the current buffer, the current window will be 392 closed. If it was the last window, Vim will find another buffer to edit. You 393 can't be editing nothing! 394 395 Note: Even after removing the buffer with ":bdelete" Vim still 396 remembers it. It's actually made "unlisted", it no longer appears in 397 the list from ":buffers". The ":buffers!" command will list unlisted 398 buffers (yes, Vim can do the impossible). To really make Vim forget 399 about a buffer, use ":bwipe". Also see the 'buflisted' option. 400 401 ============================================================================== 402 403 Next chapter: |usr_23.txt| Editing other files 404 405 Copyright: see |manual-copyright| vim:tw=78:ts=8:noet:ft=help:norl: