-
-
Notifications
You must be signed in to change notification settings - Fork 286
Substitute URLs and text during commands
Vimium C has an advanced option named Auto substitution of various text.
It defines rules to match text fragments in URLs and normal text,
and magically replace them with different text (and even increased/decreased numbers).
Every line means a substitution rule, if only it starts with A-Z, a-z or non-ASCII characters.
A basic pattern is just like the s command in sed scripts: keys/regexp/replacement/flags[,actions...].
- a
keysprefix includes 1~6 characters ofa-z(ignoring cases) and non-ASCII letters, and it means 1~6 aliases of a rule. A special rule is, anscharacter means bothcandp- since v1.93,
keysmay includes0-9and_ - some commands of Vimium C will only use
a-zas its default sed keys, so all others are customizable
- since v1.93,
- the
/character may be uniformly replaced by any other single ASCII character which doesn't match/[\x00-\x20A-Za-z\x7f]/. And, just like/insedscripts, the/character (or whatever other character is used in its stead) can appear in the regexp or replacement only if it is preceded by a\character. For example, you may use@,=, and~ - the
regexpsection (along with theflagssection) is used to construct a JavaScript RegExp (valid on your browser) - the
replacementis used as thenewSubstrparameter during replacement, just like https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#specifying_a_string_as_a_parameter - the
actionscan be omitted, and it's a list of Vimium C's substitution conditions and actions joined by,
Here're some valid actions (ignoring cases):
-
host=<exclusion-pattern>can be used to filter prefix of an input URL (text). If ahostdoes not match, then it will be skipped even if its aliases match. -
matchedmeans to return its instantiatedreplacement, a first capture group, or whole matched string, instead of replacing result, as the input of (other) actions. This is an unique action. Since v1.92.1, the=<pattern>part is case-sensitive. This is an unique action. -
base64(akaatob) andbase64-encode(akabtoa) are used asatob()andbtoa()in JavaScript -
decodeanddecode-compare used as URL decoding and URL component decoding -
encodeandencode-compencode characters which are neither Unicode letters nor numbers -
encode-allandencode-all-compencode characters which are neither Unicode letters nor numbers -
unescapedecodes\x**,\u****,\t,\nand\r -
lower,upperandnormalizeconverts text usingString.toLocaleLowerCase,toLocaleUpperCaseandnormalize -
capitalize,capitalize-all,camelandhypermeans to convert some words to a different style -
reversecan reverse all characters in text (supporting surrogate pairs like emoji) -
return(only since v1.92.1) means to ignore any further rules and return at once
Most actions work after replacing and in the order of definitions,
while only host and matched take effects before or when <regexp> works,
Actions names also support camel mode, like encodeAll.
u@/wiki/?$@@g,host=github.com
# will make `goUp` skip home page of a Wiki.
r@^https://github\.com/[^\s\/?#]+/[^\s\/?#]+(?=/)@@,matched,return
# will make `goToRoot` stop at project home, if only current tab is a child page, like an issue.
A key mapping line may refer to some substitution rules using an option named sedKeys,
and it should be a string including a list of aliases.
Vimium C executes rules following the order of their definitions.
A key mapping may also use sed=<substitution-rule> to define and use temporary rules.
In this situation, keys prefixes will be ignored, and different rules should be joined using (<space>).
Some commands have default sedKey:
- when copying text, use
c; while usepduring pasting from clipboard -
goNextandgoPrevioususen -
goUpusegto modify (correct) upper URLs -
goToRootuserto modify source URLs, and if a different URL is returned, then use it directly - when selected text or link title is used as URL and opened, use
t - when on Vomnibar input query is opened (no suggestion is selected), use
o - when opening image on
Vimium C Display, image URL will be converted usingi
Commands like openUrl and createTab can create new URLs using information of a current tab,
like URL, host, title and even tabId.
To do this, openUrl needs both url and url_mask options:
-
urlmeans a URL template -
url_mask,host_mask,title_maskandtabId_maskpoint out how to fill the template - if a mask doesn't have a value, then it will become an empty string
-
url_maskmust exist and be a string. If it's not needed, please make it an empty string usingurl_mask= - if
url_maskis%sor$s, then it will be replaced by an encoded URL; otherwise, by a raw URL - tab title will always be encoded
For example,
map xxx openUrl url="http://webcache.googleusercontent.com/search?q=cache:$s" url_mask="$s"
will open a cached version (on Google's server) of a current tab (in a new tab, by default).
goNext and goPrevious support further substitution to increase / decrease numbers in URLs.
They find ${N/[start]:[end]:[step]} sections in result text of Auto substitution of various text,
increase (or decrease) N by step, and use result numbers to construct a new URL.
-
Nmeans old page number in a URL -
[start]:[end]:[step]is just a similar syntax in Python, and can be used to limit result number-
startandendshould be non-negative integers, whilestepcan also be a negative integer
-
-
${prefix and}are used to recognize number sections to update -
/can be replaced with,,#and@
For example, when with a rule in Auto substitution of various text of:
n@tid=(\d+)@tid=$${$1/::2}@
"tid=123" in a source URL will:
- become "
tid=${123/::2}" (note "$$" in<replacement>means a single "$" character) - then
goNextcalculatesmax(0, min(123 + 2, +Infinity))and gets125 - become "
tid=125"
copyWindowInfo supports customizable formats for copied text.
Its format parameter means how to format information of a tab,
and join parameter will join strings of tabs by given string, using \n or in JSON.
By default, format is ${title}: ${url}, and you may use other fields
in the Tab class.
URLs are encoded, unless copyWindowInfo has another parameter of decoded (aka decoded=true).
When join is not defined, false or true, strings are joined using \n,
and appended with a tail \n when join is not false.
When it's json (or json*** since v1.92.1), strings are converted into an JSON array.
When it's other string, it will be used as the separator.
There's also a string parameter named type to specify where to copy information:
-
window, its default value, means all tabs in a current window -
browsermeans tabs in all windows which have a same incognito state with a current window -
tabworks with the command's count prefix, to select some tabs -
urlandtitlemeans only URL or title of a current tab, soformatandjoinwill be ignored -
framemeans URL of an active iframe in a current tab
Since v1.92.1, copyWindowInfo supports a string parameter named filter,
some words joined by , or (<space>), to filter tabs by URL parts or titles:
- one word can be
url,urlWithHash,host, to decide what URL part to use - the other word can be
title, and if it exists, also filter by tab titles