Surround Several Words With Quotes At Once In vim - vim

I was curious if there is a way to surround several words at once with quotes using vim. I am using tpope surround and repeat but I was wondering if there is a command like
3ysw"
so from
one two three
to
"one" "two" "three"

You can visually select the range with v3e, and then run a substitution command on it: :s/\v(\w+)/"\1"/g (the range '<,'> should automatically be inserted).
Personally though, I'd rather surround one word with ysw", and then do w.w. (repeat as often as needed).
Alternatively, record a macro that does both steps (surrounding and moving on to the next word), then call it n times:
qqysw"3wq
After this is in your q register, you can then call 2#q to perform the surroundings on the remaining words.

When you want to enquote three words, beginning with the one your cursor is currently placed within, you can do:
bv3ec'<Ctrl+r>"'
b places the cursor at the beginning of the current word, v enters visual mode, 3e jumps at the end of the current 3-word sequence, c cuts the selection and enters insert mode, where you insert the left enclosing quote ' and press <Ctrl+r>" in order to paste current contents of the clipboard buffer, before you insert the other enclosing quote '.
Omit the leading b if you start off with the cursor at the first character of the first word.

Another substitution option
s,\w\+,"&",g
s ............. substitute current line (add %s for the whole file)
\w\+ .......... one word or more
"&" ........... & represents the whole match on the search part
g ............. every occurrence on the line
OBS: When using substitution we can use a different delimiter in order to make easy to type. (Also useful when searching for things like "/my/pattern/")

Related

Replace a word (or many words) with spaces of same length?

In a general sense, my question is how do I do something like "dw" or "dd", but instead of deleting characters, I want to over-write with spaces?
E.g. lets say I have text:
first second third
if the cursor is on the "s" in second, I can hit "dw" to get:
first third
but what if I want:
first third
Is there a simple way to do that? An ideal solution would be to use the "d" style syntax (e.g. dw, daw, d$, etc.) but with whitespace replacement instead of deletion.
From the start of the word,
Ctrl-v to enter visual block mode,
e to move to the end of the word (highlighting the word in the process),
r[SPACE] to replace the highlighted characters with spaces.
Because of their very nature (the next character must be consumed), r and R can't work like operators. If you want to replace a motion, visually select it first, and then do r<Space> or r_ or whatever.
In this very specific case:
ver<Space>
or:
viwr<Space>
NOTE: I used ve and viw because the semantics of w are inconsistent so I prefer to avoid it when possible.

Vim: Quickest way to copy text inside quotes

What is the most efficient way to copy text inside quotes in vim, for example hello in "hello" 'hello' '''hello''' or """hello"""? The quickest I'm able to do is:
v (enter visual mode)
w or e (get to the end of the text, approximately
h or l to get to the exact correct place
y to yank the text
Here's an example: https://gyazo.com/a2bb432dc04de58ac628327740f6c033. While I might be able to improve to get it, perhaps in 3s, doing this with a mouse would take all of 0.25s. What would be the most efficient way to do a copy-paste such as the above?
If the text is surrounded by only one pair of quotes, in this case double quotes, the most efficient way to copy that text is yi". This will copy (y) the text inside the quotes (i"), regardless of where the cursor originally is. To make this work with single quotes, brackets, parentheses, or something else, simply replace the " with the character surrounding the text.
If the text is surrounded by more than one pair of quotes, however, we must first navigate to the innermost quote before we can copy the text inside. The command above will not work, since it will see the first two quotes with nothing in between them ("").
The fastest way to navigate to the first quote is f". Then, press ; until the cursor is on the innermost quote, and we can now use yib (the ib command selects the inner block.) to copy the text inside!
It might be possible to create a mapping that will automatically move the cursor to the innermost quote and copy the text inside, but that is a bit too advanced for me.
If your cursor is inside the word you want to copy, simply press yi followed by a quote char ' or "
Make sure you also read :h text-objects

How to complete the whole list with Vim?

I have a list of products to place on a rails seed and I would like to instead of put brackets one by one on the list with a command place the brackets on the whole list?
for example:
1. Dakine
2. Dale of Norway
3. Dan Post
1. ["Dakine"],
2. ["Dale of Norway"],
3. ["Dan Post"],
I searched on the help but did not find any about. Thanks.
You can record a macro in Vim and repeat that.
If you are on number 1, you can do following:
qqf a["Esc$a"],Esc0jq
Explanation:
qq: Start recording macro in register q
f: Go to first space character
a: : Insert after (the space character from above)
\[": Insert those characters
Esc: Back to normal mode
$: Go to end of line
a: Insert after (end of line)
"],: Insert the characters
Esc: Back to normal mode
0: Jump to start of line
j: Go down one line
If you have 100 such lines, you can do 100#q to achieve your result.
With vim substitute command:
:%s/.*/["&"]/
If you don't want to operate on all lines, then select the ones you want to transform or note the related line numbers, and then type :s/..... without the %. You'll see actually :'<,'>s this range represent the visually selected lines, and vim adds it automatically in visual mode.
On Atom you can enable the find to use Regex in the search(there is a button next to the search field)
Then you can search for something like (^.*$) to get every line separated by groups and in the Replace field you use ["$1"],. The $1 represents the value matched by the Regex.
Then just do a Replace All and remove the last comma in your list if needed.

How to insert variable number of spaces necessary to align textual columns in Vim?

I’m a fan of Visual mode in Vim, as it allows to insert text before any given column.
For example, insertion of spaces after the quotation leaders below:
> one
> two
> three
can be done via <Ctrl-V>jjI <Esc>:
> one
> two
> three
as follows:
Start Visual mode with Ctrl-V.
Extend visual selection with jj.
Insert some spaces with I__.
Propagate the change to all the lines of the block selection with Esc.
Now I have a text file that needs some formatting. This is what it looks like:
start() -- xxx
initialize() -- xxx
go() -- xxx
Now I want to align part of this text to arrange it into columns like this:
start() -- xxx
initialize() -- xxx
go() -- xxx
The problem I have is that I cannot insert a different amount of indentation into each line and merely indenting a fixed amount of spaces/tabs is insufficient.
How can you do an indentation where all indented text will have to be aligned at the same column?
Update
I only figured out a rather verbose and unwieldy method:
Find the string position to indent from: \--.
Insert n (let's say 20) spaces before that: 20i <Esc>.
Delete a part of those spaces back to a certain column (let's say 15): d|15.
Save those steps as a macro and repeat the macro as often as necessary.
But this approach is very ugly, though!
I'm much better off without any vim plugins.
Here is my solution:
<Shift-V>jj:!column -ts --
Then insert -- into multiple lines just as you wrote in the question.
You can also append a number of comments at insertion time.
:set virtualedit=all
<Ctrl-V>jjA-- xxx<Esc>
You have to use a specific plugin, you can use either Tabular or Align plugin in this case.
They both allow you to align text on specific characters, like -- in your example. Their syntax is a bit different though. Pick the one that suit you the most.
Without plugin and if you have already entered your comments without emix's solution:
:,+2 s/--/ &
This will ensure all comments are to be shifted leftwise in order to align them properly.
Then blockwise select the column to which you want to align the text, and : 100<
An easy way to align text in columns is to use the Tabular or
Align plugin. If neither of these is ready at hand, one can use
the following somewhat tricky (and a little cumbersome looking) yet
perfectly working (for the case in question) commands.1,2
:let m=0|g/\ze -- /let m=max([m,searchpos(#/,'c')[1]])
:%s//\=repeat(' ',m-col('.'))
The purpose of the first command is to determine the width of the
column to the left of the separator (which I assume to be --
here). The width is calculated as a maximum of the lengths of the text
in the first column among all the lines. The :global command is used
to enumerate the lines containing the separator (the other lines do
not require aligning). The \ze atom located just after the beginning
of the pattern, sets the end of the match at the same position where
it starts (see :help \ze). Changing the borders of the match does
not affect the way :global command works, the pattern is written in
such a manner just to match the needs of the next substitution
command: Since these two commands could share the same pattern, it can
be omitted in the second one.
The command that is run on the matched lines,
:let m=max([m,searchpos(#/,'c')[1]])
calls the searchpos() function to search for the pattern used in the
parent :global command, and to get the column position of the match.
The pattern is referred to as #/ using the last search pattern
register (see :help "/). This takes advantage of the fact that the
:global command updates the / register as soon as it starts
executing. The c flag passed as the second argument in the
searchpos() call allows the match at the first character of a line
(:global positions the cursor at the very beginning of the line to
execute a command on), because it could be that there is no text to
the left of the separator. The searchpos() function returns a list,
the first element of which is the line number of the matched position,
and the second one is the column position. If the command is run on
a line, the line matches the pattern of the containing :global
command. As searchpos() is to look for the same pattern, there is
definitely a match on that line. Therefore, only the column starting
the match is in interest, so it gets extracted from the returning list
by the [1] subscript. This very position equals to the width of the
text in the first column of the line, plus one. Hence, the m variable
is set to the maximum of its current value and that column position.
The second command,
:%s//\=repeat(' ',m-col('.'))
pads the first occurrence of the separator on all of the lines that
contain it, with the number of spaces that is missing to make the text
before the separator to take m characters, minus one. This command
is a global substitution replacing an empty interval just before the
separator (see the comment about the :global command above) with the
result of evaluation of the expression (see :help sub-replace-\=)
repeat(' ',m-col('.'))
The repeat() function repeats its first argument (as string) the
number of times given in the second argument. Since on every
substitution the cursor is moved to the start of the pattern match,
m-col('.') equals exactly to the number of spaces needed to shift
the separator to the right to align columns (col('.') returns the
current column position of the cursor).
1 Below is a one-line version of this pair of commands.
:let m=0|exe'g/\ze -- /let m=max([m,searchpos(#/,"c")[1]])'|%s//\=repeat(' ',m-col('.'))
2 In previous revisions of the answer the commands used
to be as follows.
:let p=[0]|%s/^\ze\(.*\) -- /\=map(p,'max([v:val,len(submatch(1))+1])')[1:0]/
:exe'%s/\ze\%<'.p[0].'c -- /\=repeat(" ",'.p[0].'-col("."))'
Those who are interested in these particular commands can find their
detailed description in this answer’s edit history.
This is a modification on Benoit's answer that has two steps.
First step, block select text search and replace -- with lots of spaces.
'<,'>s/--/ --/
Now all the comments should have lots of spaces, but still be uneven.
Second step, block select the text again and use another regex to match all the characters you want to keep (say the first 20 characters or so) plus all the spaces following, and to replace it with a copy of those first 20 characters:
'<,'>s/\(.\{20}\)\s*/\1/
Not quite as easy as Benoit's, but I couldn't figure out how to make his second step work.

How do I repeat an edit on multiple lines in Vim?

I'm aware that in Vim I can often repeat a command by simply adding a number in front of it. For example, one can delete 5 lines by:
5dd
It's also often possible to specify a range of lines to apply a command to, for example
:10,20s:hello:goodbye:gc
How can I perform a 'vertical edit'? I'd like to, for example, insert a particular symbol, say a comma, at the beggining (skipping whitespace, i.e. what you'd get if you type a comma after Shift-I in command mode) of every line in a given range. How can this be achieved (without resorting to down-period-down-period-down-period)?
Ctrl-v enters visual mode blockwise. You can then move (hjkl-wise, as normal), and if you want to insert something on multiple lines, use Shift-i.
So for the text:
abc123abc
def456def
ghi789ghi
if you hit Ctrl-v with your cursor over the 1, hit j twice to go down two columns, then Shift-i,ESC , your text would look like this:
abc,123abc
def,456def
ghi,789ghi
(the multi-line insert has a little lag, and won't render until AFTER you hit ESC).
:10,20s/^/,/
Or use a macro, record with:
q a i , ESC j h q
use with:
# a
Explanation: q a starts recording a macro to register a, q ends recording. There are registers a to z available for this.
That's what the :norm(al) command is for:
:10,20 normal I,
If you are already using the '.' to repeat your last command a lot, then I found this to be the most convenient solution so far. It allows you to repeat your last command on each line of a visual block by using
" allow the . to execute once for each line of a visual selection
vnoremap . :normal .<CR>
I believe the easiest way to do this is
1) record a macro for one line, call it 'a'; in this case one types
q a I , ESC j q
2) select the block of lines that you want to apply the macro to
3) use the 'norm' function to execute macro 'a' over this block of lines, i.e.,
:'<,'>norm#a
I think the easiest is to record a macro, and then repeat the macro as many times as you want. For example to add a comma at the start of every line, you type:
q a I , ESC j q
to repeat that 5 times, you enter
5 # a
With your edit already saved in the . operator, do the following:
Select text you want to apply the operator to using visual mode
Then run the command :norm .
Apart from the macros, as already answered, for the specific case of inserting a comma in a range of lines (say from line 10 to 20), you might do something like:
:10,20s/\(.*\)/,\1
That is, you can create a numbered group match with \( and \), and use \1 in the replacement string to say "replace with the contents of the match".
I use block visual mode. This allows you to perform inserts/edits across multiple lines (aka 'vertical edits').

Resources