usr_21.txt 17 KB

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