Jump to content

User:Guywan/Scripts/InsertShortcuts

From Wikipedia, the free encyclopedia




User script
Insert Shortcuts
DescriptionAllows the insertion of user-defined tags into the edit area.
Author(s)Guywan
StatusBeta
First releasedMarch 20, 2020; 4 years ago (2020-03-20)
Version01.04.20
UpdatedApril 1, 2020; 4 years ago (2020-04-01)
SkinsGreen tickY All skins supported.
SourceUser:Guywan/Scripts/InsertShortcuts.js

Insert Shortcuts

[edit]

Do you spend a lot of time wrapping text in formatting, tags, or templates? Unless you're a WikiGnome, this is probably tiring. Insert Shortcuts is what you need! This script maps the insertion of user-defined text to Alt+1 through Alt+0 (⌥ Option+1 through ⌥ Option+0 on Mac). It is only usable while source editing. Bold and italics are defined by default, and are mapped to Ctrl+B and Ctrl+I, respectively (+B and +I on Mac).

This script will insert text into the edit area based on specially defined rules. If you are not selecting anything, the script will expand your selection to the word at the caret position. Before using the script, you must define your tags at Special:MyPage/InsertShortcutsSettings.js. It's a JS file so only you can edit it. Ignore the warnings when saving.

Here is an example tag definition file:

{{useful template|_}}
<useful tag>_</useful tag>
<code>_</code>
<pre>\n_\n</pre>

The tags are numbered starting at 1 from the top. The _ (underscore) defines where the text you are selecting goes, and the \n expands to an actual 'newline'.

Start Keys Result
Lorem ipsum dolor sit amet. Alt+1 Lorem {{useful template|ipsum}} dolor sit amet.
Lorem ipsum do|lor sit amet. Ctrl+B Lorem ipsum '''dolor''' sit amet.
Lorem ipsum dolor sit amet. Alt+3 <code>Lorem ipsum dolor sit amet.</code>
Pre|stuff Alt+4
<pre>
Prestuff
</pre>
The vertical bar | denotes the caret position.

If you want the tag definition to span multiple lines (in the definition file), you can escape newlines with a backslash \, like so:

{{long template\
|param1\
|param2\
|param3\
|param4\
}}

When you insert this tag, it will become: {{long template|param1|param2|param3|param4}}. If you want it to span multiple lines in the definition file and in the edit area when it is inserted, you need to use \n\, like so:

{{long template\n\
|param1\n\
|param2\n\
|param3\n\
|param4\n\
}}

The 'tag' definitions can take any one of these forms:

  • x_y – Asymmetric wrap.
  • x_ – Prepend.
  • _x – Append.
  • x_x – Symmetric wrap.
  • x – Effectively replaces your selection.
  • _ – Take my word for it, this does nothing. Seriously.

Cross-wiki support

[edit]

If you are using this script on multiple projects and would like to load tag settings from a specific project, there are two options available.

!!REDIRECT

[edit]

You can redirect the tag parser to a different project via the !!REDIRECT directive. As an example, see v:User:Guywan/InsertShortcutsSettings.js, where I !!REDIRECT to English Wikipedia. Double redirects won't work.

Examples:

  • !!REDIRECT commons.wikimedia
  • !!REDIRECT fr.wikibooks
  • !!REDIRECT he.wikipedia

Notice that you only specify the first part of the domain, without .org. The page should contain no other text except the !!REDIRECT directive and the project indentifier.

window.us_InsertShortcuts_loadFrom

[edit]

You can set the window.us_InsertShortcuts_loadFrom attribute where you are importing the script.

Examples:

  • window.us_InsertShortcuts_loadFrom = "commons.wikimedia"
  • window.us_InsertShortcuts_loadFrom = "fr.wikibooks"
  • window.us_InsertShortcuts_loadFrom = "he.wikipedia"

This works very similarly to the !!REDIRECT directive, except that you only need to create an InsertShortcutsSettings.js page on the project you are loading from.

Installation

[edit]

To use this script, add the following to your common.js (or other):

mw.loader.getScript("/w/index.php?title=User:Guywan/Scripts/InsertShortcuts.js&action=raw&ctype=text/javascript"); // Backlink: [[User:Guywan/Scripts/InsertShortcuts.js]]