Remove everything after first space occurs in sublime text 3 - sublimetext2

does anyone know how to remove everything after the first space occurs in sublime text 3.
For example i have this file:
abcde fghi jklm
And i would like to have this output:
abcde

After searching a bit this is what worked for me:
In regular expression mode, search for:
\t.*
And replace with
Nothing

And a few years later...
Tested on macOS:
Use ⌥+⌘+F to popup the Find/Replace dialog.
Make sure the .* button is on (this enables the Regex mode).
Find: \ .*
Replace: don't type anything here
Obs: the \ in the regex is not strictly necessary, I just added it to emphasize the fact that there is a space in the string before the dot.
Obs2: the answer from the OP works only if you have tabs instead of spaces.

Press CtrlH to replace with regex, and use:
Find What: ^(\S+)\s?[^\n]*\n
Replace With: \1

Related

Replace arbitrary number of plus signs with spaces

I have more than 2000 <img> tags and I want to replace alt text for each one of them. Alt text are like:
alt="pinblock"
alt="Rich+Austin+shop+4"
alt="hot+dry+sun+az"
I want a quick way to replace all '+' with space (' '), hence I'm using regex to fix this.
I've tried this so far:
Find what: alt="(\D+)[+](\D+)[+*](\D*)[+*](\D*)[+*](\D*)[+*](\D*)[+*](\D*)[+*](\D*)"\s
Replace With: alt="\1 \2 \3 \4 \5 \6 \7 \8"
I know I'm doing something wrong, please help.
Complete string would be:
<img border="0" height="111" src="https://1.bp.blogspot.com/-WL5_jMT96p4/U8ILVU9D-mI/AAAAAAAAGeI/rP_RJccbhj8/s1600/hot+dry+sun+az.jpg" alt="hot+dry+sun+az" width="200" />
Just to demonstrate, I have tested this out and repeated pressing of the Replace All button works. First, press the Regular expression radio button, then:
Find what: (alt=\"[^+"]*?)\+([^\"]*?")
Replace with: \1 \2
This is of course not fool-proof, but it should work as long as you have no pathological data.
NOTE: My first version had a bug in that it would change alt="hot+dry+sun+az" width="200+200" to alt="hot+dry+sun+az" width="200 200", which is a good example of why one should not use regex to process HTML. I think this task can probably be done in a few lines of JavaScript with zero danger of getting tripped up as I did above, but that's another question for another day!
NOTE 2: My second version also got Zalgoed.

Sublime Text regex to find and replace whitespace between two xml or html tags?

I'm using Sublime Text and I need to come up with a regex that will find the whitespaces between a certain opening and closing tag and replace them with commas.
Example: Replace white space in
<tags>This is an example</tags>
so it becomes
<tags>This,is,an,example</tags>
Thanks!
You have just to use a simple regex like:
\s+
And replace it with with a comma.
Working demo
This will find instances of
<tags>...</tags>
with whitespace between the tags
(<tags>\S+)\W(.+</tags>)
This will replace the first whitespace with a comma
\1,\2
Open Find and Replace [OS X Cmd+Opt+F :: Windows Ctrl+H]
Use the two values above to find and replace and use the 'Replace All' option. Repeat until all the whitespaces are converted to commas.
The best answer is probably a quick script but this will get you there fairly fast without needing to do any coding.
You can replace any one or more whitespace chunks in between two tags using a single regular expression:
(?s)(?:\G(?!\A)|<tags>(?=.*?</tags>))(?:(?!</?tags>).)*?\K\s+
See the regex demo. Details
(?s) - a DOTALL inline modifier, makes . match line breaks
(?:\G(?!\A)|<tags>(?=.*?</tags>)) - either the end of the previous successful match (\G(?!\A)) or (|) <tags> substring that is immediately followed with any zero or more chars, as few as possible and then </tags> (see (?=.*?</tags>))
(?:(?!</?tags>).)*? - any char that does not start a <tags> or </tags> substrings, zero or more occurrences but as few as possible
\K - match reset operator
\s+ - one or more whitespaces (NOTE: use \s if each whitespace must be replaced).
SublimeText settings:

Insert same code around different filenames

I am making a small online database that is accessible through the form of checkboxes for download. I was wondering if there was some way to list all of the filenames available for download in Sublime Text 2 and insert the same code around each filename?
Everything is functional, it would just save me a lot of repetitive copy and pasting if there is a faster way to do this.
Use SublimeText Find & Replace. Click the Regex button (it looks like a * to the left of the search box)
In the Find box, insert: (^.*$)
In the replace box: [yourcode]$1[yourcode]
Where [yourcode] is what you want to insert into the box.
So, if you want to make them all <li> then your replace box would be:
<li>$1</li>
Remember to use escape \ characters where they are needed, in case you need to insert restricted characters.
^ - beginning of a new line.
. - wildcard
* - any number of the previous character in the sequence (in this case a wildcard, so any text)
$ - the end of a line
() - denotes a block, it's how the $1 knows what text to put in it's place.
Sublime Text Search and Replace
Use search/replace on a text editor with regular expressions.
^ and $ represent the beginning and end of a line - thus allowing you to easily surround each line with the appropriate text.
Sometimes you can copy the newline character (as in copy the end of one line to the beginning of the next line), and replace that with whatever text you need.
You could always use the regular expression search / replace feature in Notepad++.

Emmet - Wrap with Abbreviation - Token that represents the wrapped text i.e. {original text}

I'm attempting to convert a list of URLs into HTML links as lazily as possible:
www.annaandsally.com.au
www.babylush.com.au
www.babysgotstyle.com.au
... etc
Using wrap in abbreviation, I'd like to do something like: a[href="http://${1}/"]*
The expanded abbreviation would result in:
www.annaandsally.com.au
www.babylush.com.au
www.babysgotstyle.com.au
... etc
The missing piece of the puzzle is an abbreviation token that represents the text being wrapped.
Any idea if this can be done?
If they are already on their own lines (which in the question, they look like they are), a simple Find and Replace with RegEx turned on will work. The Params are as follows:
Find What:
(.+)
Replace With:
$1
Before
After
Sergey from Emmet was kind enough to point me in the right direction. The $# token contains the original content:
a[href="http://$#/"]*>{$#}
By specifying $# as the href attribute, the original content is no longer 'wrapped' and must be be reinserted via {$#}.
http://docs.emmet.io/actions/wrap-with-abbreviation/#controlling-output-position

Align indent to parenthesis after linebreak in Sublime Text

I like to keep my lines below 80 columns, so I often want to refactor a line that looks like this:
object.function(a_long_argument, another_long_argument, and_a_third)
to this:
object.function(a_long_argument,
another_long_argument,
and_a_third)
But when I press Enter after the first "," in Sublime it just linebreaks and indents the cursor a few spaces. I want it to align to the paranthesis or [] or {} that I am in, like Emacs does so beautifully.
Is there an option for this? Is there a plugin for this? Do I have to write my own?
I have tried searching for it, but I have not found anything.
EDIT:
Even better would be a shortcut or plugin or something for selecting a few rows, or the entire buffer, and let it try to auto-linebreak at good spots. Refactor comments too. If it has to be language specific, I want it primarily for Python and C++.
Sublime's indent_to_bracket will wrap the cursor for you. Just add the following line to either your User/Preferences.sublime-settings or User/Python.sublime-settings file:
"indent_to_bracket": true
Unfortunately this currently only seems to work with parentheses, curly braces and square brackets still wrap to the previous line indent.