usr_21.txt 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. *usr_21.txt* For Vim version 9.0. Last change: 2019 Apr 25
  2. VIM USER MANUAL - by Bram Moolenaar
  3. Go away and come back
  4. This chapter goes into mixing the use of other programs with Vim. Either by
  5. executing program from inside Vim or by leaving Vim and coming back later.
  6. Furthermore, this is about the ways to remember the state of Vim and restore
  7. it later.
  8. |21.1| Suspend and resume
  9. |21.2| Executing shell commands
  10. |21.3| Remembering information; viminfo
  11. |21.4| Sessions
  12. |21.5| Views
  13. |21.6| Modelines
  14. Next chapter: |usr_22.txt| Finding the file to edit
  15. Previous chapter: |usr_20.txt| Typing command-line commands quickly
  16. Table of contents: |usr_toc.txt|
  17. ==============================================================================
  18. *21.1* Suspend and resume
  19. Like most Unix programs Vim can be suspended by pressing CTRL-Z. This stops
  20. Vim and takes you back to the shell it was started in. You can then do any
  21. other commands until you are bored with them. Then bring back Vim with the
  22. "fg" command. >
  23. CTRL-Z
  24. {any sequence of shell commands}
  25. fg
  26. You are right back where you left Vim, nothing has changed.
  27. In case pressing CTRL-Z doesn't work, you can also use ":suspend".
  28. Don't forget to bring Vim back to the foreground, you would lose any changes
  29. that you made!
  30. Only Unix has support for this. On other systems Vim will start a shell for
  31. you. This also has the functionality of being able to execute shell commands.
  32. But it's a new shell, not the one that you started Vim from.
  33. When you are running the GUI you can't go back to the shell where Vim was
  34. started. CTRL-Z will minimize the Vim window instead.
  35. ==============================================================================
  36. *21.2* Executing shell commands
  37. To execute a single shell command from Vim use ":!{command}". For example, to
  38. see a directory listing: >
  39. :!ls
  40. :!dir
  41. The first one is for Unix, the second one for MS-Windows.
  42. Vim will execute the program. When it ends you will get a prompt to hit
  43. <Enter>. This allows you to have a look at the output from the command before
  44. returning to the text you were editing.
  45. The "!" is also used in other places where a program is run. Let's take
  46. a look at an overview:
  47. :!{program} execute {program}
  48. :r !{program} execute {program} and read its output
  49. :w !{program} execute {program} and send text to its input
  50. :[range]!{program} filter text through {program}
  51. Notice that the presence of a range before "!{program}" makes a big
  52. difference. Without it executes the program normally, with the range a number
  53. of text lines is filtered through the program.
  54. Executing a whole row of programs this way is possible. But a shell is much
  55. better at it. You can start a new shell this way: >
  56. :shell
  57. This is similar to using CTRL-Z to suspend Vim. The difference is that a new
  58. shell is started.
  59. When using the GUI the shell will be using the Vim window for its input and
  60. output. Since Vim is not a terminal emulator, this will not work perfectly.
  61. If you have trouble, try toggling the 'guipty' option. If this still doesn't
  62. work well enough, start a new terminal to run the shell in. For example with:
  63. >
  64. :!xterm&
  65. ==============================================================================
  66. *21.3* Remembering information; viminfo
  67. After editing for a while you will have text in registers, marks in various
  68. files, a command line history filled with carefully crafted commands. When
  69. you exit Vim all of this is lost. But you can get it back!
  70. The viminfo file is designed to store status information:
  71. Command-line and Search pattern history
  72. Text in registers
  73. Marks for various files
  74. The buffer list
  75. Global variables
  76. Each time you exit Vim it will store this information in a file, the viminfo
  77. file. When Vim starts again, the viminfo file is read and the information
  78. restored.
  79. The 'viminfo' option is set by default to restore a limited number of items.
  80. You might want to set it to remember more information. This is done through
  81. the following command: >
  82. :set viminfo=string
  83. The string specifies what to save. The syntax of this string is an option
  84. character followed by an argument. The option/argument pairs are separated by
  85. commas.
  86. Take a look at how you can build up your own viminfo string. First, the '
  87. option is used to specify how many files for which you save marks (a-z). Pick
  88. a nice even number for this option (1000, for instance). Your command now
  89. looks like this: >
  90. :set viminfo='1000
  91. The f option controls whether global marks (A-Z and 0-9) are stored. If this
  92. option is 0, none are stored. If it is 1 or you do not specify an f option,
  93. the marks are stored. You want this feature, so now you have this: >
  94. :set viminfo='1000,f1
  95. The < option controls how many lines are saved for each of the registers. By
  96. default, all the lines are saved. If 0, nothing is saved. To avoid adding
  97. thousands of lines to your viminfo file (which might never get used and makes
  98. starting Vim slower) you use a maximum of 500 lines: >
  99. :set viminfo='1000,f1,<500
  100. <
  101. Other options you might want to use:
  102. : number of lines to save from the command line history
  103. @ number of lines to save from the input line history
  104. / number of lines to save from the search history
  105. r removable media, for which no marks will be stored (can be
  106. used several times)
  107. ! global variables that start with an uppercase letter and
  108. don't contain lowercase letters
  109. h disable 'hlsearch' highlighting when starting
  110. % the buffer list (only restored when starting Vim without file
  111. arguments)
  112. c convert the text using 'encoding'
  113. n name used for the viminfo file (must be the last option)
  114. See the 'viminfo' option and |viminfo-file| for more information.
  115. When you run Vim multiple times, the last one exiting will store its
  116. information. This may cause information that previously exiting Vims stored
  117. to be lost. Each item can be remembered only once.
  118. GETTING BACK TO WHERE YOU STOPPED VIM
  119. You are halfway editing a file and it's time to leave for holidays. You exit
  120. Vim and go enjoy yourselves, forgetting all about your work. After a couple
  121. of weeks you start Vim, and type:
  122. >
  123. '0
  124. And you are right back where you left Vim. So you can get on with your work.
  125. Vim creates a mark each time you exit Vim. The last one is '0. The
  126. position that '0 pointed to is made '1. And '1 is made to '2, and so forth.
  127. Mark '9 is lost.
  128. The |:marks| command is useful to find out where '0 to '9 will take you.
  129. GETTING BACK TO SOME FILE
  130. If you want to go back to a file that you edited recently, but not when
  131. exiting Vim, there is a slightly more complicated way. You can see a list of
  132. files by typing the command: >
  133. :oldfiles
  134. < 1: ~/.viminfo ~
  135. 2: ~/text/resume.txt ~
  136. 3: /tmp/draft ~
  137. Now you would like to edit the second file, which is in the list preceded by
  138. "2:". You type: >
  139. :e #<2
  140. Instead of ":e" you can use any command that has a file name argument, the
  141. "#<2" item works in the same place as "%" (current file name) and "#"
  142. (alternate file name). So you can also split the window to edit the third
  143. file: >
  144. :split #<3
  145. That #<123 thing is a bit complicated when you just want to edit a file.
  146. Fortunately there is a simpler way: >
  147. :browse oldfiles
  148. < 1: ~/.viminfo ~
  149. 2: ~/text/resume.txt ~
  150. 3: /tmp/draft ~
  151. -- More --
  152. You get the same list of files as with |:oldfiles|. If you want to edit
  153. "resume.txt" first press "q" to stop the listing. You will get a prompt:
  154. Type number and <Enter> (empty cancels): ~
  155. Type "2" and press <Enter> to edit the second file.
  156. More info at |:oldfiles|, |v:oldfiles| and |c_#<|.
  157. MOVE INFO FROM ONE VIM TO ANOTHER
  158. You can use the ":wviminfo" and ":rviminfo" commands to save and restore the
  159. information while still running Vim. This is useful for exchanging register
  160. contents between two instances of Vim, for example. In the first Vim do: >
  161. :wviminfo! ~/tmp/viminfo
  162. And in the second Vim do: >
  163. :rviminfo! ~/tmp/viminfo
  164. Obviously, the "w" stands for "write" and the "r" for "read".
  165. The ! character is used by ":wviminfo" to forcefully overwrite an existing
  166. file. When it is omitted, and the file exists, the information is merged into
  167. the file.
  168. The ! character used for ":rviminfo" means that all the information is
  169. used, this may overwrite existing information. Without the ! only information
  170. that wasn't set is used.
  171. These commands can also be used to store info and use it again later. You
  172. could make a directory full of viminfo files, each containing info for a
  173. different purpose.
  174. ==============================================================================
  175. *21.4* Sessions
  176. Suppose you are editing along, and it is the end of the day. You want to quit
  177. work and pick up where you left off the next day. You can do this by saving
  178. your editing session and restoring it the next day.
  179. A Vim session contains all the information about what you are editing.
  180. This includes things such as the file list, window layout, global variables,
  181. options and other information. (Exactly what is remembered is controlled by
  182. the 'sessionoptions' option, described below.)
  183. The following command creates a session file: >
  184. :mksession vimbook.vim
  185. Later if you want to restore this session, you can use this command: >
  186. :source vimbook.vim
  187. If you want to start Vim and restore a specific session, you can use the
  188. following command: >
  189. vim -S vimbook.vim
  190. This tells Vim to read a specific file on startup. The 'S' stands for
  191. session (actually, you can source any Vim script with -S, thus it might as
  192. well stand for "source").
  193. The windows that were open are restored, with the same position and size as
  194. before. Mappings and option values are like before.
  195. What exactly is restored depends on the 'sessionoptions' option. The
  196. default value is:
  197. "blank,buffers,curdir,folds,help,options,tabpages,winsize,terminal".
  198. blank keep empty windows
  199. buffers all buffers, not only the ones in a window
  200. curdir the current directory
  201. folds folds, also manually created ones
  202. help the help window
  203. options all options and mappings
  204. tabpages all tab pages
  205. winsize window sizes
  206. terminal include terminal windows
  207. Change this to your liking. To also restore the size of the Vim window, for
  208. example, use: >
  209. :set sessionoptions+=resize
  210. SESSION HERE, SESSION THERE
  211. The obvious way to use sessions is when working on different projects.
  212. Suppose you store your session files in the directory "~/.vim". You are
  213. currently working on the "secret" project and have to switch to the "boring"
  214. project: >
  215. :wall
  216. :mksession! ~/.vim/secret.vim
  217. :source ~/.vim/boring.vim
  218. This first uses ":wall" to write all modified files. Then the current session
  219. is saved, using ":mksession!". This overwrites the previous session. The
  220. next time you load the secret session you can continue where you were at this
  221. point. And finally you load the new "boring" session.
  222. If you open help windows, split and close various windows, and generally mess
  223. up the window layout, you can go back to the last saved session: >
  224. :source ~/.vim/boring.vim
  225. Thus you have complete control over whether you want to continue next time
  226. where you are now, by saving the current setup in a session, or keep the
  227. session file as a starting point.
  228. Another way of using sessions is to create a window layout that you like to
  229. use, and save this in a session. Then you can go back to this layout whenever
  230. you want.
  231. For example, this is a nice layout to use:
  232. +----------------------------------------+
  233. | VIM - main help file |
  234. | |
  235. |Move around: Use the cursor keys, or "h|
  236. |help.txt================================|
  237. |explorer | |
  238. |dir |~ |
  239. |dir |~ |
  240. |file |~ |
  241. |file |~ |
  242. |file |~ |
  243. |file |~ |
  244. |~/=========|[No File]===================|
  245. | |
  246. +----------------------------------------+
  247. This has a help window at the top, so that you can read this text. The narrow
  248. vertical window on the left contains a file explorer. This is a Vim plugin
  249. that lists the contents of a directory. You can select files to edit there.
  250. More about this in the next chapter.
  251. Create this from a just started Vim with: >
  252. :help
  253. CTRL-W w
  254. :vertical split ~/
  255. You can resize the windows a bit to your liking. Then save the session with:
  256. >
  257. :mksession ~/.vim/mine.vim
  258. Now you can start Vim with this layout: >
  259. vim -S ~/.vim/mine.vim
  260. Hint: To open a file you see listed in the explorer window in the empty
  261. window, move the cursor to the filename and press "O". Double clicking with
  262. the mouse will also do this.
  263. UNIX AND MS-WINDOWS
  264. Some people have to do work on MS-Windows systems one day and on Unix another
  265. day. If you are one of them, consider adding "slash" and "unix" to
  266. 'sessionoptions'. The session files will then be written in a format that can
  267. be used on both systems. This is the command to put in your vimrc file: >
  268. :set sessionoptions+=unix,slash
  269. Vim will use the Unix format then, because the MS-Windows Vim can read and
  270. write Unix files, but Unix Vim can't read MS-Windows format session files.
  271. Similarly, MS-Windows Vim understands file names with / to separate names, but
  272. Unix Vim doesn't understand \.
  273. SESSIONS AND VIMINFO
  274. Sessions store many things, but not the position of marks, contents of
  275. registers and the command line history. You need to use the viminfo feature
  276. for these things.
  277. In most situations you will want to use sessions separately from viminfo.
  278. This can be used to switch to another session, but keep the command line
  279. history. And yank text into registers in one session, and paste it back in
  280. another session.
  281. You might prefer to keep the info with the session. You will have to do
  282. this yourself then. Example: >
  283. :mksession! ~/.vim/secret.vim
  284. :wviminfo! ~/.vim/secret.viminfo
  285. And to restore this again: >
  286. :source ~/.vim/secret.vim
  287. :rviminfo! ~/.vim/secret.viminfo
  288. ==============================================================================
  289. *21.5* Views
  290. A session stores the looks of the whole of Vim. When you want to store the
  291. properties for one window only, use a view.
  292. The use of a view is for when you want to edit a file in a specific way.
  293. For example, you have line numbers enabled with the 'number' option and
  294. defined a few folds. Just like with sessions, you can remember this view on
  295. the file and restore it later. Actually, when you store a session, it stores
  296. the view of each window.
  297. There are two basic ways to use views. The first is to let Vim pick a name
  298. for the view file. You can restore the view when you later edit the same
  299. file. To store the view for the current window: >
  300. :mkview
  301. Vim will decide where to store the view. When you later edit the same file
  302. you get the view back with this command: >
  303. :loadview
  304. That's easy, isn't it?
  305. Now you want to view the file without the 'number' option on, or with all
  306. folds open, you can set the options to make the window look that way. Then
  307. store this view with: >
  308. :mkview 1
  309. Obviously, you can get this back with: >
  310. :loadview 1
  311. Now you can switch between the two views on the file by using ":loadview" with
  312. and without the "1" argument.
  313. You can store up to ten views for the same file this way, one unnumbered
  314. and nine numbered 1 to 9.
  315. A VIEW WITH A NAME
  316. The second basic way to use views is by storing the view in a file with a name
  317. you choose. This view can be loaded while editing another file. Vim will
  318. then switch to editing the file specified in the view. Thus you can use this
  319. to quickly switch to editing another file, with all its options set as you
  320. saved them.
  321. For example, to save the view of the current file: >
  322. :mkview ~/.vim/main.vim
  323. You can restore it with: >
  324. :source ~/.vim/main.vim
  325. ==============================================================================
  326. *21.6* Modelines
  327. When editing a specific file, you might set options specifically for that
  328. file. Typing these commands each time is boring. Using a session or view for
  329. editing a file doesn't work when sharing the file between several people.
  330. The solution for this situation is adding a modeline to the file. This is
  331. a line of text that tells Vim the values of options, to be used in this file
  332. only.
  333. A typical example is a C program where you make indents by a multiple of 4
  334. spaces. This requires setting the 'shiftwidth' option to 4. This modeline
  335. will do that:
  336. /* vim:set shiftwidth=4: */ ~
  337. Put this line as one of the first or last five lines in the file. When
  338. editing the file, you will notice that 'shiftwidth' will have been set to
  339. four. When editing another file, it's set back to the default value of eight.
  340. For some files the modeline fits well in the header, thus it can be put at
  341. the top of the file. For text files and other files where the modeline gets
  342. in the way of the normal contents, put it at the end of the file.
  343. The 'modelines' option specifies how many lines at the start and end of the
  344. file are inspected for containing a modeline. To inspect ten lines: >
  345. :set modelines=10
  346. The 'modeline' option can be used to switch this off. Do this when you are
  347. working as root on Unix or Administrator on MS-Windows, or when you don't
  348. trust the files you are editing: >
  349. :set nomodeline
  350. Use this format for the modeline:
  351. any-text vim:set {option}={value} ... : any-text ~
  352. The "any-text" indicates that you can put any text before and after the part
  353. that Vim will use. This allows making it look like a comment, like what was
  354. done above with /* and */.
  355. The " vim:" part is what makes Vim recognize this line. There must be
  356. white space before "vim", or "vim" must be at the start of the line. Thus
  357. using something like "gvim:" will not work.
  358. The part between the colons is a ":set" command. It works the same way as
  359. typing the ":set" command, except that you need to insert a backslash before a
  360. colon (otherwise it would be seen as the end of the modeline).
  361. Another example:
  362. // vim:set textwidth=72 dir=c\:\tmp: use c:\tmp here ~
  363. There is an extra backslash before the first colon, so that it's included in
  364. the ":set" command. The text after the second colon is ignored, thus a remark
  365. can be placed there.
  366. For more details see |modeline|.
  367. ==============================================================================
  368. Next chapter: |usr_22.txt| Finding the file to edit
  369. Copyright: see |manual-copyright| vim:tw=78:ts=8:noet:ft=help:norl: