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

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?

Related

Search and replace with the result of a command

In vim, I want to search and replace a text. But I want to replace it with the output of a command.
Given the text:
{ "_template": "foo" }
{ "_template": "foo" }
I want to search and replace that to become:
{ "_id": "239c55fd-538e-485f-8588-83d9735b6819" }
{ "_id": "2ae9f49f-244c-47b0-8f0f-c6c46e860af3" }
The latter is the result of the unix/linux command uuidgen.
It would look something like s:/"_template": "foo"/"_id": "<uuidgen>"/, but I'm unsure what to do at <uuidgen>. The command uuidgen is just an example, but it would be a command that takes no arguments and does not need any stdin passed in (like for example wc or so would).
It needs to call the command for each search/replace again.
Is this possible at all with vim? Or should I better use sed and/or awk instead?
Hum...
Something like that?
:s/"_template": "foo"/\='"_id": "'.trim(system('uuidgen')).'"'
The key is :s-\=

Tab on Expect String concatenation

I'm kinda a novice on Expect, but I can't get over a problem I have with a logging-monitoring script i'm writing.
I've spent hours googling on why I can't get this to work:
puts $redirect [concat "${time}\t" "${context}\t" "$id\t" "${eventtype}" "${eventstatus}\t" "${eventcontext}" ]
The \t char ( it does not work even with other \chars ) is not showing up. No matter how and where I place it, I've tried different stuff:
puts $redirect [concat "${time}" "\t" "${context}" [...] ]
puts $redirect [concat "${time}\t" "${context}" [...] ]
puts $redirect [concat "${time}" "\t${context}" [...] ]
puts $redirect [concat "${time}" \t "${context}" [...] ]
*where redirect is set redirect [open $logfile a]
*where [...] are other strings I'm concatenating, in the same way.
From http://tcl.tk/man/tcl8.5/TclCmd/Tcl.htm#M10
[5] Argument expansion.
If a word starts with the string “{}” followed by a non-whitespace character, then the leading “{}” is removed and the
rest of the word is parsed and substituted as any other word. After
substitution, the word is parsed as a list (without command or
variable substitutions; backslash substitutions are performed as is
normal for a list and individual internal words may be surrounded by
either braces or double-quote characters), and its words are added to
the command being substituted. For instance, “cmd a {}{b [c]} d
{}{$e f "g h"}” is equivalent to “cmd a b {[c]} d {$e} f "g h"”.
[6] Braces.
If the first character of a word is an open brace (“{”) and rule [5] does not apply, then the word is terminated by the matching close
brace (“}”). Braces nest within the word: for each additional open
brace there must be an additional close brace (however, if an open
brace or close brace within the word is quoted with a backslash then
it is not counted in locating the matching close brace). No
substitutions are performed on the characters between the braces
except for backslash-newline substitutions described below, nor do
semi-colons, newlines, close brackets, or white space receive any
special interpretation. The word will consist of exactly the
characters between the outer braces, not including the braces
themselves.
Ironically, I can get this to work:
puts $redirect [concat "${time}\n" "-\t${context}" [...] ]
If I put a char before the TAB, it works, but I can't use it.
Ex output: 2016-06-01 15:43:12 - macro
Wanted output: 2016-06-01 15:43:12 macro
I've tried on building the string with append but it's like it is eating pieces of string due to max buffer char, is it possible?
Am I missing something?
Thanks in advice.
That is what concat does. It eats whitespace.
From the documentation for concat:
This command joins each of its arguments together with spaces after trimming leading and trailing white-space from each of them. If all the arguments are lists, this has the same effect as concatenating them into a single list. It permits any number of arguments; if no args are supplied, the result is an empty string.
#Etan gave you why it's not working for you.
An alternate way to code that is to use format
puts $redirect [format "%s\t%s\t%s\t%s%s\t%s" $time $context $id $eventtype $eventstatus $eventcontext]

vim substitute backslash

I am trying to extract only the function name from a function declaration using vim script.
For testing purposes I am using this simple example:
int func(int a);
In vim script I am extracting the function name by this:
:let a = substitute(getline(line('.')), ".*\(func\).*", "\1", "")
But the backreference is not working. When I echo the variable a with
:echo a
it displays the whole line, i.e. int func(int a);
How to extract only the function name with bacreference or any other method?
Thanks in advance!
Inside double quotes, backslashes must be escaped. Either use single quotes ('\1') or double all backslashes ("\\1").
See :help expr-string for details.

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