Sublime text: Merge multiple adjacent white spaces into one - text

How to do this in sublime text
"aaa bbb ccc ddddddd "
should get converted to
"aaa bbb ccc ddddddd "

This can also be done with a regex search and replace. First, select the text you want to alter. Next, click on Find -> Replace.... Make sure the Regex and In selection buttons are selected. To visualize what you're going to replace, you can also select the Highlight matches button as well. In the Find What field, enter (\s){2,}, and in the Replace With field, just enter a space. Click Replace All, and all instances of two or more whitespace characters will be replaced with a single space.

Create a snippet:
<snippet>
<content><![CDATA[
${SELECTION/\s{2,}/ /g}
]]></content>
</snippet>
Then create a keybinding to call that snippet in your Preferences -> KeyBindings - User file:
{ "keys": ["ctrl+shift+z"], "command": "insert_snippet", "args": { "name": "Packages/User/Snippets/test_snippets/regex_whitespace.sublime-snippet" } },
Then, select text on a line, or multiple lines, and use the keymap.

Related

vim how to cut text and insert into a new line

I want to do this:
1 printf("hello world\n");
2 bool func() //I want to cut the comment and insert between line 1 and line 2
3 {
4 //to do
5 }
And, I want to cut the comment after // and to insert between line 1 and line 2. As I know, by using Ctrl+v, vim is transformed into VISUAL mode, and you can select the text, and by pressing d, you can cut the text, but How can I insert the text that be cutted and insert into a new line? Is there any command?
And I think after cutting the text, and you can press o and press esc and press p to paste, but it seems to tedious. Are there any better command?
Thank you in advance!
If you want to save keystrokes you can insert the " register in insert mode by using <c-r>".
Also you can delete from the cursor to the end of the line with D, which places the deleted section in the " register.
So if your cursor was on the first character of the comment you can use
DO<c-r>"
to transfrom
printf("hello world\n");
bool func() //I want to cut the comment and insert between line 1 and line 2
{
//to do
}
Into
printf("hello world\n");
//I want to cut the comment and insert between line 1 and line 2
bool func()
{
//to do
}
All you have to do is clean up the trailing white space after func().
Take a look at :help i_CTRL-R to learn more about <c-r> in insert mode.
Another solution beside the one suggested by FDinoff:
D at the first slash(cuts the comment until end of line)
And run:pu! (put the content above the current line.)

Sublime Text 2, can't indent double quoted strings in python

While editing Python code, if I have a double quoted String on a single line, for example:
my_var = [
"string1",
"string2"
]
and I try to indent "string1" with the TAB key, it doesn't indent it, but instead it get the cursor inside the quotes.
This does NOT happen if I use single quotes.
To investigate I put this in the console:
sublime.log_commands(True)
And whenever I try to perform the previous action I get:
command: move {"by": "characters", "forward": true}
I suppose it is the right command but there's something in the way that prevents to be executed. Any clue?

Repeat match pattern and replace

I'd like to repeat a search and replace as below:
Example:
set_path 1 -start -from [get_obj { A_1[0] B_2[1] .... Z_n[100] }]
replace to
set_path 1 -start -from [get_obj {xyz/A_1[0] xyz/B_2[1] .... xyz/Z_n[100]
hit Esc, and type:
:%s/array/xyz\/array/g
:%s/ \(\u\)/ xyz\/\1/g
\1 means the content matched in the first brackets.
It means replace every space and a uppercase letter to xyz and the uppercase letter.
To make this work with variably named array names, you could do something like this:
s:\v([^[ {]+\[[0-9]+\]):xyz/\1:g
\v turns on "very magic" regular expressions, see :help /magic.
[^[ {]+ ensures the only strings not starting with a bracket, space or curly brace are matched.
\[[0-9]+\] ensures that it "looks" like an array reference.

Vim: how to use ci (change inside) with multiple occurrences per line

In a line like this:
something "one!", "and two", "and three", "and more"
How should I use the command ci" to replace "and three"?
I have tried some made up combos with no luck.
First navigate to the desired substring:
/three
or in the line:
f";;;;
(which moves to the next " 5 times)
or
f,;
Then, ci" zaps the string leaving the cursor for insert inside the empty quotes
Move into substring:
2fa
Then replace and three:
ci"

How to replace text between quotes in vi

Say I have this line of code:
$query = "SELECT * FROM table";
Is there a command in vi/vim which can instantly delete everything between quotes and position the cursor between them so I can start typing?
Use ci", which means: change what inside the double quotes.
You can also manipulate other text objects in a similar way, e.g.:
ci' - change inside the single quotes
ciw - change inside a word
ci( - change inside parentheses
dit - delete inside an HTML tag, etc.
More about different vim text objects here.
You can select between quotes and then delete (d), change (c) etc. using
vi"
Similarly, you can substitute braces, brackets, XML elements etc. thus:
vi(
vi{
vit
or to simply change/delete, do the corresponding di", ci" etc. Substituting a for i will encompassing the surrounding elements (so you mark or change the brackets and contents, for example)
I've made a plugin vim-textobj-quotes: https://github.com/beloglazov/vim-textobj-quotes
It provides text objects for the closest pairs of quotes of any type and supports quotes spanning multiple lines. Using only iq or aq it allows you to operate on the content of single ('), double ("), or back (`) quotes that currently surround the cursor, are in front of the cursor, or behind (in that order of preference). In other words, it jumps forward or backwards when needed to reach the quotes.
It's easier to understand by looking at examples (the cursor is shown with |):
Before: foo '1, |2, 3' bar; after pressing diq: foo '|' bar
Before: foo| '1, 2, 3' bar; after pressing diq: foo '|' bar
Before: foo '1, 2, 3' |bar; after pressing diq: foo '|' bar
Before: foo '1, |2, 3' bar; after pressing daq: foo | bar
Before: foo| '1, 2, 3' bar; after pressing daq: foo | bar
Before: foo '1, 2, 3' |bar; after pressing daq: foo | bar
The examples above are given for single quotes, the plugin works exactly the same way for double (") and back (`) quotes.
You can also use any other operators: ciq, diq, yiq, viq, etc.
Please have a look at the github page linked above for more details.
An addition to Brian's answer, you can also p(paste) and y(yank) the new value, so if you want to replace the value inside quotes with another value, you could do yi" on the selection that you want to copy, vi" to select the area that you want to replace and then just p to properly replace the value.
From already inside the quotes you can do
di"
Read it as delete inside "
The chosen answer is suitable ONLY for ViM but NOT for vi. The question is inaccurate as well because the author did not mention what is initial position of the cursor. If we assume that the cursor is inside the double quotes then for vi the answer will be:
T"ct"
Where:
T" - move back just after the " character
c - change command
t" - provide end position for c command, where it should stop erasing characters, in other words the range to change

Resources