Remove/Add Line Breaks after Specific String using Sublime Text - string

Using Sublime Text 2 - Is it possible to insert a line break/text return after a specific String in a text file e.g. by using the Find ‣ Replace tool?
(Bonus question: Is it possible to remove all line breaks after a specific String)

Here's how you'd do it on a Mac:
Command+F > type string > Control+Command+G > ESC > Right Arrow > line break
and Windows/Linux (untested):
Control+F > type string > Alt+F3 > ESC > Right Arrow > line break
The important part being Control+Command+G to select all matches.
Once you've selected the text you're looking for, you can use the provided multiple cursors to do whatever text manipulation you want.
Protip: you can manually instantiate multiple cursors by using Command+click (or Control+click) to achieve similar results.

Using the Find - Replace tool, this can be accomplished in two different ways:
Click in the Replace field and press Ctrl + Enter to insert a newline (the field should resize but it doesn't, so it is hard to see the newline inserted).
Inside the Find - Replace tool, activate the S&R regex mode (first icon on the left .*, keyboard shortcut is Alt + Ctrl/Cmd + R to activate/deactivate it).
Type \n in the Replace field wherever you want to insert a newline.
Both solutions also work if you want to find newlines, just do it in the Find field.

Edit->Lines->Join Line (Ctrl+J)

You should probably use multiple cursors. See the unofficial documentation, or this nice tutorial. Here's some brief instructions to set you on your way:
Put the cursor on the string of interest.
Type Command+D (Mac) or Control+D (Windows/Linux) to select the current instance of the string.
Type Command+D (Mac) or Control+D (Windows/Linux) to select successive instances of the string.
Alternately, type Control+Command+G (Mac) or Control+Command+G to select all instances of your string.
Now you have multiple cursors, so insert or remove your newline as you please.
(type esc to exit multiple cursor mode.)
Have fun!

Related

How do I substitute in vim editor?

I want to substitute with vim editor a word but only on a portion of a code, that is to say the word "[iteration]" by "[index]". I know how to select the text to replace (visual mode) but I don't know how to apply a substitution on the text selected. The only thing I know to do is global substitution by typing "%s/\[interation\]/\[index\]/g: how can do the same on only selected text?
After you select a block in visual mode, press :, and you'll get a prompt with '<,'>, representing the block you've selected. You can then add instructions to be performed on that block like s/[interation]/[index]/g (without the %!). I.e., you'll have '<,'>s/[interation]/[index]/g. Press Enter and you're good to go.

vim - surround text with function call

I want to wrap some code :
myObj.text;
with a function call where the code is passed as an argument.
console.log(myObj.text);
I've thought about using surround.vim to do that but didn't manage to do it.
Any idea if it's possible ? I
With Surround in normal mode:
ysiwfconsole.log<CR>
With Surround in visual mode:
Sfconsole.log<CR>
Without Surround in normal mode:
ciwconsole.log(<C-r>")<Esc>
Without Surround in visual mode:
cconsole.log(<C-r>")<Esc>
But that's not very scalable. A mapping would certainly be more useful since you will almost certainly need to do it often:
xnoremap <key> cconsole.log(<C-r>")<Esc>
nnoremap <key> ciwconsole.log(<C-r>")<Esc>
which brings us back to Surround, which already does that—and more—very elegantly.
I know and use two different ways to accomplish this:
Variant 1:
Select the text you want to wrap in visual mode (hit v followed by whatever movements are appropriate).
Replace that text by hitting c, then type your function call console.log(). (The old text is not gone, it's just moved into a register, from where it will be promptly retrieved in step 3.) Hit <esc> while you are behind the closing parenthese, that should leave you on the ) character.
Paste the replaced text into the parentheses by hitting P (this inserts before the character you are currently on, so right between the ( and the )).
The entire sequence is v<movement>c<functionName>()<esc>P.
Variant 2:
Alternatively to leaving insert mode and pasting from normal mode, you can just as well paste directly from insertion mode by hitting <ctrl>R followed by ".
The entire sequence is v<movement>c<functionName>(<ctrl>R")<esc>.
You can use substitution instruction combined with visual mode
To change bar to foo(bar):
press v and select text you want (plus one more character) to surround with function call (^v$ will select whole text on current line including the newline character at the end)
type :s/\%V.*\%V/foo\(&\)/<CR>
Explanation:
s/a/b/g means 'substitute first match of a with b on current line'
\%V.*\%V matches visual selection without last character
& means 'matched text' (bar in this case)
foo\(&\) gives 'matched text surrounded with foo(...) '
<CR> means 'press enter'
Notes
For this to work you have to visually select also next character after bar (^v$ selects also the newline character at the end, so it's fine)
might be some problems with multiline selections, haven't checked it yet
when I press : in visual mode, it puts '<,'> in command line, but that doesn't interfere with rest of the command (it even prevents substitution, when selected text appears also somewhere earlier on current line) - :'<,'>s/... still works

PhpStorm - Mark multiple lines

Is it possible to mark multiple lines in PhpStorm?
e.g.
LineA
LineB
LineC
lets say I try to mark LineA and LineC to copy paste them to another place, is this possible?
LineA
LineC
Sure .. but it requires a bit of "precision pointing" and using mouse (not sure if it's easily doable with keyboard only).
Basically -- the idea is to use Multiple Carets functionality:
Place caret at the beginning of first line
Now Alt + Click on the beginning of the other line to create new caret there (shortcut depends on OS -- on Linux it might be different shortcut/combo as that shortcut may used by Desktop/Window manager for own stuff)
Now you have 2 carets on the beginning of Line A and Line C
Just use Shift + End to select text to the end of line -- it will be done in both lines.
Now just Copy + Paste it elsewhere (or whatever you wanted to do).
Use Esc to get rid of carets.
Use multiple cursors. The keyboard shortcuts may vary (those described in the documentation do not work for me on Linux).
Explore clicking around the text while various combinations of the Ctrl, Alt and Shift keys are pressed. On macOS also try Cmd. One of them allows you to place multiple carets in the text (f.e. at the beginning of the lines you want to copy).
After you placed all the carets you need use Shift and the arrows to extend the selection. Use Copy and Paste as usual. Use Escape or click anywhere in the text to deselect and go back to the regular, single-caret, status.

Substitute for vim replace in Sublime

I'm looking for a replacement for vim's "replace with character" command--specifically, I want to be able to select some text and replace each character with some character that I type (difficulty: No "vintage" mode)
Example:
Starting with
I am some text with an arbitrary number: 12358998281
I want an easy way to select 12358998281 and turn it into 99999999999, to make the result
I am some text with an arbitrary number: 99999999999
(in vim, this would be done by moving the cursor to the beginning of 12358998281, selecting with ve, then pressing r9)
I can do this by selecting the text, bringing up the "find" dialog, making sure "in selection" and "by regex" are enabled, searching for ., then typing my character into the resulting multiselect. This is incredibly laborious, however, and it prevents me from doing this process on a multiselect (for example, if 12358998281 exists in multiple parts of the file, I might want to quickly replace all instances of it with 99999999999, rather than performing the process above, getting the substitution, copying it to the clipboard, and then replacing with that).
Does Sublime have a command that acts like vim's "replace" that I can bind to something, or do I have to write a macro to get what I need? Or, am I approaching this from entirely the wrong direction?
A more generalized way of thinking of this is "how can I break a select into a multiselect on all characters", if that helps.
By using this package https://sublime.wbond.net/packages/RegReplace you can create regex patterns and bind them to shortcuts.
Also if there are multiple occurrences of one word, you can put cursor on whatever part of the word and press CTRL+D multiple times. One CTRL+D press will select the word under the cursor, every other press will select next occurrence of the word.
You could also use https://sublime.wbond.net/packages/Expand%20Selection%20to%20Whitespace to expand the selection to whitespace if your word contain some random characters, and then press CTDL+D to select next occurrences of the word.
Edit: With the package regex shortcuts indeed you have to create regexes before binding them. But the CTRL+D does work without it.
I don't see a problem with using "Expand selection to whitespace" and than doing the CTRL+D as I wrote in the answer.
I've checked the usage of visual as you wrote in the question and this solution seems much faster to do. You don't have to place cursor in the beggining of the word as It can be whereever in the word and no find/replace is needed, since you'll multiselect all occurrences by holding CTRL+D for a sec and You'll be free to edit it.
You can also use https://sublime.wbond.net/packages/Expand%20Selection%20to%20Quotes to select text inside quote and combine it with CTRL+D if standard CTRL+D doesn't work with some text, or if select to whitespace selects too much text.
I ended up solving this with a simple (if inelegant) plugin:
import sublime_plugin
import sublime
class MultiSelectWithinSelectedCommand(sublime_plugin.TextCommand):
def run(self, edit):
selection = self.view.sel()
new_regions = []
for selected_region in selection:
if selected_region.empty():
selection.add(self.view.word(selected_region))
for selected_region in selection:
if selected_region.a > selected_region.b:
region_begin = selected_region.b
else:
region_begin = selected_region.a
for pos in range(selected_region.size()):
subregion_begin = region_begin + pos
subregion_end = subregion_begin + 1
new_regions.append(sublime.Region(subregion_begin, subregion_end))
selection.clear()
selection.add_all(new_regions)
Once I've stuck this in my plugins directory, I would bind a command in the keymap file like usual:
{ "keys": ["alt+f"], "command": "multi_select_within_selected" }
(with alt+f chosen arbitrarily), and lo, multi-select on all selected characters with a keypress (after which, I can press my replacement character).

Delete lines with highlighted text / delete highlighted text

Does anyone know how to delete:
lines with highlighted text
all highlighted text self
(highlighted text (p.e. after a search) not selected text)
Is there a command which search all highlighted text and delete the line?
(independent which search command or function I used to highlight text)
the g/pattern/d command does not always delete the highlighted text
p.e. /^\(.*\)\(\n\1\)\+$ --> highlight all double lines
but g/^\(.*\)\(\n\1\)\+$/d --> does NOT delete all double lines
Well, you can delete the searched pattern this way:
:%s/<pattern>//gc
And you can delete the whole line with the searched pattern this way:
:g/<pattern>/d
In addition to sixfeetsix' answer:
to delete all lines NOT containing <pattern>, type :g!/<pattern>/d or :v/<pattern>/g
to avoid having to type <pattern> after :g/, type :g/CTRL-r//d which inserts the content of the search register (CTRL-r/ means register /) into your command being typed.
how to delete: 2) all highlighted text self
You could use search-and-replace (substitute) to do this.
It is generally used like this:
:%s/your_search_here/your_replacement_here/gc
More specifically, replace your search results with nothing (to remove them):
:%s/your_search_here//gc
Omit the c at the end to replace all without confirmation.
Type :help :s for more info.
how to delete: 1) lines with highlighted text
To delete whole lines, you could either do a substitute, and just match the whole line with a regular expression (%s/^.*your_search_here.*\n//g), or you could use the multiple repeats (multi-repeat) feature.
It is generally used like this:
:g/your_search_here/[cmd]
More specifically, combine it with the normal command you use to delete a line (d):
:g/your_search_here/d
Type :help :g for more info.
Tips:
An easy way to get your query right before doing your substitute is to do your search in command mode rather than the default mode.
Instead of:
/your_search_here
Type:
:/your_search_here
Then you can go to command mode (:), hit the up key to bring up your last search, and edit the line to convert it to a substitute.
From this SuperUser answer:
You can use gn in version 7.4 onwards (and gN to go backwards). It replaces the v//e trick.
Search forward for the last used search pattern, like with `n`, and start Visual mode to select the match.
See :help gn or this Vimcast for more information.
I guess it's the essentially the same question as this:
Vim: when matching string across multiple lines using \_. in regex, yank command only works for the first line
It looks like a bug in Vim.

Resources