Do vim or gVim commands exist to copy text between parentheses? - vim

In the following code snippet, if I go to the first open parenthesis ( of the line beginning with (spit
(defn missing-accts
"Prints accounts found in one report but not the other."
[report-header mapped-data out-file]
(spit out-file (str "\n\n "
(count mapped-data)
" " report-header
"\n\n") :append true)
.
.
.
vim highlights the first ( and closing ) parentheses.
Is there and, if there is, what is the vim command that would yank the entire spit command?
Thanks.

The sequence
va(
will highlight from the opening to closing brackets inclusively, and a y will then yank that. Note unlike the % command, you don't have to be positioned on the bracket - you just need to be inside the clause.
Note that
vi(
would highlight everything inside the brackets, but not the brackets.
You can do this for braces too ({ instead of () and XML tags (t - presumably for tag)

Vim does have such a command, and fortunately it is very simple. Just type y%.
The reason this works is that % is what Vim calls a movement command. It moves from one delimiter to the matching delimiter -- in your case from the opening parenthesis to the closing one. The y command yanks a single line into Vim's buffer if invoked as yy, but the second y is not required. Instead, one can issue a movement like %, whereupon Vim will yank the text moved over. Thus, y%.

use % with y. press "y" once then "%" ,your cursor should be on "(" when you type the command.

Related

Vim: % command doesn't move to first parenthesis

I am reading the vim manual. I am having an unexpected result using the % command.
Here is a quote from the manual:
When the cursor is not on a useful character, "%" will search forward to find
one. Thus if the cursor is at the start of the line of the previous example,
"%" will search forward and find the first "(". Then it moves to its match:
if (a == (b * c) / d)
---+---------------->
%
However, when I repeat the same thing (placing the cursor at the start of the line) and press %, the cursor moves the last ) parenthesis instead.
I didn't configure vim, I am using the default settings. I am wondering if my vim is not configured well. Is there a way to configure vim for it to move the cursor the first parenthesis similar to how it is described in the manual?
I am using gVim on Windows.
"it moves to its match", which means that it will move to the matching paranthesis of the one it found (in this case the closing paranthesis). So it does what it should.
To get back to the first paranthesis you will need to press '%' again!
Also you can use f( to move the cursor to the first open parenthesis (if it's not on the same line, you can use search for it like /( just as easily). This is helpful since you could do something like df( (or d/( to target a different line) to delete everything up to and including the open parenthesis. Whereas relying on a double press of % wouldn't allow that.

vim substitute within block selection on one line

I have the following expression in a latex file
\begin{dfn} \tag{Diagram $đť——$ over a trisp $\Delta$}
\label{dfn:Diagram D over a trisp Delta}
now i want to substitute all the spaces in the brackets on the second line with :.
my idea was to visual select the expression by v% while being on one of the brackets and then :s/\ /:/g but this replaces all whitespaces and results in
::::::::::::::\label{dfn:Diagram:đť——:over:a:trisp:Delta}
which results in the task of removing the : before the \label{…}
as vim is a very powerful editor - there should be an easier way, that I don't know yet.
Go to the first {, press v, press %. This will visually select the text between { and }. Then press : and enter s/\%V\ /:/g in your minibar. See this for explanation:
http://vim.wikia.com/wiki/Search_and_replace_in_a_visual_selection
Note that otherwise g will replace all on the current line.

How to substitute for matching delimiters in vi?

I have some text which has matched delimiters (in this case, curly braces, and the text happens to be LaTeX, which is only incidental):
\nb{\vec{n},\vec{y}} \in \vec{z}
What I'd like to do is globally replace \nb{...} with (...), while respecting the nesting of delimiters. I.e., the result should be
(\vec{n},\vec{y}) \in \vec{z}
and not
(\vec{n},\vec{y}} \in \vec{z)
which is what would be produced by :%s/\\nb{\(.*\)}/(\1)/g. Standard regular expressions can't handle matched delimiters, so I wasn't expecting this way to work. Is there some vi-specific trick I can use to do this?
If you have surround.vim installed then the following should do the trick
:set nowrapscan
:let #q="/\\m\\\\nb{/e+1\<cr>cs{)dF\\#q"
gg#q
If you do not:
:set nowrapscan
let #q="/\\m\\\\nb{<cr>dt{yi{\"_ca{()\<esc>\"0P#q"
gg#q
Overview
Create a recursive macro that searches for \nb{, positions the cursor just inside the {, replace the }{'s with ()'s.
Glory of Details
:set nowrapscan this prevents searches from looping back around the file.
:let #q="..." store our macro inside the q register
/\m\nb{/e+1 searches for \nb{ and positions the cursor after the {
cs{) the surround version will just change the surrounding { with )
#q run the macro again
Used " so must escape a few things so they work correctly.
gg#q go to the top of the file and execute the macro in register q
The non surround version varies a bit here
yi{ copy the text inside {'s
"_ca{()<esc> change the text inside and including the {'s and replace with ()
"0P paste what we just copied inside the ()
I would use the following :global command.
:g/\\nb{/norm!/^M%r)[{r(dF\\
Type ^M as Ctrl+V, Enter.

How to quickly remove a pair of parentheses, brackets, or braces in Vim?

In Vim, if I have code such as (in Ruby):
anArray << [anElement]
and my cursor is on the first [, I can hop to ] with the % key, and I can delete all the content between the [] pair with d%, but what if I just want to delete the [ and ] leaving all the remaining content between the two. In other words, what's the quickest way to get to:
anArray << anElement
One can take advantage of the text objects that are built in into Vim
(see :help text-objects). The desired edit can be stated as a
sequence of the following three actions.
Cut the text inside the square brackets:
di[
Select the (empty) square brackets:
va[
Alternatively, you can just select the character under the
cursor and the one to the left of it, because the command
from step 1 always puts the cursor on the closing bracket:
vh
Paste the cut text over the selected brackets:
p
Altogether, it gives us the following sequence of Normal-mode commands:
di[va[p
or, when the alternative form of step 2 is used:
di[vhp
ma%x`ax (mark position in register a, go to matching paren, delete char, go to mark a, delete char).
EDIT:
%x``x does the same thing (thanks to #Alok for the tip).
Using the Surround plugin for Vim, you can eliminate surrounding delimiters with ds<delimeter>.
To install it via Vundle plugin, add
Plugin 'tpope/vim-surround'
to your .vimrc file and run :PluginInstall.
If you have issues with the marks pointing to the first char of the line or with using % ...
di[vhp
works as well... It deletes matching [] brackets, when the cursor is anywhere inside. '[' can be replaced by '{' or '(' .
The other answers work fine if you want to delete delimiters one line at a time.
If on the other hand you want to remove a function and it's delimiters from the entire file use:
:%s/function(\(.*\))/\1/g
which replaces function(arguments) with arguments everywhere in the file.
You can use d% while your cursor is on the bracket/parentheses.

Is there an equivalent to o-<esc>-p in Vim?

When I yank a whole line and press p, Vim creates a new line to put the yanked text, because of the newline characters. But if I yank part of a line is there a way to put that yanked text in a new line? I currently do o ESC p. Is this the only way?
Here's your answer from Vim help:
:[line]pu[t] [x] Put the text [from register x] after [line] (default
current line). This always works |linewise|, thus
this command can be used to put a yanked block as new
lines.
The cursor is left on the first non-blank in the last
new line.
The register can also be '=' followed by an optional
expression. The expression continues until the end of
the command. You need to escape the '|' and '"'
characters to prevent them from terminating the
command. Example: >
:put ='path' . \",/test\"
< If there is no expression after '=', Vim uses the
previous expression. You can see it with ":dis =".
:[line]pu[t]! [x] Put the text [from register x] before [line] (default
current line).
But colon-p-u-enter is more keystrokes anyway =/
Theres o(c-r)0 as you can always paste from registers while your in insert mode but thats only if you want to remain in insert mode after the paste

Resources