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