usr_22.txt 15 KB

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