usr_42.txt 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. *usr_42.txt* Nvim
  2. VIM USER MANUAL - by Bram Moolenaar
  3. Add new menus
  4. By now you know that Vim is very flexible. This includes the menus used in
  5. the GUI. You can define your own menu entries to make certain commands easily
  6. accessible. This is for mouse-happy users only.
  7. |42.1| Introduction
  8. |42.2| Menu commands
  9. |42.3| Various
  10. |42.4| Toolbar and popup menus
  11. Next chapter: |usr_43.txt| Using filetypes
  12. Previous chapter: |usr_41.txt| Write a Vim script
  13. Table of contents: |usr_toc.txt|
  14. ==============================================================================
  15. *42.1* Introduction
  16. The menus that Vim uses are defined in the file "$VIMRUNTIME/menu.vim". If
  17. you want to write your own menus, you might first want to look through that
  18. file.
  19. To define a menu item, use the ":menu" command. The basic form of this
  20. command is as follows: >
  21. :menu {menu-item} {keys}
  22. The {menu-item} describes where on the menu to put the item. A typical
  23. {menu-item} is "File.Save", which represents the item "Save" under the
  24. "File" menu. A dot is used to separate the names. Example: >
  25. :menu File.Save :update<CR>
  26. The ":update" command writes the file when it was modified.
  27. You can add another level: "Edit.Settings.Shiftwidth" defines a submenu
  28. "Settings" under the "Edit" menu, with an item "Shiftwidth". You could use
  29. even deeper levels. Don't use this too much, you need to move the mouse quite
  30. a bit to use such an item.
  31. The ":menu" command is very similar to the ":map" command: the left side
  32. specifies how the item is triggered and the right hand side defines the
  33. characters that are executed. {keys} are characters, they are used just like
  34. you would have typed them. Thus in Insert mode, when {keys} is plain text,
  35. that text is inserted.
  36. ACCELERATORS
  37. The ampersand character (&) is used to indicate an accelerator. For instance,
  38. you can use Alt-F to select "File" and S to select "Save". (The 'winaltkeys'
  39. option may disable this though!). Therefore, the {menu-item} looks like
  40. "&File.&Save". The accelerator characters will be underlined in the menu.
  41. You must take care that each key is used only once in each menu. Otherwise
  42. you will not know which of the two will actually be used. Vim doesn't warn
  43. you for this.
  44. PRIORITIES
  45. The actual definition of the File.Save menu item is as follows: >
  46. :menu 10.340 &File.&Save<Tab>:w :confirm w<CR>
  47. The number 10.340 is called the priority number. It is used by the editor to
  48. decide where it places the menu item. The first number (10) indicates the
  49. position on the menu bar. Lower numbered menus are positioned to the left,
  50. higher numbers to the right.
  51. These are the priorities used for the standard menus:
  52. 10 20 40 50 60 70 9999
  53. +------------------------------------------------------------+
  54. | File Edit Tools Syntax Buffers Window Help |
  55. +------------------------------------------------------------+
  56. Notice that the Help menu is given a very high number, to make it appear on
  57. the far right.
  58. The second number (340) determines the location of the item within the
  59. pull-down menu. Lower numbers go on top, higher number on the bottom. These
  60. are the priorities in the File menu:
  61. >
  62. +-----------------+
  63. 10.310 |Open... |
  64. 10.320 |Split-Open... |
  65. 10.325 |New |
  66. 10.330 |Close |
  67. 10.335 |---------------- |
  68. 10.340 |Save |
  69. 10.350 |Save As... |
  70. 10.400 |---------------- |
  71. 10.410 |Split Diff with |
  72. 10.420 |Split Patched By |
  73. 10.500 |---------------- |
  74. 10.510 |Print |
  75. 10.600 |---------------- |
  76. 10.610 |Save-Exit |
  77. 10.620 |Exit |
  78. +-----------------+
  79. <
  80. Notice that there is room in between the numbers. This is where you can
  81. insert your own items, if you really want to (it's often better to leave the
  82. standard menus alone and add a new menu for your own items).
  83. When you create a submenu, you can add another ".number" to the priority.
  84. Thus each name in {menu-item} has its priority number.
  85. SPECIAL CHARACTERS
  86. The {menu-item} in this example is "&File.&Save<Tab>:w". This brings up an
  87. important point: {menu-item} must be one word. If you want to put a dot,
  88. space or tabs in the name, you either use the <> notation (<Space> and <Tab>,
  89. for instance) or use the backslash (\) escape. >
  90. :menu 10.305 &File.&Do\ It\.\.\. :exit<CR>
  91. In this example, the name of the menu item "Do It..." contains a space and the
  92. command is ":exit<CR>".
  93. The <Tab> character in a menu name is used to separate the part that defines
  94. the menu name from the part that gives a hint to the user. The part after the
  95. <Tab> is displayed right aligned in the menu. In the File.Save menu the name
  96. used is "&File.&Save<Tab>:w". Thus the menu name is "File.Save" and the hint
  97. is ":w".
  98. SEPARATORS
  99. The separator lines, used to group related menu items together, can be defined
  100. by using a name that starts and ends in a '-'. For example "-sep-". When
  101. using several separators the names must be different. Otherwise the names
  102. don't matter.
  103. The command from a separator will never be executed, but you have to define
  104. one anyway. A single colon will do. Example: >
  105. :amenu 20.510 Edit.-sep3- :
  106. ==============================================================================
  107. *42.2* Menu commands
  108. You can define menu items that exist for only certain modes. This works just
  109. like the variations on the ":map" command:
  110. :menu Normal, Visual and Operator-pending mode
  111. :nmenu Normal mode
  112. :vmenu Visual mode
  113. :omenu Operator-pending mode
  114. :menu! Insert and Command-line mode
  115. :imenu Insert mode
  116. :cmenu Command-line mode
  117. :tlmenu Terminal mode
  118. :amenu All modes (except for Terminal mode)
  119. To avoid that the commands of a menu item are being mapped, use the command
  120. ":noremenu", ":nnoremenu", ":anoremenu", etc.
  121. USING :AMENU
  122. The ":amenu" command is a bit different. It assumes that the {keys} you
  123. give are to be executed in Normal mode. When Vim is in Visual or Insert mode
  124. when the menu is used, Vim first has to go back to Normal mode. ":amenu"
  125. inserts a CTRL-C or CTRL-O for you. For example, if you use this command:
  126. >
  127. :amenu 90.100 Mine.Find\ Word *
  128. Then the resulting menu commands will be:
  129. Normal mode: `*`
  130. Visual mode: CTRL-C `*`
  131. Operator-pending mode: CTRL-C `*`
  132. Insert mode: CTRL-O `*`
  133. Command-line mode: CTRL-C `*`
  134. When in Command-line mode the CTRL-C will abandon the command typed so far.
  135. In Visual and Operator-pending mode CTRL-C will stop the mode. The CTRL-O in
  136. Insert mode will execute the command and then return to Insert mode.
  137. CTRL-O only works for one command. If you need to use two or more
  138. commands, put them in a function and call that function. Example: >
  139. :amenu Mine.Next\ File :call <SID>NextFile()<CR>
  140. :function <SID>NextFile()
  141. : next
  142. : 1/^Code
  143. :endfunction
  144. This menu entry goes to the next file in the argument list with ":next". Then
  145. it searches for the line that starts with "Code".
  146. The <SID> before the function name is the script ID. This makes the
  147. function local to the current Vim script file. This avoids problems when a
  148. function with the same name is defined in another script file. See |<SID>|.
  149. SILENT MENUS
  150. The menu executes the {keys} as if you typed them. For a ":" command this
  151. means you will see the command being echoed on the command line. If it's a
  152. long command, the hit-Enter prompt will appear. That can be very annoying!
  153. To avoid this, make the menu silent. This is done with the <silent>
  154. argument. For example, take the call to NextFile() in the previous example.
  155. When you use this menu, you will see this on the command line:
  156. :call <SNR>34_NextFile() ~
  157. To avoid this text on the command line, insert "<silent>" as the first
  158. argument: >
  159. :amenu <silent> Mine.Next\ File :call <SID>NextFile()<CR>
  160. Don't use "<silent>" too often. It is not needed for short commands. If you
  161. make a menu for someone else, being able to see the executed command will
  162. give them a hint about what they could have typed, instead of using the mouse.
  163. LISTING MENUS
  164. When a menu command is used without a {keys} part, it lists the already
  165. defined menus. You can specify a {menu-item}, or part of it, to list specific
  166. menus. Example: >
  167. :amenu
  168. This lists all menus. That's a long list! Better specify the name of a menu
  169. to get a shorter list: >
  170. :amenu Edit
  171. This lists only the "Edit" menu items for all modes. To list only one
  172. specific menu item for Insert mode: >
  173. :imenu Edit.Undo
  174. Take care that you type exactly the right name. Case matters here. But the
  175. '&' for accelerators can be omitted. The <Tab> and what comes after it can be
  176. left out as well.
  177. DELETING MENUS
  178. To delete a menu, the same command is used as for listing, but with "menu"
  179. changed to "unmenu". Thus ":menu" becomes, ":unmenu", ":nmenu" becomes
  180. ":nunmenu", etc. To delete the "Tools.Make" item for Insert mode: >
  181. :iunmenu Tools.Make
  182. You can delete a whole menu, with all its items, by using the menu name.
  183. Example: >
  184. :aunmenu Syntax
  185. This deletes the Syntax menu and all the items in it.
  186. ==============================================================================
  187. *42.3* Various
  188. You can change the appearance of the menus with flags in 'guioptions'. In the
  189. default value they are all included, except "M". You can remove a flag with a
  190. command like: >
  191. :set guioptions-=m
  192. <
  193. m When removed the menubar is not displayed.
  194. M When added the default menus are not loaded.
  195. g When removed the inactive menu items are not made grey
  196. but are completely removed. (Does not work on all
  197. systems.)
  198. For translating menu items, see |:menutrans|.
  199. Since the mouse has to be used to select a menu item, it is a good idea to use
  200. the ":browse" command for selecting a file. And ":confirm" to get a dialog
  201. instead of an error message, e.g., when the current buffer contains changes.
  202. These two can be combined: >
  203. :amenu File.Open :browse confirm edit<CR>
  204. The ":browse" makes a file browser appear to select the file to edit. The
  205. ":confirm" will pop up a dialog when the current buffer has changes. You can
  206. then select to save the changes, throw them away or cancel the command.
  207. For more complicated items, the confirm() and inputdialog() functions can
  208. be used. The default menus contain a few examples.
  209. ==============================================================================
  210. *42.4* Toolbar and popup menus
  211. There are two special menus: ToolBar and PopUp. Items that start with these
  212. names do not appear in the normal menu bar.
  213. TOOLBAR
  214. The toolbar appears only when the "T" flag is included in the 'guioptions'
  215. option.
  216. The toolbar uses icons rather than text to represent the command. For
  217. example, the {menu-item} named "ToolBar.New" causes the "New" icon to appear
  218. on the toolbar.
  219. The Vim editor has 28 built-in icons. You can find a table here:
  220. |builtin-tools|. Most of them are used in the default toolbar. You can
  221. redefine what these items do (after the default menus are setup).
  222. You can add another bitmap for a toolbar item. Or define a new toolbar
  223. item with a bitmap. For example, define a new toolbar item with: >
  224. :tmenu ToolBar.Compile Compile the current file
  225. :amenu ToolBar.Compile :!cc %:S -o %:r:S<CR>
  226. Now you need to create the icon. For MS-Windows it must be in bitmap format,
  227. with the name "Compile.bmp". For Unix XPM format is used, the file name is
  228. "Compile.xpm". The size must be 18 by 18 pixels. On MS-Windows other sizes
  229. can be used as well, but it will look ugly.
  230. Put the bitmap in the directory "bitmaps" in one of the directories from
  231. 'runtimepath'. E.g., for Unix "~/.config/nvim/bitmaps/Compile.xpm".
  232. You can define tooltips for the items in the toolbar. A tooltip is a short
  233. text that explains what a toolbar item will do. For example "Open file". It
  234. appears when the mouse pointer is on the item, without moving for a moment.
  235. This is very useful if the meaning of the picture isn't that obvious.
  236. Example: >
  237. :tmenu ToolBar.Make Run make in the current directory
  238. <
  239. Note:
  240. Pay attention to the case used. "Toolbar" and "toolbar" are different
  241. from "ToolBar"!
  242. To remove a tooltip, use the |:tunmenu| command.
  243. The 'toolbar' option can be used to display text instead of a bitmap, or both
  244. text and a bitmap. Most people use just the bitmap, since the text takes
  245. quite a bit of space.
  246. POPUP MENU
  247. The popup menu pops up where the mouse pointer is. On MS-Windows you activate
  248. it by clicking the right mouse button. Then you can select an item with the
  249. left mouse button. On Unix the popup menu is used by pressing and holding the
  250. right mouse button.
  251. The popup menu only appears when the 'mousemodel' has been set to "popup"
  252. or "popup_setpos". The difference between the two is that "popup_setpos"
  253. moves the cursor to the mouse pointer position. When clicking inside a
  254. selection, the selection will be used unmodified. When there is a selection
  255. but you click outside of it, the selection is removed.
  256. There is a separate popup menu for each mode. Thus there are never grey
  257. items like in the normal menus.
  258. What is the meaning of life, the universe and everything? *42*
  259. Douglas Adams, the only person who knew what this question really was about is
  260. now dead, unfortunately. So now you might wonder what the meaning of death
  261. is...
  262. ==============================================================================
  263. Next chapter: |usr_43.txt| Using filetypes
  264. Copyright: see |manual-copyright| vim:tw=78:ts=8:noet:ft=help:norl: