How to quickly remove a pair of parentheses, brackets, or braces in Vim? - 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.

Related

Delete trailing whitespace, alternative to substitute command

To delete trailing whitespace on a line or across a file I can do:
:[range]s/\s\+$//
However, I was wondering for a single line in normal mode if there's an easier approach, for example if the line is:
Hi, I am a line |
And my cursor is past the e, is there a more generic command than doing dTe in normal mode? The next best I could find was dg_, but that goes one too far. And then, one more option might be gElD.
Generic way without :substitute
Use diw to delete multiple whitespace (including tabs) while the cursor is on one of the whitespaces (like in the example).
This works for any whitespace sequence: trailing, leading, etc.
Use dw to delete from the cursor to the end of the sequence. This is useful when you want to re-indent a line or fix alignment.
As this is independent of the first/last character of the previous/next word this is the most generic way.
Using a mapping with :substitute
Use nmap <leader>tr :%s/\s\+$// to remove all trailing whitespace in the whole buffer by pressing \tr (assuming the original mapleader)1.
Replace tr with what ever suits you best.
Omit the the % in the mapping to make it only work on the current line.
--
As a side note: Use set list listchars=trail:· to show trailing whitespace (replace · with any character you like).
--
1 Removing all whitespace may not be what you want, especially when using a version control system and the file contains whitespace not added by yourself.

Writing whole alphabet in Vim

I sometimes need to write the whole alphabet abcd…z and I hate typing it letter by letter in Vim's insert mode. Does there exist any method to do this more efficiently?
I know about the ga command which gives me the ascii code of the character where the cursor is … but don't know anything about how to mix it with my standard solution to type numbers from 1 to (for example) 5000: a1ESCqqyyp^Aq4998#q …
Using set nrformats+=alpha:
ia<Esc>qqylp<C-a>q24#q
Step by step:
ia<Esc> " Start with 'a'
qqylp<C-a>q " #q will duplicate the last character and increment it
24#q " Append c..z
If your shell does brace expansion this is a pretty elegant solution:
:r !printf '\%s' {a..z}
:read! reads the output of an external command into the current buffer. In this case, it reads the output of the shell's printf applied to {a..z} after it's been expanded by the shell.
How about this command:
:put =join(map(range(char2nr('a'),char2nr('z')),'nr2char(v:val)'),'')
Collect the ASCII values of the characters in the range from a to z, then map them over the nr2char() function and insert the result into the current buffer with :put =.
When you leave out the enclosing join( … ,'') you get the characters on a separate line each.
See
:h nr2char(),
:h char2nr(),
:h :put,
and look up range(), map(), join() and friends in the list-functions table.
First, set nrformats+=alpha.
Then:
ia<ESC>Y25p<CTRL-V>}g<CTRL-A>k26gJ
Which means:
ia insert the initial a
Y25p yank the a and duplicate it on 25 lines
<CTRL-V> go into visual block mode
} go to the last character at the end of the current paragraph
g<CTRL-A> incrementally increase each alphabetic character (see help v_g_CTRL-A)
k go up one line
26gJ join 26 lines without inserting or removing any spaces
Which leads to:
abcdefghijklmnopqrstuvwxyz
I have found a shorter solution (you don't need to change nrformats beforehand) while solving http://www.vimgolf.com/challenges/5ebe8a63d8085e000c2f5bd5
iabcdefghijklm<Esc>yiwg??P
which means:
iabcdefghijklm<Esc> insert first half of the alphabet
yiw copy it
g?? ROT13 encode (shift by 13 letters) to get the second half
P paste the first half
You might try using Vim abbreviations or a full-fledged snippet manager plugin like UltiSnips. It might take a few moments to set up, and you'd have to type that alphabet one more time to define it as an abbreviation or snippet, but after that you'd be able to insert the alphabet or any other common chunk of text much more easily.

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

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.

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 can I insert text in the middle of the line to multiple lines in Vim?

Say I have ten lines and I want to prepend text to some word that occurs in those lines? It does not have to be at the beginning of the line.
From:
sdfsd foo sdfsd
sfsd foo fsdf
sdfsdf foo sdfsdf
to:
sdfsd bar(foo sdfsd
sfsd bar(foo fsdf
sdfsdf bar(foo sdfsdf
Is it also possible to not only prepend the bar( but actually surround foo with bar(foo)?
I would also like a quick way to append // comments to multiple lines (C-style comments).
I use Vim/GVim 7.2.
Go to the first foo, press Ctrl-v to enter visual block mode and press down until all the lines with foo are marked. Then press Shift-i to insert at the beginning (of the block). When you are finished and press Esc, the inserted characters will be added to each line at the left of the marked block.
To insert at the end, press again Ctrl-v, move up/down to mark all affected lines and then press End or $ to extend the selection until the end of the lines. Now you can press Shift-a to append at the end of all the lines, just like previously with Shift-i.
The visual selection can also be done with normal movement commands. So to comment a whole block in C you could move to the opening brace and type Ctrl-v % Shift-i // Esc.
To answer your first question, the below
:%s/foo/bar(&)/g
will look for foo, and surround the matched pattern with bar(). The /g will do this multiple times in one line.
Since you're just matching foo, you could do a simple :s/foo/bar(foo)/g. The above will work, however, if you decide to match on a regular expression rather than a simple word (e.g. f[a-z][a-z]). The '&' in the above represents what you've matched.
To prefix a set of lines I use one of two different approaches:
One approach is the block select (mentioned by sth). In general, you can select a rectangular region with ctrl-V followed by cursor-movement. Once you've highlighted a rectangle, pressing shift-I will insert characters on the left side of the rectangle, or shift-A will append them on the right side of the rectangle. So you can use this technique to make a rectangle that includes the left-most column of the lines you want to prefix, hit shift-I, type the prefix, and then hit escape.
The other approach is to use a substitution (as mentioned by Brian Agnew). Brian's substitution will affect the entire file (the % in the command means "all lines"). To affect just a few lines the easiest approach is to hit shift-V (which enables visual-line mode) while on the first/last line, and then move to the last/first line. Then type:
:s/^/YOUR PREFIX/
The ^ is a regex (in this case, the beginning of the line). By typing this in visual line mode you'll see '<,'> inserted before the s automatically. This means the range of the substitution will be the visual selection.
Extra tip: if your prefix contains slashes, you can either escape them with backslash, or you can use a different punctuation character as the separator in the command. For example, to add C++ line comments, I usually write:
:s:^:// :
For adding a suffix the substitution approach is generally easier unless all of your lines are exactly the same length. Just use $ for the pattern instead of ^ and your string will be appended instead of pre-pended.
If you want to add a prefix and a suffix simultaneously, you can do something like this:
:s/.*/PREFIX & SUFFIX/
The .* matches the whole line. The & in the replacement puts the matched text (the whole line) back, but now it'll have your prefix and suffix added.
BTW: when commenting out code you'll probably want to uncomment it later. You can use visual-block (ctrl-V) to select the slashes and then hit d to delete them, or you can use a substitution (probably with a visual line selection, made with shift-V) to remove the leading slashes like this:
:s:// ::
:normal to the rescue!
:%norm Wibar(
:%norm WEa)
:norm(al) replays the commands as if you had typed them:
W - goes to the next word
i - starts insertion mode
bar( - types the sequence 'bar('
Or in one line:
:%norm Wibar(ctrlvESCEa)
If you're running Windows then type ctrlq instead of ctrlv.
Yet another possibility (probably not-so-useful in your test case, but handy in other situations) is to cordon off the area you want to change with marks.
Put the cursor anywhere in the top line and press 'a
Put the cursor anywhere in the last line and press 'b
Issue the command :'a,'b s/foo/bar(&)/
I usually like visual block mode if everything is visible on the screen, and I usually prefer marks if the start and stop are separated by many screens.
Another simple regular expression is:
%s/^/<text you want to prepend>/
For the C-style comments, use the regexp answer by Brian, and match on line ending $, and insert away.

Resources