Sublime Text 3 Replace with regular expression - sublimetext3

How to add string pieces to existing strings in Sublime Text 3?
Example: I have a text with strings like (23aa67) or (ret457) and I would like to transform them into \as{(23aa67)} or \as{(ret457)}.
Is it possible?

This solution will work if you have at least build number 4107 - you can tell by selecting Help → About Sublime Text.
Open your key bindings by selecting Preferences → Key Bindings. The pane on the right is your user key bindings file, and may simply look like this:
[
]
Position your cursor between the brackets and paste in the following:
{
"keys": ["ctrl+alt+super+a"],
"command": "chain",
"args":
{
"commands":
[
{
"command": "expand_selection",
"args": {"to": "smart"}
},
{
"command": "insert_snippet",
"args": {"contents": "\\as{${0:$SELECTION}}"},
}
]
},
"context":
[
{
"key": "selector",
"operator": "equal",
"operand": "text.tex",
"match_all": true
}
]
}
Here's how it works: The key combo CtrlAltSuperA (where Super is the Windows key) initiates two commands - expand_selection and insert_snippet.
You place your cursor between the two parentheses of your original text, and expand_selection expands the selection to include all of the text plus the opening and closing parens.
The second command wraps the selection - ${0:$SELECTION} - with \as{ at the beginning and } at the end. The "context" at the end only allows the command to run in TeX/LaTeX files. This can be removed if you'd like access to it everywhere.
If you want to change the key binding from CtrlAltSuperA to something else, just be sure that you're not overriding another keybinding. The FindKeyConflicts plugin is great for figuring that out.
Make sure you save the key bindings file when you're done. This shortcut will even work with multiple selections, so you can put multiple cursors in multiple places throughout your text, hit the key combo once, and they'll all be wrapped.

Using regular expression, you can do:
Ctrl+H
Find: (\(.+?\))
Replace: \\as{$1}
Replace all
Explanation:
( # start group 1
\( # opening parens, have to be escaped as it has special meaning in regex
.+? # 1 or more any character, not greedy
\) # closing parens
) # end group 1
Replacement:
\\ # backslash, have to be escaped
as{ # literally
$1 # content of group 1
} # literally
Screenshot (before):
Screenshot (after):

Related

The vscode vim extension does not write real characters?

I have customized the vim-extension in vscode, and for most modes, it executes commands correctly. However If I try to write actuall characters (which are thus no longer commands), it won't. Why is that?
Example:
"vim.insertModeKeyBindingsNonRecursive": [
{
"before": [
"<leader>",
"o"
],
"after": [
"<Esc>",
"i",
"Abcd"
]
},
]
This should only write Abcd, because before that sequence is i, switching into insert mode. (So the <Esc> -> i, is redundant, it is here just as example). The vscode vim extension executes the <Esc> and also the i (becuase I know after that command I am back in insert mode), but will not print the Abcd. Why? Is the extension configured just to execute commands and not to actually print something? How to enable that?
It looks like you just need to split each character of the "Abcd" on to a separate line:
"vim.insertModeKeyBindingsNonRecursive": [
{
"before": [
"<leader>",
"o"
],
"after": [
"<Esc>",
"i",
"A",
"b",
"c",
"d"
]
},
]
As mentioned in the documentation of the project, you are supposed to pass a single key character (single character for normal key and multiple characters for special keys such as arrow keys or escape keys).
Updating after value will solve the issue:
"after": [
"<Esc>",
"i",
"A", "b", "c", "d"
]
You can take a look at the internals of the plugin to understand why it is not working with multiple characters passes in here, specifically NormalizeKey function, which would wrap any string with multiple characters with <>.

Can't change the user key bindings to open sublime in different browsers?

Hey friends I've been using chrome for the development of my websites but know I wanna switch things up a bit. I read this article on how to do it. I did this in the past for chrome and it worked. But when I paste:
[
{ “keys”: [ “ctrl+alt+v” ], “command”: “view_in_browser” },
{ “keys”: [ “ctrl+alt+f” ], “command”: “view_in_browser”, “args”: { “browser”: “firefox” } },
{ “keys”: [ “ctrl+alt+c” ], “command”: “view_in_browser”, “args”: { “browser”: “chrome” } },
{ “keys”: [ “ctrl+alt+i” ], “command”: “view_in_browser”, “args”: { “browser”: “iexplore” } },
{ “keys”: [ “ctrl+alt+s” ], “command”: “view_in_browser”, “args”: { “browser”: “safari” } }
]
in the key bindings user file I get this error?
Error trying to parse file: Expected value in Packages\User\Default
(Windows).sublime-keymap:3:4
Edit: I was told to turn the curly quotes into straight quotes. I did this and while it did fix the issue of saving the file, the error message did not show up. However I am not able to open Firefox with Ctrl + Alt + f?
The reason that this is not working for you is that your JSON is invalid; JSON only allows for straight double quotes, but the JSON you pasted above is using curly quotes:
From https://en.wikipedia.org/wiki/Quotation_mark:
'...' and "…" are known as neutral, vertical, straight, typewriter, dumb, or ASCII quotation marks. The left and right marks are identical. These are found on typical English typewriters and computer keyboards, although they are sometimes automatically converted to the other type by software.
‘…’ and “…” are known as typographic, curly, curved, book, or smart quotation marks. The beginning marks are commas raised to the top of the line and rotated 180 degrees. The ending marks are commas raised to the top of the line. Curved quotation marks are used mainly in manuscript, printing and typesetting.
As a result of this, Sublime's JSON parser (which is lenient in that it allows for extraneous trailing commas and comments that standard JSON disallows) doesn't understand the curly quotes, so it's not finding what it expects at line 3, column 4.
Replacing all of the double quote characters with straight quotes should solve the problem.

Sublime Text: select/edit all occurrences of variable *scoped to function*

Sublime provides ability to:
select/edit all occurrences of a variable (Quick Find All; alt+F3 on Windows)
select each occurrence one-by-one and then edit the summed total (Quick Add Next; ctrl+d on Windows)
What I want:
select/edit all occurrences within a function's scope
note: I've read this related link (Sublime Text: Select all instances of a variable and edit variable name) and didn't see an answer to how editing might be restricted to function scope.
You can create a fairly simple plugin to do this, making use of Quick Find All, and then just removing any selections that are not inside the current function.
From the Tools menu -> Developer -> New Plugin...
Replace the contents of the new tab with the following:
import sublime
import sublime_plugin
class SelectWordInFunctionCommand(sublime_plugin.TextCommand):
def run(self, edit):
view = self.view
prev_sel = [sel for sel in view.sel()]
function_region = next((region for region in view.find_by_selector('meta.function') if region.contains(view.sel()[0])), None)
if function_region:
#word = view.expand_by_class(view.sel()[0].begin(), sublime.CLASS_WORD_START + sublime.CLASS_WORD_END)
view.window().run_command('find_all_under')
sel = [sel for sel in view.sel() if function_region.contains(sel)]
view.sel().clear()
view.sel().add_all(sel if any(sel) else prev_sel)
else:
view.window().status_message('Not inside a function')
instead of using the find_all_under command, use select_word_in_function - you can create a keybinding to do this only when inside a function definition:
{ "keys": ["alt+f3"], "command": "select_word_in_function", "context":
[
{ "key": "selector", "operator": "equal", "operand": "meta.function", "match_all": true },
]
},
Disclaimer: this definitely works with PHP in ST build 3142 and other syntaxes that scope the whole function, but a different approach to detect where the function starts and ends may need to be used for other syntaxes that can't/don't behave this way.

Backslash can not be inserted in Sublime Text 3

I am using Sublime Text 3 on Windows with German keyboard. The backslash \ is assigned to alt gr + ß but this key binding is not properly working. (The cursor goes up by several lines.)
Since alt gr is equivalent to ctrl+alt on Windows, I added the following line in sublime-keymap.
{ "keys": ["ctrl+alt+ß"], "command": "insert", "args": {"characters": "\\"} }
But this is not working, neither.
Is it possible to assign \ to alt gr+ß?
Note:
"keys": ["ctrl+alt+0"] (instead of "keys": ["ctrl+alt+ß"]) works. But I want use "alt gr+ß" for backslash.
"keys": ["ctrl+alt+\u00DF"] is not working.
The following settings works perfect!
In Preferences -> Key Bindings
{ "keys": ["ctrl+alt+["], "command": "insert", "args": {"characters": "\\"} }

Delete till end of current parenthesis block in vim

Say I am editing this json
{
"a": {"language": "python"},
"b": {},
"c": {"language": "java"},
"d": {"encoding": "utf-16"}
}
My cursor is at b of "b": {}. I want to delete till the end of current {} block. So it'll look like,
{
"a": {"language": "python"},
"
}
Looks little odd. But explains what I want.
How can I do that in Vim?
You can use d]}.
From :help ]}:
*]}*
]} go to [count] next unmatched '}'.
|exclusive| motion.
The help also says that this is one of the motion's use case:
The above four commands can be used to go to the start or end of the current
code block. It is like doing "%" on the '(', ')', '{' or '}' at the other
end of the code block, but you can do this from anywhere in the code block.
for your example, d]] works too. It is easier to press.
However, ]} is better, since it works no matter which column the { or } sits on.

Resources