How to wrap some text in vim with '', "", (), [], {}, etc? - vim

I'm new to vim. And I'm pressing too many buttons doing basic text wrapping:
string -> "string"
long string with many words -> 'long string with many words'
a + b * c -> (a + b) * c
(elem0, elem1, elem2) -> [elem0, elem1, elem2] (optional)
I'm doing all that manually: go to begin, Insert mode, press key, Normal mode, (the same for second character).
How to do it faster? E.g.: visually select the text, smart-wrap it with what you need. Or even without visual selection.

string -> "string"
ciw"<C-r>""
long string with many words -> 'long string with many words'
veeeeec'<C-r>"'
a + b * c -> (a + b) * c
vwwc(<C-r>")
(elem0, elem1, elem2) -> [elem0, elem1, elem2] (optional)
"edibxs[<C-r>e]
That one is a bit more complicated:
"edib cut the content of those parentheses into
an arbitrary register, here I used "e
xs cut the closing parenthese then cut the opening one
and enter insert mode
[<C-r>e] insert the opening bracket, followed by the content of
register e, followed by the closing bracket
But yeah, use Tim Pope's Surround.

You can use visuall mode for this. For example you have string. ^ will be cursor positioning. Start in normal mode
1. string # press viwc(your word will be selected and deleted to unnamed register)
^
2. # press " and then <C-r>"(this will paste your selected text) and then press again "
This method can be with any surrounding parenthesis or brackets and with any number of words. you just need to change your selection in visual mode

Related

In Vim, paste over text between two parenthesis?

Something that I find myself doing often is yanking the text between two parenthesis and pasting that over another pair of parenthesis. For example:
foo(int a, int b, int c)
bar(int d, int e)
becomes
foo(int a, int b, int c)
bar(int a, int b, int c)
Is there a quick way in Vim to yank the text from foo and paste it over the text in bar?
Yank the content of the first pair of parentheses:
yib
Visually select the content of the second pair of parentheses and put:
vibp
One way would be yi) inside foo's arguments and "_di)P within bar's arguments.
yi) yanks the text inside the parentheses
"_di)P uses the null register to delete the text inside the parentheses and pastes the text, vi)p also works and avoids the null register
The only thing changing is the function name though, so you could also just yank the line and use cw (change word) to change foo to bar.
Cursor over the first paren of foo, then use y% to yank all the text until the matching paren. (You can also use v%y if you prefer to visually see the text you're yanking.)
Then cursor over the first paren of bar, then use v%p. It selects the text up until the matching paren then pastes over it.
Use this to go to last parenthesis shift + 5.
Press 5 twice for the first parentheses.
I use vim-scripts/ReplaceWithRegister.
Copy as usual with
yi(
Paste with gri(

White spaces before and post equal sign, brackets and other

I use Visual studio 2012 and plugin Visual assist X ver. 1916. I have two questions.
1) When I write equal sign (=), I want automatic insertion of white spaces before and after the sign (I must always insert by space on keyboard...). Is it possible?
Example:
int variable=167;
->
int variable = 167;
or
"=" -> " = "
2) I want automatic insertion of white space before and after brackets and before and after commas. Is it possible?
Example:
void fun(int param1,int param2);
->
void fun ( int param1, int param2 );
VASSISTX -> Tools -> Edit VA Snippets.
Add new one with shortcut "="(without the "") and in the body write " = "(without the ""). Same goes for the rest of your requirements, the rule is simple: in shortcut write the symbol you want to be identified when typed and in the body what you actually want to be printed.

Vim Copy and paste text on the same line

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 !

English mnemonics to Vim's shortcuts

I realize that Vim's main author is Dutch, so I'll settle for those as well. I'm interested, do Vim's "control keys" have equivalents in the english language? You know, Ctrl-O for Open, Ctrl-N for New and so on.
Some of Vim's "control keys" could be assigned some meanings
(a) append / (i) insert
(w) word / b (back one word)
These are just those that I thought off the top of my head.
Do they all have some meaning (:e ?)
I find it much easier to remember them if I know they mean something; they're not just randomly used keys.
In normal mode:
a: append
b: beginning (of current or previous word)
c: change
d: delete
e: end (of current word)
f: find (next given character on current line)
g: go (used as "leader" for many commands)
h: left (only makes sense on the keyboard used by vi's author, same for jkl)
i: insert
j: down
k: up
l: right
m: mark
n: next (occurrence of last search)
o: open (new line below current line)
p: put (paste)
q: quote? (record a macro in given register)
r: replace
s: substitute
t: toward (next given character on current line)
u: undo
v: (enter) visual mode
w: (next) word
x: x-out (delete a single character)
y: yank
z: fold (it's visual, it looks like a folded sheet of paper)
Some do. Check out this cheatsheet, it has a lot of mnemonics:
http://michael.peopleofhonoronly.com/vim/
Some of the more obvious ones:
y = yank
c = change
O = over
f = find
r = replace
u = undo
t = unTil character
My mnemonic for ^ (go to beginning of line): ^ looks like a roof, roof symbolizes home. Home key moves your cursor to the start of line/document.
Look at ADM-3A keyboard layout: the Home key is used to print ^ and ~ symbols.

How to add a word at the beginning of multiple lines in vim?

In Vim,
How do i add a word at the beginning of all lines?
Also how do i add it at end?
Eg..
If i have
A
B
C
D
I want to make it to
int A =
int B =
etc..
use visual block mode (Ctrl-v) to select the column you want, and then hit I, type the characters you want, and then hit Esc
So in this case, you'd put your cursor on A, hit Ctrl-v, go down to D, hit I and type int (it'll only appear on the first line while you type it), and then hit Esc at which point it'll apply that insert to all visually selected portions.
This works for anywhere in the document, beginning of line or end of line.
:he v_b_I for more info on Visual Block Insert
You can do this:
:%s/^/at the beginning/
:%s/$/at the end/
:%s/.\+/int & =
+ won't match on empty lines
If you need to copy just the first word, then do:
:%s/^\w\+/int & =/g
If you want to preserve indentation, then do:
:%s/^\(\s*\)\(\w\+\)/\1int \2 =/g
A global substitute should do i:
:%s/.\+/int & =/
This is how it works: in the second part of the substitution (ie in the int & =) the ampersand is replaced with what machted in the first part (the .*). Since .* matches the entire line, each line is subsituted as wanted.
If you have empty lines (in which you don't want to have any replacements), you could go with a
:%s/^\S\+$/int & =/

Resources