Wrapping a code block around code in VIM - vim

Is it possible to wrap a code block around a piece of code in VIM? For example, if I want to wrap a while loop around the following, how can I do it without having to scroll to the bottom of the code to add the closing brace:
if(z > y)
{
switch(x)
{
case 1:
addPoint();
break;
case 2:
addValue();
break;
}
}
This is what the code will look like afterwards:
while (a > 10)
{
if(z > y)
{
switch(x)
{
case 1:
addPoint();
break;
case 2:
addValue();
break;
}
}
}

Vim does not have a built in surrounding system. I suggust you look at Tim Pope's excellent surround plugin. Then assuming you are positioned on the if you can do this:
Vj%SBIwhile (a > 10)
The Vj% selects the block + the conditional.
SB surrounds the selected text with a {,} (read surround with block)
Insert your loop text at the beginning of the newly inserted block.
You don't need surround to do this. You can do it with plain vanilla vim.
Vj%>`]o}<esc>g;g;Owhile (a > 10)<cr>{<esc>
However I prefer to use surround as it is a very handy plugin to have.
For more help see:
:h %
:h g;
:h `]

You can use the vim plugin surround.vim. It lets you wrap lines or fragments in blocks (and change the wrapping pairs).

Check something like Snipmate, it's possible they have an ability of surrounding Visually selected code with a snippit.
http://www.vim.org/scripts/script.php?script_id=2540
snipMate-visual-selection-support There is a special placeholder called {VISUAL}. If you visually select text, then press Vim
switches to insert mode. The next snippet you'll expand will replace
{VISUAL} by the text which was selected previously

You can make a mapping to do this in vanilla Vim e.g.:
:imap <S-F2> <Esc>j>>j>%%o<C-D>}
Use the mapped key after typing the opening brace for the surrounding code. Goes without saying that you should change <S-F2> to a keymap you like.
Breakdown of the key sequence:
Esc: go to Normal mode
j>>: move cursor to next line (in your example the if line) and indent it.
j>%: move cursor to the line with opening brace of if, indent from there to matching brace (the cursor will jump back to the opening brace)
% goes to the closing brace of if
o add a new line after the closing brace, and enter Insert mode.
CTRL+D: unindent (to undo the effect of smartindent)
} add the closing brace for the surround code (in your example, while)
Of course the plugins recommended by others have many more features so you should look at those as well.

Related

vim - surround text with function call

I want to wrap some code :
myObj.text;
with a function call where the code is passed as an argument.
console.log(myObj.text);
I've thought about using surround.vim to do that but didn't manage to do it.
Any idea if it's possible ? I
With Surround in normal mode:
ysiwfconsole.log<CR>
With Surround in visual mode:
Sfconsole.log<CR>
Without Surround in normal mode:
ciwconsole.log(<C-r>")<Esc>
Without Surround in visual mode:
cconsole.log(<C-r>")<Esc>
But that's not very scalable. A mapping would certainly be more useful since you will almost certainly need to do it often:
xnoremap <key> cconsole.log(<C-r>")<Esc>
nnoremap <key> ciwconsole.log(<C-r>")<Esc>
which brings us back to Surround, which already does that—and more—very elegantly.
I know and use two different ways to accomplish this:
Variant 1:
Select the text you want to wrap in visual mode (hit v followed by whatever movements are appropriate).
Replace that text by hitting c, then type your function call console.log(). (The old text is not gone, it's just moved into a register, from where it will be promptly retrieved in step 3.) Hit <esc> while you are behind the closing parenthese, that should leave you on the ) character.
Paste the replaced text into the parentheses by hitting P (this inserts before the character you are currently on, so right between the ( and the )).
The entire sequence is v<movement>c<functionName>()<esc>P.
Variant 2:
Alternatively to leaving insert mode and pasting from normal mode, you can just as well paste directly from insertion mode by hitting <ctrl>R followed by ".
The entire sequence is v<movement>c<functionName>(<ctrl>R")<esc>.
You can use substitution instruction combined with visual mode
To change bar to foo(bar):
press v and select text you want (plus one more character) to surround with function call (^v$ will select whole text on current line including the newline character at the end)
type :s/\%V.*\%V/foo\(&\)/<CR>
Explanation:
s/a/b/g means 'substitute first match of a with b on current line'
\%V.*\%V matches visual selection without last character
& means 'matched text' (bar in this case)
foo\(&\) gives 'matched text surrounded with foo(...) '
<CR> means 'press enter'
Notes
For this to work you have to visually select also next character after bar (^v$ selects also the newline character at the end, so it's fine)
might be some problems with multiline selections, haven't checked it yet
when I press : in visual mode, it puts '<,'> in command line, but that doesn't interfere with rest of the command (it even prevents substitution, when selected text appears also somewhere earlier on current line) - :'<,'>s/... still works

How to delete a paragraph as quickly as possible

I want to delete the following codes in my config file:
server {
listen 80;
server_name xxx;
location / {
try_files xx;
}
}
I know I can use 7dd, but this is not handy enough- if the section is too long, counting the rows would be inconvenient.
Is there a better way to do this?
Sometimes, I have to delete a whole function, any ideas for that?
As in common in Vim, there are a bunch of ways!
Note that the first two solutions depend on an absence of blank lines within the block.
If your cursor is on the server line, try d}. It will delete everything to the next block.
Within the entry itself, dap will delete the 'paragraph'.
You can delete a curly brack block with da}. (If you like this syntax, I recommend Tim Pope's fantastic surround.vim, which adds more features with a similar feel).
You could also try using regular expressions to delete until the next far left-indented closing curly brace: d/^}Enter
]] and [[ move to the next/previous first-column curly brace (equivalent to using / and ? with that regex I mentioned above. Combine with the d motion, and you acheive the same effect.
If you're below a block, you can also make use of the handy 'offset' feature of a Vim search. d?^{?-1 will delete backwards to one line before the first occurrence of a first-column opening curly brace. This command's a bit tricky to type. Maybe you could make a <leader> shortcut out of it.
Note that much of this answer is taken from previous answer I gave on a similar topic.
If there is a blank line immediately after these lines, Vim will identify it as a paragraph, so you can simply use d} to delete it (assuming the cursor is on the first line).
Try using visual mode with %.
For example, say your cursor is at the beginning of the "server" line.
Press v to go into visual mode. Press % to highlight to the end of the block. Press d to delete it.
As David said their are many ways. Here are a few that I like:
Vf{%d This assumes you are on the line w/ server and you do a visual line selection, find the { and then find the matching } via the % command.
set relativenumber or set rnu for short. This turns on line numbering relative to your cursor. So you do not have to count the lines just look and 7dd away to deleting your block.
Use VaB to visually select a block i.e. { to }, line-wise. While still in visual mode continue doing aB until the proper block is selected then execute d. This means you can select a block from inside the block instead of at the start or end.
d} will delete a paragraph. This works in your current scenario because there is no blank line inside the block. If there is one then all bets are off. Although you can continue pressing . until the block is properly deleted.
If inside a block you can jump to the current block via [{ then continue executing [{ until you are at the correct block then V%d or d%dd to delete the block
Using the techniques above you can delete a function as well, but you can also use the [M and friends ([m, ]m, ]M) to jump to the start and endings of methods but they commonly work for functions as well.
For more information
:h %
:h 'rnu'
:h aB
:h }
:h .
:h [{
:h [m
Given matching parens, or braces, you can use % with d to delete, spanning lines.
So, assuming that you're on the server { line, you can do d%. This is generally useful for all code blocks, e.g. function blocks, loop blocks, try blocks.
Similarly, dip works, but d} is shorter. However, both will only delete to the next empty 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.

Indent or comment several text lines with vi

can vim or vim be used to comment or indent at the same time a number of lines? For instance:
for item in Lista:
ind = int(floor(1.0*(item-lmin)/width))
if ind==nintervals:
ind=ind-1
print item,ind
comment it to:
#for item in Lista:
#ind = int(floor(1.0*(item-lmin)/width))
#if ind==nintervals:
#ind=ind-1
#print item,ind
or indent it to:
for item in Lista:
ind = int(floor(1.0*(item-lmin)/width))
if ind==nintervals:
ind=ind-1
print item,ind
P.D. Is relevant the difference between VI and VIM?
here is another way.
block lines with ctrl+v
Insert comment sign (//) with I
escape with ESC
the key typing is
ctrl+v → jjjj → I → // → ESC
To comment, press a capital V to enter VISUAL LINE mode, select all lines, then press : to enter command mode and use the command (note that VIM already include the '<,'>marks for you):
:'<,'>s/^/#/
If you prefer hash marks near the text, and not near the left margin, the command is:
:'<,'>s/^\(\s*\)/\1#/
To indent, select the block the same, then type > to indent, < to unindent.
type :set number. take note of the start and end line number of the block you want to comment. then do an address range substitution, eg
:12,17s/^/#
Lots of answers here, all with a theme. The best way to do it really depends on context (because context determines the easiest navigation method), so I'll make some assumptions about the context. If the section you want to indent or comment is a single paragraph (eg, you want to indent or comment everything from the cursor up to the next blank line), you can indent with:
>)
If the cursor is not on the start of the paragraph, but the section you want to indent is a single paragraph and the cursor is in the middle, use
>ip
Finally, suppose you want to indent a block of code delimited by {}, and the cursor is in the middle of that block. Use
>i{
To comment, in each case just replace the > with v and use the above commands to make a block selection, then do a text replace like s/^/#/.
The key is the navigation commands. I highly recommend
:help v_a
Similar to the accepted answer, but easier for blocks or paragraphs:
Block lines: Ctrl + V
Select paragraph: }
Insert mode: I (uppercase i)
Type character to insert: # (with space after char, no Enter!)
Press: ESC
This should auto complete the character in all the selected block.
Basically the diference with the accepted answer is that instead of using j to go down line by line you use } to select the whole paragraph (or you could use G for end of file, for ex.
Short version:
Ctrl + V + } + I + # + ESC
I know there are a zillion answers here already explaining how to use > and < for indentation, so I'm not going to bother with that. With respect to the commenting, though, while you can do it quick and dirty with a block insert or a substitution, you can do way better with the NERD Commenter plugin. It provides commands to comment and uncomment in various ways, it knows what comment symbol to insert based on the syntax, and it can do pretty multi-line comments if the language supports them.
Select the lines using visual mode.
To indent once type >> or << to indent right or left respectively. To indent n times type n>> or n<<.
To comment out do replace the beginning of the line with the comment:
:'<,'>s/^/#/
'<,'> means "from the beginning of the selection until the end.
s/^/#/ replaces the beginning of each line in the range with #, assuming # makes a line into a comment.
Put your cursor on the first line, count how many lines should be indented, in the above example it's 5, then for hash (#) type
:.,.+5%s/^\([ <tab>]*\)/#\1/<enter> or for a tab indentation,
:.,.+5%s/^\([ <tab>]*\)/<tab>\1/<enter>,
<tab> and <enter> are the tab and enter keys.
There are probably more elegant ways of doing this, but something like this is a quick-n-dirty thing.
For commenting you could use VISUAL BLOCK selection (Ctrl-V) and select the beginnings of the lines, then press Shift-I and write one #. After pressing Esc all the lines get the #.
My usual solution is:
<ESC>
<q><a> => start a macro and save it as macro a
<^> => to get to the start of the line
<i> => insert mode
<#> => Add the #
<ESC> => End insert mode
<down> => Move to the next line
<q> => End macro
Then once:
<[at]><a> => repeat macro a
Then just repeat <[at]><[at]> (repeat last executed macro) until all lines are commented. You can just hold <[at]> and let keyboard repeat do the rest.
BTW: How do you write an [at] sign here without stackoverflow turning it into "> blockquote"?
To indent:
[shift] + [v] => line select mode
[down] => until all lines to indent are selected
then:
[>] => indent once
or:
[2..x][>] => indent 2..x levels
If you are using Python (or other languages that use # as comment), a faster way to comment multiple lines would be:
Enter visual block mode (Ctrl+v) from the start of the line.
Go down as needed (j multiple times).
Replace space with # by pressing r then #.
To uncomment, do the same but for step three replace it with space.

In Vim, what is the best way to select, delete, or comment out large portions of multi-screen text?

Selecting a large amount of text that extends over many screens in an IDE like Eclipse is fairly easy since you can use the mouse, but what is the best way to e.g. select and delete multiscreen blocks of text or write e.g. three large methods out to another file and then delete them for testing purposes in Vim when using it via putty/ssh where you cannot use the mouse?
I can easily yank-to-the-end-of-line or yank-to-the-end-of-code-block but if the text extends over many screens, or has lots of blank lines in it, I feel like my hands are tied in Vim. Any solutions?
And a related question: is there a way to somehow select 40 lines, and then comment them all out (with "#" or "//"), as is common in most IDEs?
Well, first of all, you can set vim to work with the mouse, which would allow you to select text just like you would in Eclipse.
You can also use the Visual selection - v, by default. Once selected, you can yank, cut, etc.
As far as commenting out the block, I usually select it with VISUAL, then do
:'<,'>s/^/# /
Replacing the beginning of each line with a #. (The '< and '> markers are the beginning and and of the visual selection.
Use markers.
Go to the top of the text block you want to delete and enter
ma
anywhere on that line. No need for the colon.
Then go to the end of the block and enter the following:
:'a,.d
Entering ma has set marker a for the character under the cursor.
The command you have entered after moving to the bottom of the text block says "from the line containing the character described by marker a ('a) to the current line (.) delete."
This sort of thing can be used for other things as well.
:'a,.ya b - yank from 'a to current line and put in buffer 'b'
:'a,.ya B - yank from 'a to current line and append to buffer 'b'
:'a,.s/^/#/ - from 'a to current line, substitute '#' for line begin
(i.e. comment out in Perl)
:'s,.s#^#//# - from 'a to current line, substitute '//' for line begin
(i.e. comment out in C++)
N.B. 'a (apostrophe-a) refers to the line containing the character marked by a. ``a(backtick-a) refers to the character marked bya`.
To insert comments select the beginning characters of the lines using CTRL-v (blockwise-visual, not 'v' character wise-visual or 'V' linewise-visual). Then go to insert-mode using 'I', enter your comment-character(s) on the first line (for example '#') and finally escape to normal mode using 'Esc'. Voila!
To remove the comments use blockwise-visual to select the comments and just delete them using 'x'.
Use the visual block command v (or V for whole lines and C-V for rectangular blocks). While in visual block mode, you can use any motion commands including search; I use } frequently to skip to the next blank line. Once the block is marked, you can :w it to a file, delete, yank, or whatever. If you execute a command and the visual block goes away, re-select the same block with gv. See :help visual-change for more.
I think there are language-specific scripts that come with vim that do things like comment out blocks of code in a way that fits your language of choice.
Press V (uppercase V) and then press 40j to select 40 lines and then press d to delete them. Or as #zigdon replied, you can comment them out.
The visual mode is the solution for your main problem. As to commenting out sections of code, there are many plugins for that on vim.org, I am using tComment.vim at the moment.
There is also a neat way to comment out a block without a plugin. Lets say you work in python and # is the comment character. Make a visual block selection of the column you want the hash sign to be in, and type I#ESCAPE. To enter a visual block mode press C-q on windows or C-v on linux.
My block comment technique:
Ctrl+V to start blockwise visual mode.
Make your selection.
With the selection still active, Shift+I. This put you into column insert mode.
Type you comment characters '#' or '//' or whatever.
ESC.
Or you may want to give this script a try...
http://www.vim.org/scripts/script.php?script_id=23
For commenting out lines, I would suggest one of these plugins:
EnhancedCommentify
NERD Commenter
I find myself using NERD more these days, but I've used EnhancedCommentify for years.
If you want to perform an action on a range of lines, and you know the line numbers, you can put the range on the command line. For instance, to delete lines 20 through 200 you can do:
:20,200d
To move lines 20 through 200 to where line 300 is you can use:
:20,200m300
And so on.
Use Shift+V to go in visual mode, then you can select lines and delete / change them.
My usual method for commenting out 40 lines would be to put the cursor on the first line and enter the command:
:.,+40s/^/# /
(For here thru 40 lines forward, substitute start-of-line with hash, space)
Seems a bit longer than some other methods suggested, but I like to do things with the keyboard instead of the mouse.
First answer is currently not quite right?
To comment out selection press ':' and type command
:'<,'>s/^/# /g
('<, '> - will be there automatically)
You should be aware of the normal mode command [count]CTRL-D.
It optionally changes the 'scroll' option from 10 to [count], and then scrolls down that many lines. Pressing CTRL-D again will scroll down that same lines again.
So try entering
V "visual line selection mode
30 "optionally set scroll value to 30
CTRL-D "jump down a screen, repeated as necessary
y " yank your selection
CTRL-U works the same way but scrolls up.
v enters visual block mode, where you can select as if with shift in most common editors, later you can do anything you can normally do with normal commands (substitution :'<,'>s/^/#/ to prepend with a comment, for instance) where '<,'> means the selected visual block instead of all the text.
marks would be the simplest mb where u want to begin and me where u want to end once this is done you can do pretty much anything you want
:'b,'ed
deletes from marker b to marker e
commenting out 40 lines you can do in the visual mode
V40j:s/^/#/
will comment out 40 lines from where u start the sequence

Resources