How to apply text object commands to one level upper? - vim

I have a template tag in my HTML like this:
<span>Hello, {{ customer.name| truncatewords: 1, "" | capitalize }}</span>
I want to cut the tag. If I use da{ when cursor inside of the template tag, it looks like this:
<span>Hello, {}</span>
This is what I want:
<span>Hello, </span>
And the {{ customer.name| truncatewords: 1, "" | capitalize }} part must be yanked of course.
I tried da{{ but it doesn't works. How I do this?
ps: I have problem with y c or v commands too of course.

You can use a count before the text-object:
d2a{
or, in most cases, before the operator:
2da{
From :help a{:
a} *v_a}* *a}* *a{*
a{ *v_aB* *v_a{* *aB*
aB "a Block", select [count] Blocks, from "[count] [{" to
the matching '}', including the '{' and '}' (see
|[{|).
When used in Visual mode it is made characterwise.

In addition to the accepted answer it's worth noting that if you're selecting objects with visual mode you can always repeat it to select the next level. In your case, va{ would select the inner set of {}, but repeating a{ would expand this to include the outer set as well.

Related

node.js - Regex: Put space between triple mustaches ( {{{ => { {{ )

I have a String which contains minified CSS and handlebars.js notations (if statements, variables and so on). Because that the String is minified, it also contains stuff like '{{{' or '}}}' which I need to replace with '{ {{' or '}} }' (put one space between) in order to compile it correctly through handlebars.
Trouble is that I cannot manage to put the correct Regex together for this simple task. I guess the { symbols make the whole thing difficult since its a Regex-specific character.
String:
.class1{{{#if style.textColor}}color:{{style.textColor}};{{/if}}}item-price{{{#if style.showPrice}}display:block;{{else}}display:none;{{/if}}{{#if style.fontSizeItemPrice}}font-size:{{style.fontSizeItemPrice}}px;{{/if}}}
Expected output:
.class1{ {{#if style.textColor}}color:{{style.textColor}};{{/if}} }item-price{ {{#if style.showPrice}}display:block;{{else}}display:none;{{/if}}{{#if style.fontSizeItemPrice}}font-size:{{style.fontSizeItemPrice}}px;{{/if}} }
Simply substituting triple mustaches works, but only for the first occurance:
css = css.replace("{{{", "{ {{");
css = css.replace("}}}", "}} }")
String.replace function automatically converts the first string param to regex (without the global option set). That's why only the first occurrence is replaced. If you want to replace all occurrences of the pattern, you can create a regex with global option set. Try the following snippet.
css = css.replace(/{{{/g, '{ {{').replace(/}}}/g, '}} }')

Select first word from each line in multiple lines with Vim

I would like to copy the first words of multiple lines.
Example of code :
apiKey := fmt.Sprintf("&apiKey=%s", args.ApiKey)
maxCount := fmt.Sprintf("&maxCount=%d", args.MaxCount)
id := fmt.Sprintf("&id=%s", args.Id)
userid := fmt.Sprintf("&userid=%s", args.Userid)
requestFields := fmt.Sprintf("&requestFields=%s", args.RequestFields)
I would like to have this in my clipboard :
apiKey
maxCount
id
userid
requestFields
I tried with ctrl-v and after e, but it copies like on the image :
You could append every first word to an empty register (let's say q) using
:'<,'>norm! "Qyiw
That is, in every line of the visual selection, execute the "Qyiw sequence of normal commands to append (the first) "inner word" to the q register.
You need to have > in cpoptions for the newline to be added in between yanks (:set cpoptions+=>), otherwise the words will be concatenated on a single line.
If you want to quickly empty a register you can use qqq in normal mode (or qaq to empty register a).
Note: the unnamed register ("") will also contain what you want at the end of the operation, so you don't need to "qp to paste it, p will do.
I think the chosen answer is a really good one, the idea of appending matches to registers can be pretty useful in other scenarios as well.
That said, an alternative way to get this done might be to align the right-hand side first, do the copying and then undo the alignment. You can use a tool like tabular, Align or easy-align.
With tabular, marking the area and executing :Tab/: would result in this:
apiKey : = fmt.Sprintf("&apiKey=%s", args.ApiKey)
maxCount : = fmt.Sprintf("&maxCount=%d", args.MaxCount)
id : = fmt.Sprintf("&id=%s", args.Id)
userid : = fmt.Sprintf("&userid=%s", args.Userid)
requestFields : = fmt.Sprintf("&requestFields=%s", args.RequestFields)
You can now use visual block mode to select the first part, and then use u to undo the alignment.
Relying on the external cut program:
:'<,'>!cut -d' ' -f1

Very strange column string "single-quote-comment-brace-time-char"-bug substitutions in postgres selects - maybe related to JDBC only

If you ever had something similar and ripped your hair out to find the problem - in short:
--'
select '{t'::text
text
-----
TIME
???
What a nasty bug and interesting how this could lead to really messed up data!
It seems to be known since 9.1, may be related to the JDBC driver only and happened with our 9.3 as well!:
Here are some more details I found out (for helping to find and fix this ad hoc in your code or eventually by the "source hackers" ;) ) so far with example code below:
a single quote must appear somewhere in a single-line or multi-line comment above the select (e.g. --', /*'*/, -- foo's cool)
explicitely given strings must contain {t or {d to be substituted by TIME or DATE respectively
one of the strings must be explicitely (maybe also implicitely) of type text
a closing brace in the same or another string somewhere is necessary to continue this substition (e.g. select 'foo } bar')
a closing brace in a comment disables the behaviour again (e.g. --})
.
--'
select '{t'::text
union all select '{ta}'
union all select '{tfoo bar'
-- these are untouched
union all select '{ t}'
union all select 'foo { t}'
-- there seems to be an opening/closing "{" "}" match behaviour behind
-- it since the 2nd row below
union all select '{t'
union all select '{ta}'
union all select '{tfoo bar'
-- also "d" seems to be a "trigger"
union all select '}{d}'
-- a closing brace in a comment seems to disable it completely again
union all select '{d'
union all select '{d'
-- }
union all select 'a}{d}'
text
------------
TIME
{ta
TIME foo bar
{ t
foo { t}
TIME
{ta
TIME foo bar
DATE
DATE
{d
a}{d}

What is different about these two pairs of strings that makes this sed script with one and not the other?

This question is related to this other question I asked earlier today:
Find and replace text with all-inclusive wild card
I have a text file like this
I want= to keep this
This is some <text> I want to keep <and "something" in tags that I" want to keep> aff FOO1 WebServices and some more "text" that" should "</be> </deleted>
<this is stuff in tags I want=to begone> and other text I want gone too. </this is stuff in tags I want to begone>
A novice programmer walked into a "BAR2" descript keepthis
and this even more text, let's keep it
<I actually want this>
and this= too.`
when I use sed -f script.sed file.txt to run this script:
# Check for "aff"
/\baff\b/ {
# Define a label "a"
:a
# If the line does not contain "desc"
/\bdesc\b/!{
# Get the next line of input and append
# it to the pattern buffer
N
# Branch back to label "a"
ba
}
# Replace everything between aff and desc
s/\(\baff\)\b.*\b\(desc\b\)/\1TEST DATA\2/
}
I get this as my output:
I want= to keep this
This is some <text> I want to keep <and "something" in tags that I" want to keep> aff FOO1 WebServices and some more "text" that" should "</be> </deleted>
<this is stuff in tags I want=to begone> and other text I want gone too. </this is stuff in tags I want to begone>
A novice programmer walked into a "BAR2" descript keepthis
and this even more text, let's keep it
<I actually want this>
and this= too.
However, by simply changing the search strings from aff and desc to FOO1 and BAR2:
# Check for "FOO1"
/\bFOO1\b/ {
# Define a label "a"
:a
# If the line does not contain "BAR2"
/\bBAR2\b/!{
# Get the next line of input and append
# it to the pattern buffer
N
# Branch back to label "a"
ba
}
# Replace everything between FOO1 and BAR2
s/\(\bFOO1\)\b.*\b\(BAR2\b\)/\1TEST DATA\2/
}
gives the expected output:
I want= to keep this
This is some <text> I want to keep <and "something" in tags that I" want to keep> aff FOO1TEST DATABAR2" descript keepthis
and this even more text, let's keep it
<I actually want this>
and this= too.`
I am completely stumped about what is going on here. Why should searching between FOO1 and BAR2 work differently from the exact same script with aff and desc?
The end marker should be \bdesc instead of \bdesc\b.
Note the \b in the pattern, it matches a word boundary. Your above text contains the word description, but not desc.
Your previous question made me assume that you want that. If you don't care about word boundaries, remove the \b escape sequences completely.

Add a number of '=' in a rest (reStructuredText) document that equals to characters from last line?

I want to use a shortcut to add needed = (from Section/Title reStructuredText syntax) according to the last line.
So, suppose (being | the cursor position)
Title
|
and pressing an specific mapping mapped to a function, add a number of = that equals to the last line (where Title is), becoming:
Title
=====|
This sequence will get you close:
kyyp:.s/./=/g
Duplicate the previous line, then in that line, change every character to an equals sign. Map that to a key sequence you like, and try it out.
Another way:
:execute "normal " . strlen(getline(line(".") - 1)) . "i="
strlen(getline(line(".") - 1)) returns the lenght of the line above the current position. The result is that the command Ni= is executed, inserting = N times.
For a mapping I would have used:
put=repeat('=', col('$')-1)
For something more interactive, I would have use the same solution as Ned's.
(I don't like my mappings to change the various registers like #" or #/)
My vim-rst-sections vim plugin will convert lines to section headings:
http://www.vim.org/scripts/script.php?script_id=4486
In your case, you'd put the cursor on the line, and type <leader><leader>d to get a top-level heading like this:
#####
Title
#####
A few repeats of <leader><leader>d will take you down to the standard hierarchy of Python ReST sections to the =.

Resources