I am using vim to log a lot of my day-to-day work. I usually have a lot of results with matrix. When I paste those at the end of the day I have pages of (sometimes) 0ne or 2 columns matrix
My question is how can I ask vim to programatically format
this:
R) DT = data.frame(x=rnorm(6),y=rnorm(6))
R) DT
x y
1 -0.0007499 0.7661
2 1.5559552 -0.6664
3 0.2082094 -0.1598
4 -0.1684766 -0.0536
5 3.1014233 -0.7439
6 0.1985104 2.0415
R) DT2 = data.frame(x=rnorm(7),y=rnorm(7))
R) DT2
x y
1 -0.005116 -0.4388
2 2.317381 0.6597
3 0.359883 0.4770
4 -1.426220 0.4911
5 0.468820 -0.3260
6 0.626147 -0.6254
7 -1.086660 2.1973
to this: side by side
R) DT = data.frame(x=rnorm(6),y=rnorm(6)) R) DT2 = data.frame(x=rnorm(7),y=rnorm(7))
R) DT R) DT2
x y x y
1 -0.0007499 0.7661 1 -0.005116 -0.4388
2 1.5559552 -0.6664 2 2.317381 0.6597
3 0.2082094 -0.1598 3 0.359883 0.4770
4 -0.1684766 -0.0536 4 -1.426220 0.4911
5 3.1014233 -0.7439 5 0.468820 -0.3260
6 0.1985104 2.0415 6 0.626147 -0.6254
7 -1.086660 2.1973
EDIT:
Thanks everybody, Kent solution worked, strangely before set ve=all my C-Q did not allow to select a block like
############
###############
#########
#########
#########
I couldn't reach the last column of the second line (using gvim on win XP).
Now works very well
I'll also use Ingo Karkat plugin"
try following steps:
:set ve=all
move cursor to beginning of line R) DT2 = data.frame(x=rnorm(7),y=rnorm(7))
press C-V, then using motion magic to select the 2nd block
press d
move cursor to the first line, the position you want to paste the 2nd block. type p
if your text is always like that format, you could try this mapping:
:nnoremap <leader>mt :let &ve='all'<cr>gg/^R)<CR>n<c-v>G$dgg$3lp
then you could in normal mode type <leader>mt to do the transformation.
NOTE
I have no experience of vim on windows... so you may have to change the mapping for windows blockwise selection. <C-Q> ? not sure.
I assume the first line of the file is R)...
this will paste the 2nd block to the position: 3 spaces after the end of 1st line, change the number 3 in mapping if you want to adjust it
The key to this is blockwise visual mode. Go to the beginning of the second block (what should become the right column next to the first block), press Ctrl+V (on Windows, this is often remapped to Ctrl+Q), create the selection with $ and jjj. . ., then delete with d. Then go to the first line of the first block, $ to go to the end of line, (optionally append padding whitespace), then paste with p . The second block should be pasted to the right of your cursor. Finally, you can delete the empty lines left where the second block was.
My UnconditionalPaste plugin simplifies this task: You can then simply delete the second block with dd or Vjjj. . .d, and force a paste in blockwise mode with the plugin's gdp mapping.
If you find a way to determine the size of the blocks (the } key, which jumps to the next empty line, may come handy here), you can then put all of the steps into a custom mapping.
Related
I have the following function declaration:
function f1(s)
real f1,s
f1 = 1/s
end
I would like to copy the same block but with increasing function name, i.e. f1, f2, f3, ... in order to get this:
function f1(s)
real f1,s
f1 = 1/s
end
function f2(s)
real f2,s
f2 = 1/s
end
function f3(s)
real f3,s
f3 = 1/s
end
My present approach is to copy the block in visual mode, paste it several times and then rename the functions manually. I would like to know how to achieve this faster using Vim. Thanks.
You can always use a recording (see :h recording). Assuming you have a blank line before and after your function and no blank lines in between
// empty line here, cursor here
function f1(s)
real f1,s
f1 = 1/s
end
// empty line here
With the cursor on the empty line above, make a recording to any register you like. Here I'm using register c. Press qc then press y}}Pjw*Ne^Ane^Ane^A{ and exit with q.
Explanation
y} - yank next paragraph
} - move down one paragraph
P - put above this line
j - move one line done
w - move to next word
* - search for word under cursor ( this is the function name here)
N - search backwards ( we moved with * to get the pattern into the register )
e - go to end of word
^A - Ctrl a to increase the number
n - go to next match / search forward ( this is the function name )
e - go to end of word
^A - increase the number
n - go to next match / search forward
e - go to end of word
^A - increase the number
{ - move up one paragraph (same relative position as in the beginning, but at the inserted function f2 )
Now you can use #c to copy the function and increase all numbers. Prefix with the count you want, e.g. 5#c will copy the function 5 times and adjust the numbering.
In case you don't want to remember the string y}}Pjw*Ne^Ane^Ane^A{ you can paste it in the vim buffer. You will have to replace the ^A before yanking though. Delete ^A and when in insert mode press Ctrl va. ( If you are inside a screen session you will have to press Ctrl aa, this is CTRL-a and a)
With the cursor on the line in normal mode press "cY to yank it in register c.
Then you can replay it with #c.
This way you can also modify it or yank it to another register.
Use let #c=y}}Pjw*Ne^Ane^Ane^A{ in your .vimrc to have it always load to register c when starting vim.
I figured out the solution. It may seems complex but does not, cause you have just to copy this function to the clipboard and load the function with:
:#+
Below the function with additional features
fun! CopyAndIncrease()
try
let l:old_copy = getreg('0')
normal yip
let #0 = substitute(#0,'\d\+','\=submatch(0) + 1','g')
exec "normal }O\<Esc>p"
finally
call setreg('0', l:old_copy)
endtry
endfun
command! -nargs=0 CopyIncrease silent call CopyAndIncrease() | exec "normal \<Esc>"
let mapleader = ','
nnoremap <Leader>c :CopyIncrease<CR>
Now pressing ,c Now you can use the keybinding and commands defined in the function.
Function's explanation:
let l:old_copy = getreg('0') -> save copy register
normal yip -> copy block
let #0 ... increase numbers on copy register
exec "normal }O\<Esc>p" -> paste increased block
g is a prefix to several commands. e.g. goto to move the cursor, but also gqip to format a paragraph. Where is the reference for all commands that are prefixed with g?
Vim's documentation is http://vimdoc.sourceforge.net/. If you go for the HTML docs, you will find |reference_toc| More detailed information for all commands, which includes |index.txt| alphabetical index of all commands, which -- due to an unfortunate quirk with the doc file named index.txt and linked as index.html -- doesn't actually lead to where you would expect it to lead.
Long story short, http://vimdoc.sourceforge.net/htmldoc/vimindex.html#g is the documentation you are looking for ("Commands starting with 'g'").
Alternatively, type :help *g* in Vim.
(Sorry merlin2011 but your list is somewhat incomplete...)
Some reformatting applied:
2.4 Commands starting with 'g'
char note action in Normal mode
------------------------------------------------------------------
g CTRL-A only when compiled with MEM_PROFILE
defined: dump a memory profile
g CTRL-G show information about current cursor
position
g CTRL-H start Select block mode
g CTRL-] |:tjump| to the tag under the cursor
g# 1 like "#", but without using "\<" and "\>"
g$ 1 when 'wrap' off go to rightmost character of
the current line that is on the screen;
when 'wrap' on go to the rightmost character
of the current screen line
g& 2 repeat last ":s" on all lines
g'{mark} 1 like |'| but without changing the jumplist
g`{mark} 1 like |`| but without changing the jumplist
g* 1 like "*", but without using "\<" and "\>"
g0 1 when 'wrap' off go to leftmost character of
the current line that is on the screen;
when 'wrap' on go to the leftmost character
of the current screen line
g8 print hex value of bytes used in UTF-8
character under the cursor
g< display previous command output
g? 2 Rot13 encoding operator
g?? 2 Rot13 encode current line
g?g? 2 Rot13 encode current line
gD 1 go to definition of word under the cursor
in current file
gE 1 go backwards to the end of the previous
WORD
gH start Select line mode
gI 2 like "I", but always start in column 1
gJ 2 join lines without inserting space
["x]gP 2 put the text [from register x] before the
cursor N times, leave the cursor after it
gQ switch to "Ex" mode with Vim editing
gR 2 enter Virtual Replace mode
gU{motion} 2 make Nmove text uppercase
gV don't reselect the previous Visual area
when executing a mapping or menu in Select
mode
g] :tselect on the tag under the cursor
g^ 1 when 'wrap' off go to leftmost non-white
character of the current line that is on
the screen; when 'wrap' on go to the
leftmost non-white character of the current
screen line
ga print ascii value of character under the
cursor
gd 1 go to definition of word under the cursor
in current function
ge 1 go backwards to the end of the previous
word
gf start editing the file whose name is under
the cursor
gF start editing the file whose name is under
the cursor and jump to the line number
following the filename.
gg 1 cursor to line N, default first line
gh start Select mode
gi 2 like "i", but first move to the |'^| mark
gj 1 like "j", but when 'wrap' on go N screen
lines down
gk 1 like "k", but when 'wrap' on go N screen
lines up
gm 1 go to character at middle of the screenline
go 1 cursor to byte N in the buffer
["x]gp 2 put the text [from register x] after the
cursor N times, leave the cursor after it
gq{motion} 2 format Nmove text
gr{char} 2 virtual replace N chars with {char}
gs go to sleep for N seconds (default 1)
gu{motion} 2 make Nmove text lowercase
gv reselect the previous Visual area
gw{motion} 2 format Nmove text and keep cursor
gx execute application for file name under the
cursor (only with |netrw| plugin)
g#{motion} call 'operatorfunc'
g~{motion} 2 swap case for Nmove text
g<Down> 1 same as "gj"
g<End> 1 same as "g$"
g<Home> 1 same as "g0"
g<LeftMouse> same as <C-LeftMouse>
g<MiddleMouse> same as <C-MiddleMouse>
g<RightMouse> same as <C-RightMouse>
g<Up> 1 same as "gk"
note: 1 = cursor movement command; 2 = can be undone/redone
Open vim. Type :help g.
2.4 Commands starting with 'g' g
tag char note action in Normal mode
------------------------------------------------------------------------------
g_CTRL-A g CTRL-A only when compiled with MEM_PROFILE
defined: dump a memory profile
g_CTRL-G g CTRL-G show information about current cursor
position
g_CTRL-H g CTRL-H start Select block mode
g_CTRL-] g CTRL-] :tjump to the tag under the cursor
g# g# 1 like "#", but without using "\<" and "\>"
g$ g$ 1 when 'wrap' off go to rightmost character of
the current line that is on the screen;
when 'wrap' on go to the rightmost character
of the current screen line
g& g& 2 repeat last ":s" on all lines
g' g'{mark} 1 like ' but without changing the jumplist
g` g`{mark} 1 like ` but without changing the jumplist
gstar g* 1 like "*", but without using "\<" and "\>"
g+ g+ go to newer text state N times
g, g, 1 go to N newer position in change list
g- g- go to older text state N times
g0 g0 1 when 'wrap' off go to leftmost character of
the current line that is on the screen;
when 'wrap' on go to the leftmost character
of the current screen line
g8 g8 print hex value of bytes used in UTF-8
character under the cursor
g; g; 1 go to N older position in change list
g< g< display previous command output
The list above has been truncated for readability.
Suppose I have the following text (I have numbered the lines for clarity) and the cursor is at the beginning of the 5th line:
1
2 var x = 1;
3 var y = 2;
4
5 if (true) {
6 print("Hey!");
7 }
Okay, now I try to cut the lines 5, 6, 7 (all that "if" thingy). For that purpose I do:
Vjjd. Now it appears I am at the beginning of the 4th line which is an empty string.
My question: is it possible at this moment to remove the 4th line without loosing previously copied lines 5, 6, 7 (that "if" thingy), so that I'll be able to paste them somewhere else, say, on the 1st line later?
You can always yank or delete into a register using "n, where n is just about any key. See a list of available registers in "help registers", some of which have special meaning. For example, you could do:
> "a3dd (to delete the last three lines into a register called a)
> dd (to delete the blank line)
> "ap (to paste the a register)
You can also use Vjj"ad, to match what you were doing in the original question.
Yes: You can use the blackhole buffer register: type "_dd
if your #4 line is empty line, it is easy, you don't have to play with register. just do:
kJ
it means:
k: move to #3
J: (shift-J) Join #3 and #4
or you prefer do it in INSERT mode.
i<BS>
or
I<c-u>
if that line is not empty:
using register to store the 3 lines or #4, like #Derek suggested
using blackhole register like #Jan suggest
or using numbered register.
say, now you just did 3dd (without named register), and cursor on a not-empty line (#4), you could directly do dd. the 3 lines are not gone. you can paste them again by:
"2p
I have an input text as follows -
(command (and (A B C) ))
(command (and (D E F) ))
(command (and (G H I) ))
...
...
I would like to copy and paste part of text on the same line as
(command (and (A B C) (A B C)))
(command (and (D E F) (D E F)))
(command (and (G H I) (G H I)))
...
...
Will it be possible to do it using VI Editor automatically?
Update :
I think I missed one important point that the values A,B,C ... I... can have variable length. I just used them as symbols.
Thanks !
If all the lines are the same length and format as in your example:
With cursor anywhere on or inside of parens (A B C):
va(Ctrl+v
Now you have (A B C) selected and are in block select mode. Use any mechanism to block select downward. If it is a few lines, you can just move downward. If it is many you can add a count, or use a search (/) or end of file Shift+g.
Once you have selected all:
y/)Enterp
This will yank (y) the whole block, move to the close paren, and paste the block after it (p).
If the lines vary in length or otherwise cannot be reasonably selected as a block
You can use a pattern replacement. This is specific to your example, where we are looking for the pattern (A B C) where A, B and C are capital letters contained in parentheses and separated by spaces. We take a match of that pattern plus the following space, and replace it with the match of that pattern, a space, and the pattern match again.
:%s/\(([A-Z] [A-Z] [A-Z])\) /\1 \1/
Yes, several ways to do this in vim (as with most things). I would probably opt for a quick macro: go to the first line and hit qa from normal mode to start recording a macro named "a". Now do the edit on that line manually. Of course you'll want the operations to be generic, so don't just type in the values, use yank and put to copy it. Once the edit is done, escape to normal mode and press j to move down to the next line (this will set you up to run the macro on the next line). Hit q again to stop recording, then type #a to execute the macro on the next line, then hit it again to run it on the next line, etc. Or, once you do #a once, you can do ## to run the same macro again. You can also supply a count to ## to do is several times.
Alternatively, you can do a regex with the :s command, but it depends on what your lines actually look like and how good you are with regex.
(these work for me in vim)
using block select:
14l<C-v>jj6ly7lp
using macro (if lengths are varied):
record the macro using:
qqf(;vf)y;pj0q
and then repeat as neccessary:
100#q
works for a file with 100 lines
I combine the techniques given by bmearns and Kev.
So what I did is as follows
start recording the macro by q.
/( to find the opening bracket, so it goes to the second one.
n to goto the third one.
v to mark the visual block
/) to search for the end of the bracket
y to copy the visual block
n to goto next ) bracket
One time arrow key to go next to the closing bracket
p to paste the visual block
Down Arrow key to goto next line.
Home Key to goto first location of the next line.
q to stop recording the macro
#a to do the same operation for all the lines.
And it worked just completely fine !
Thanks a lot guys !
I'm trying to get away from my arrow use but there is one thing I've yet to solve without using the arrow keys. Take this example:
var1 = "1"
var2 = "2"
var3 = "3"
var4 = "4"
Now I want this to be:
var_1 = "1"
var_2 = "2"
var_3 = "3"
var_4 = "4"
Using arrows I would just goto the var1, insert and add the underscore and then arrow down and do the same thing. The problem with using hjkl is I can't be in insert mode so I have to esc out, move down, insert...rinse repeat which required more work. Is there another way to accomplish this?
You can also use a visual block insert:
go to the "1" in "var1"
press CTRL+V
go down with j to select all the rows you wish to affect
I (that's capital i)
_
<ESC>
The underscore should now be inserted at the correct place in all the rows selected (for some reason it takes a second for it to happen on my machine)
There are many ways to do this. Using movement commands for example:
1G0 → Go to the start of the first line
f1 → go to the first occurence of "1"
i_<ESC> → insert "_" and go back to normal mode
j. → go down a line and repeat the insert command
j. → go down a line and repeat the insert command
...
Or, better yet, use an "ex" command:
:%s/var/var_/
Or even with the visual block command, as johusman notes.
Assuming you're at line 1, character 1...
Using a macro:
qqfra_<Esc>+q3#q
q Record macro
q Into register q
f find
r 'r'
a append
_ underscore
Esc Normal mode
+ Start of next line
3 Three times
# Play macro
q from register q
Par 11.
Or (better) using substitute:
:%s!r!&_<CR>
Par 9!
[Sorry... too much VimGolf!]
I tend to prefer :substitute over the visual block mode.
%s/var\zs\ze\d/_/
I always have line numbers turned on, so I'd do e.g.
1,4 s/var/var_/
This is similar to the
% s/var/var_/
answer, but it only functions on the named lines. You can use visual mode to mark the lines, if you don't like typing the range (the 1,4 prefix) in your command.