I try to use VIM build my SQL query by concatenate block of strings. For example, I have the following strings in three tabs in VIM:
# block one
where c = '123'
where c = '2345'
...
# block two
set b = 12
set b = 345
...
# block three
update myTable set a = 'abc',
update myTable set a = '23423',
...
each block contains 100 lines (fragments of SQL query). I would like to concatenate those blocks in to one complete SQL query: block one + block two + block three (100 lines) like this:
# sql queries
update myTable set a = 'abc', set b = 12 where c = '123'
update myTable set a = '23423', set b = 345 where c = '2345'
...
Just ignore the first line #..., it is just for explanation. I think that Visual Block can be used do that:
Yank all the lines in tab "block two";
Paste buffer to tab "block three" at the beginning;
Yank all the lines in tab "block one";
Paste the buffer to tab "block three" at the beginning.
However, I tried to the tip in Visual Block Mode(first two y and p examples), I could not get my expected result. The paste does not concatenate the buffer strings to the block. Not sure what I did wrong. Or any other alternative ways?
I recently ran across a VimCasts episode that describes how to do editing in visual block mode. Check out the video, I believe he describes what you want.
Did you try Visual-blockwise (ctrl-v)? They have to be the same width lines, but it works.
# Yank the lines from the other file
gg V G
# Add whitespace to the end
:%s/$/ /
# Select the whitespace at the end of the other file, and paste it
gg $ ctrl-v $ p
You may have to make a few changes, but hopefully it gave you some ideas at least.
Related
I am working in vim. I have a piece text that looks like :
one = 24
two = 52
three = 56
four = 74
Is there a way to use visual select to yank and paste up to the equal to sign in each line ? I want an operation that leaves me with the following result :
one = 24
two = 52
three = 56
four = 74
one =
two =
three =
four =
My current solution is to copy the whole thing, then jump to the one = 24 line in what I copied and then record this macro : 0f=ld$j to #w and then repeat it three times with 3#w. Is there a way to do this using visual select and yank and paste ?
I tend to use :substitute for these things
" First I yank and paste, in normal mode
yapP
" Then I transform
gv " to reselect, while in normal mode
:s/=.*/=/ " that will actual display :'<,'>s/.....
The actual reselection part may need a little work depending on where the cleared snippet shall appear. May be something like yapo<esc>p:'[,']s/=.*/=/ + enter
You can visually select the lines to apply normal commands to them with :norm.
Thus, you could do:
ggVG:norm f=ld$
How about
:global /=/ copy $ | substitute /=\zs.*//
We use global to select the original lines, then copy them to the end $ and remove the parts after = with substitute.
You could use a mapping like this
vnoremap ,s y:let #"=system('sed -nE "s/=.*/=/p"',#")<cr>
When selecting now some lines in in visual mode, type ,s. This will put the desired modification into the " register and you can paste them now using p wherever you want.
I have a code segment like below.
type Account struct {
Id int
UserId int
Name string
Address string
City string
State string
CountryId string
}
I want to delete all the data types. Is there a key combination to this?
I tried <C-V> and select the first letter of all data types in a vertical line, hoping d + $ would work post that, however vim only takes the first input d and deletes the first letter.
Use <C-v> to enter visual block mode, select the lines you want to change and then D to delete until the end of those.
From :h v_D :
{Visual}["x]X or *v_X* *v_D* *v_b_D*
{Visual}["x]D Delete the highlighted lines [into register x] (for
{Visual} see |Visual-mode|). In Visual block mode,
"D" deletes the highlighted text plus all text until
the end of the line. {not in Vi}
Note that, as mentioned in the help, X and D are not equivalent in visual block mode (X only deletes the current selection, not until the end of the line).
You can move to the left brace, press the % key and issue:
s/ \+[^ ]* *$/
to get:
type Account struct
Id
UserId
Name
Address
City
State
CountryId
}
The substitution removes all non-white-space characters at the end of the line.
You can use C-V and select the first column of all data type, then do $ to select until the end of the line, followed by x or d to delete.
Visually select all those lines with v6j.
Remove the extra stuff with :'<,'>norm ElD ('<,'> is added automatically for you).
Also, watch out for trailing space!
Not the shortest possible sequence but following is more natural to me
vi{ - Visually select the inner paragraph
'<,'>norm weld$ (typed as :norm weld$)
witch breaks down to
'<,'>norm - Apply normal commands over the selection
wel - jump to the end of the first word
d$ - delete until the end of the line
If I have multi-line line snippet:
length = 1;
keys = NewKey(value);
gt_backref = NULL;
ls_backref = NULL;
And I need to paste yanked (<ctrl>-V+y) node-> between every line of snippet:
node->length = 1;
node->keys = NewKey(value);
node->gt_backref = NULL;
node->ls_backref = NULL;
How do I paste yanked text in several sequential lines? Something like <ctrl>-V+<shift>-I but for paste, not for typed text.
<C-v>{motion}I<C-r>"<Esc>
Enter visual block mode with <C-v>.
Extend your selection.
Hit I to enter insert mode.
Do <C-r>" to insert the content of the unnamed register.
Hit <Esc> to apply the change to all the selected lines.
Or with :normal:
:[range]norm I<C-r>"<CR>
Well, if you select the text with Shift-V, then do a regex
:'<,'>s/.*/node->&/
that would add node-> to the selected lines.
or I guess even simpiler
:'<,'>s/^/node->/
If it's more complicated, maybe you would create some kind of macro with a search to find the type of lines you want to replace and run the same regex replace on each of those lines
This answer based on #Shaun's answer. This really needs a macro. But the correct regexp is
:'<,'>s/\(^\s\+\)/\1node->/
Because I need to take into account indentation.
But this approach does not universal. For every particular case we need new regexp.
Sometimes I need to insert some similar lines in a file which differ only in a sequence number. For example,
print "func 1";
print "func 2";
print "func 3";
print "func 4";
print "func 5";
Using vim, I end up copy pasting the first line using [yypppp] and then changing the last four lines. This is really slow if you have more lines to insert.
Is there a faster way to do this in vim?
An example of this is:
Initial state
boot();
format();
parse();
compare();
results();
clean();
Final state
print "func 1";
format();
print "func 2";
parse();
print "func 3";
compare();
print "func 4";
results();
print "func 5";
clean();
Record a macro. Here is the workflow for your particular example:
Copy-paste the first line. Then,
qa : Start recording macro to register a
yy : Yank current line
p : Paste current line in line below
/\d : Search for start of number (you can skip this command, the next command automagically moves the cursor to the number)
C-A : Control-A increments the number
q : Stop recording macro
3#a : Replay macro 3 times
You can replace 3 with any number to keep generating new print lines with incremented numbers.
For your second example, you can just add
j : Moves one line down
after the yy command, to get alternating lines of commands and print's.
You have plugins that do it. For example, visincr. Visually select your column of numbers, and run :I.
Another way to do it is to record a macro. run qx to start recording macro to register x, yiw to yank word under the cursor, j to go one line down, viwp to paste it, CTRLA to increment the new number, q to stop recording, and then #x to replay contents of register x.
For this particular case, you could use a macro. There's a good write-up of how to do sequence numbers in this post.
You need to change the example in the post to write out the entire line first and then record a macro that copies the line and updates the counter.
I am using ruby on rails but that does not matter much for this question. Let's say that i have a statement like this
error = 'this is an error message'
I have noticed that I end up doing this a lot
error = 'this is an error message'
puts "error = #{error.inspect}"
I am sure a macro can be written which would take the work on the left hand side of left most = and then create another line along with template shown above.
I am using mvim on mac. Any pointer in terms of where I should start to look for developing what I want.
Try snipmate:
http://www.vim.org/scripts/script.php?script_id=2540
I recorded a simple macro that does your sample. To record a macro type q followed by what register you want the macro to be put in (convention calls for qq). To play the macro type # then the macro register. You can view this at :help recording
To write the macro, use the following commands (and here is how is should look in the register)
^yEoputs "error = #{^Op.inspect}"^[
^ moves to the first non whitespace character of the line
yE yanks to the end of the space separated word.
o Puts you in insert mode on the next line
puts "error = #{ is the text that you type out
^O is ctrl+O (capital letter o) - this allows the next, and only the next command to be run in command mode, which is...
p Puts the yanked word, after this command is run you're still in insert mode
.inspect}" is the text that you type and finally...
^[ is Esc
I would go for:
nnoremap µ :s/^\s*\(\k\+\)\s*=.*/&\rputs "\1 = #{\1.inspect}"/<cr>
:s presents the advantage of doing the job plus matching the assigned variable if any. Doing the same thing with classical commands like yw, p, etc would be more cumbersome.
If the template become more complex, we can rely on template-file expanders as long as they easily permit to call viml function like matchstr(). Of course, in that case I would use mu-template with the following template-file:
VimL:" $Id: {rtp}/template/ruby/inspect.template
VimL: let s:value_start = '¡'
VimL: let s:value_end = '¡'
VimL: let s:reindent = 1
VimL: let s:marker_open = '<+'
VimL: let s:marker_close = '+>'
VimL: let s:varname = matchstr(getline(line('.')-1), '^\s*\zs\k\+\ze\s*=')
VimL: if empty(s:varname) |throw "the previous line don't assign any variable" |endif
puts "¡s:varname¡ = #{¡s:varname¡.inspect}"<++>
VimL:"vim: encoding=utf-8
If you're doing these on the fly, a snipmate snippet could look like this:
${1:error} = '${2:error message here}'
puts "error = #{$1.inspect}"
If, on the other hand you're just wanting to output pre-existing variables for debugging purposes. Nick-Canzoneri's macro may be more useful.