Note: I have Emmet installed onto Sublime Text 2
In my workflow I have a lot of projects were I have to put underscores in between every word in a sentence. Is there a snippet I could build in sublime text 2 to do this for me with a tab trigger of some selected text?
You can easily create a snippet with this functionality by using regular expression–based snippet substitutions. The only code you need is this:
<snippet>
<content><![CDATA[
${SELECTION/\s/_/g}
]]></content>
</snippet>
You can then select a sentence, run this snippet from the command palette, and all of the spaces within will be substituted with underscores. You might have to adjust the regex (currently just \s) depending on what amount of whitespace you want to replace.
You can always use Find and Replace. Highlight the text you want and hit Ctrl-H (or Command-H on Mac) to open the Find and Replace box. Click the right-hand button on the top row to choose In Selection, and maybe the bottom right-hand button for Highlight matches if you want. Type a space in the Find What: box, an underscore in the Replace With: box, and hit Ctrl-Alt-Enter to Replace All (or Ctrl-Shift-H to Replace one at a time).
To use a snippet, do the following. Select Tools -> New Snippet... and put the following in it:
<snippet>
<content><![CDATA[${SELECTION/\s/_/g}]]></content>
</snippet>
Save it as Packages/User/replace_space_with_underscore.sublime-snippet. Then, open Preferences -> Key Bindings - User and put in the following:
[
{ "keys": ["ctrl+shift+-"], "command": "insert_snippet", "args": { "name": "Packages/User/replace_space_with_underscore.sublime-snippet" } }
]
(If you already have custom key bindings, just put in { "keys": ["ctrl+shift+-"], "command": "insert_snippet", "args": { "name": "Packages/User/replace_space_with_underscore.sublime-snippet" } } at the end, and remember to put a , after the one just above it.)
Save both files, and now you can highlight whatever text you want, hit Ctrl-Shift--, and replace the whitespace with underscores. If you just want spaces (no tabs or newlines), replace the "\s" with "\" (backslash space). This regex will replace multiple spaces with the same number of underscores. It gets a bit more complicated if you only want exactly one space, or exactly one underscore, but it's doable.
Related
This question already has answers here:
VSCode - wrap selected text in "<mark></mark>"
(2 answers)
Closed 2 years ago.
I'm using VS Code to edit markdown files and I'd like to create some new shortcuts, such as to highlight or underline selected texts. This can be achieved by surrounding selected text by <mark>TEXT</mark> or <u>TEXT</u>. And I want to create new shortcuts to add these tags around selected text, just like adding **TEXT** by using CTRL+B.
I know how to create snippets but don't know how to write the command of these shorcuts.
This is very simple in VSCode because it has Emmet integrated into it.
You just select the text, open the command pallet (CTRL SHIFT P) and select Emmet: Wrap with Abbreviation and write your tag and it will automatically wrap the test in that tag. you can also use Emmet class and tag syntax too.
Here is a demo:
You can set the keyboard shortcuts for this action too in VSCode. In the Keyboard Shorcut Screen you can search for Emmet wrap and change them.
this is what i can recomment as a VS Code user.
please add this snippet,
"MYmARK": {
"scope": "",
"prefix": "my mark",
"body": [
"<mark>$1</mark>"
],
"description": "mymark"
},
Then.
since you are selecting that text,
you can cut that and write "mymark" so the snippet will show. and hit enter
then paste your text and it will directly insert where $1 is located.
Due to a new coding style that I've been having to use I'm required to use tabs in the beginning of lines but spaces everywhere else to align things.
Is there a way to customize notepad++ to only replace tabs with spaces if it's not at the beginning of a new line?
Just as an example of what I mean I'll use this bit of 'code':
function someFunction():
while(true):
veryLongCodeStuff() // Some comment
shortCode() // Aligned comment
Which I would have to write like this (where \t = tab and a "." represents a space):
function someFunction():
\twhile(true):
\t\tveryLongCodeStuff()..// Some comment
\t\tshortCode()..........// Aligned comment
To convert existing files, I would suggest a two step approach:
replace all tabs with spaces (this can be done with Edit -> Blank operations -> TAB to Space )
replace spaces at the beginning of lines: do a regular expression find/replace like this:
Open Replace Dialog
Find What: ^([\t]?)( ){4} instead of 4 use the number of spaces you have configured as tab width
Replace With: \1\t
check regular expression
click Replace All: as each Replace All replaces only one indent level for all lines: repeat until the status bar of the find dialog tells you, that no more replacements were done (just keep Alt-A pressed for a second or two)
Notepad ++ (REGEX); Invert Selection,
The following 2 different codes, it works very nicely. :)
now I want to combine these two different code!
\bhttps?:[^)''"\s]+.(?:jpg|jpeg|gif|png)
https://codereview.stackexchange.com/questions/20126/regex-to-get-all-image-links
^((?!hello).)*$
notepad++ Inverse Regex replace (all but string)
So you want to find and remove all rows not containing an image URL specified by the regular expression \bhttps?:[^)''"\s]+.(?:jpg|jpeg|gif|png)?
You can combine the two regular expressions like this:
^((?!\bhttps?:[^)''"\s]+.(?:jpg|jpeg|gif|png)).)*$
If you replace the lines with an empty string, you can then use the menu option "Edit - Line Operations - Remove Empty Lines" to remove the empty lines.
Another option is to use bookmarks:
Select the "Mark" tab from the replace dialog and enter your first regular expression (the one that finds the image URL's). Check "Bookmark lines" and click "Mark all". From the menu, first select "Search - Bookmark - Inverse Bookmark", and then "Search - Bookmark - Remove Bookmarked lines" to remove the lines.
I am trying to replace in Notepad++ using the Replace module, the below paragraph in html (i have 30 html file, and need to replace the below in all of them)
<script type="text/javascript">
<!--
var slideInterval=20000;
var slideTransition=3500;
var slideArray=["/background1.jpg","background2.jpg"];
jQuery.fx.interval=33;
// -->
</script>
But Notepad++ doesn't let me replace unless it's a line instead of a paragraph, and if i put everything on one line to replace, i will have another problems to worry about in my html.
I hope you have a work around on that.
I found a good way to use a multi-line "find" or "replace". I just copy pasted the paragraph into the Ctrl+H "find" field, then brought another paragraph and pasted it into the "replace" field. Notepad++ will show a tabbed space that means a line break. And voila, you can "Replace in all open documents" with just a single click.
N.B.: the "copy" operation should be within Notepad++, otherwise it would paste only the first line in either fields.
Update:
To be clearer about my answer, i found out that Notepad++ will let me only Paste once. That means, if i Copy a paragraph, i can paste it WITH its line break in the "find" field for example but if i paste it another time in the replace field, it will paste only the first line. Hence, no more than 1 "paste" operation is allowed into the Ctrl+H box in case i want to "paste" the line break.
So, in order for this to be done, first, i select any text i want and Ctrl+C on it, then, i go for the paragraph to be found, i just "Select" it and hit Ctrl+H: Notepad++ automatically shows the already selected text into the "find" field. Secondly, we "Paste" the text that's already in our clipboard into the "Replace" field. And the line breaks are here!
In brief: Select text --> Ctrl+C --> Select text --> Ctrl+H --> Ctrl+V in "replace field"
I think I found a guide that describes what you're looking for. The author has examples and the results, and some multi-line replacements are included. You should be able to extrapolate what he does over multiple files by clicking "Replace All in All Opened Documents".
http://markantoniou.blogspot.com/2008/06/notepad-how-to-use-regular-expressions.html
I have a multitude of snippets for each tag in the language I am currently programming in. I want to share these with some of my co-workers but I don't want to send them like 30 snippet files. Is there a way to condense these into one file (apart from zipping them, sending the zip and then having them unzip it).
You can use a .sublime-completions file. These are JSON-formatted files that contain the target scope at the top, then a series of completions with a trigger and contents. For example, the following snippet
<snippet>
<content><![CDATA[function ${1:function_name} (${2:argument}) {
${0:// body...}
}]]></content>
<tabTrigger>fun</tabTrigger>
<scope>source.js</scope>
<description>Function</description>
</snippet>
can be turned into this completion:
{
"scope": "source.js",
"completions":
[
{ "trigger": "fun", "contents": "function ${1:function_name} (${2:argument}) {\n ${0:// body...}\n}" }
]
}
Use \n for newlines and \t for tab characters in the "contents" section. Double quotes need to be escaped as well. Also, you can use \t to separate the trigger from a brief description on what the completion is about, it will be displayed right-aligned and slightly grayed and does not affect the trigger itself:
{ "trigger": "fun\tFunction", "contents": "function ${1:function_name} (${2:argument}) {\n ${0:// body...}\n}" }
Since this is JSON, to add multiple completions just put a comma , after the closing curly brace } of the completion, and put your next one on the next line. The final line should not have a final comma.
Good luck!