Bram Moolenaar | cfa8f9a | 2022-06-03 21:59:47 +0100 | [diff] [blame^] | 1 | *usr_50.txt* For Vim version 8.2. Last change: 2022 Jun 03 |
Bram Moolenaar | 30ab04e | 2022-05-14 13:33:50 +0100 | [diff] [blame] | 2 | |
| 3 | VIM USER MANUAL - by Bram Moolenaar |
| 4 | |
| 5 | Advanced Vim script writing |
| 6 | |
| 7 | |
Bram Moolenaar | cfa8f9a | 2022-06-03 21:59:47 +0100 | [diff] [blame^] | 8 | |50.1| Line continuation |
| 9 | |50.2| Restoring the view |
Bram Moolenaar | 30ab04e | 2022-05-14 13:33:50 +0100 | [diff] [blame] | 10 | |
| 11 | Next chapter: |usr_51.txt| Create a plugin |
| 12 | Previous chapter: |usr_45.txt| Select your language (local) |
| 13 | Table of contents: |usr_toc.txt| |
| 14 | |
| 15 | ============================================================================== |
Bram Moolenaar | cfa8f9a | 2022-06-03 21:59:47 +0100 | [diff] [blame^] | 16 | *50.1* Line continuation |
Bram Moolenaar | 30ab04e | 2022-05-14 13:33:50 +0100 | [diff] [blame] | 17 | |
Bram Moolenaar | cfa8f9a | 2022-06-03 21:59:47 +0100 | [diff] [blame^] | 18 | In legacy Vim script line contination is done by preceding a contination line |
| 19 | with a backslash: > |
| 20 | let mylist = [ |
| 21 | \ 'one', |
| 22 | \ 'two', |
| 23 | \ ] |
| 24 | |
| 25 | This requires the 'cpo' option to exclude the "C" flag. Normally this is done |
| 26 | by putting this at the start of the script: > |
| 27 | let s:save_cpo = &cpo |
| 28 | set cpo&vim |
| 29 | |
| 30 | And restore the option at the end of the script: > |
| 31 | let &cpo = s:save_cpo |
| 32 | unlet s:save_cpo |
| 33 | |
| 34 | A few more details can be found here: |line-continuation|. |
| 35 | |
| 36 | In |Vim9| script the backslash can still be used, but in most places it is not |
| 37 | needed: > |
| 38 | var mylist = [ |
| 39 | 'one', |
| 40 | 'two', |
| 41 | ] |
| 42 | |
| 43 | Also, the 'cpo' option does not need to be changed. See |
| 44 | |vim9-line-continuation| for details. |
| 45 | |
| 46 | ============================================================================== |
| 47 | *50.2* Restoring the view |
| 48 | |
| 49 | Sometimes you want to make a change and go back to where the cursor was. |
| 50 | Restoring the relative position would also be nice, so that the same line |
| 51 | appears at the top of the window. |
| 52 | |
| 53 | This example yanks the current line, puts it above the first line in the file |
| 54 | and then restores the view: > |
| 55 | |
| 56 | map ,p ma"aYHmbgg"aP`bzt`a |
| 57 | |
| 58 | What this does: > |
| 59 | ma"aYHmbgg"aP`bzt`a |
| 60 | < ma set mark a at cursor position |
| 61 | "aY yank current line into register a |
| 62 | Hmb go to top line in window and set mark b there |
| 63 | gg go to first line in file |
| 64 | "aP put the yanked line above it |
| 65 | `b go back to top line in display |
| 66 | zt position the text in the window as before |
| 67 | `a go back to saved cursor position |
| 68 | |
Bram Moolenaar | 30ab04e | 2022-05-14 13:33:50 +0100 | [diff] [blame] | 69 | |
| 70 | ============================================================================== |
| 71 | |
| 72 | Next chapter: |usr_51.txt| Create a plugin |
| 73 | |
| 74 | Copyright: see |manual-copyright| vim:tw=78:ts=8:noet:ft=help:norl: |