I am trying to indent several rows several times in Vim and could not figure out if there is a "direct" way to tell Vim to do so. For example, how do I indent 5 rows 5 indentation levels? I could of course do 5>>.... or Ctrl-V, mark 5 rows and then do 5>. But I was looking for something more like 5>5>, but that indents 25 lines one level instead.
You could combine block selection with what you're already doing i.e.
V5j5>
Which would select 5 rows down and indent the selected block 5 steps.
:>>>>>5
is another way.
vip5>
could solve your problem, too, if applicable.
And another slightly less practical (but very precise) one:
:,5le20
where 20 is the exact number of spaces (or tabs and spaces if :set noexpandtab) you want.
one way of doing this - you can make a macro to do that for you.
for example, to save a macro under key a, type following :
qaV5j5>q
q stands for starting/stopping typing onto register.
then, if you type #a macro from register a will be executed and your 5 lines will be indented 5 times. every repeated usage of last macro can be executed by ##.
if that helped, please check http://vim.wikia.com/wiki/Macros
Try this : :1,5s/^/ /
Related
I have a file.txt of 1000 lines and every first 10 lines need to be on one line.
The eleventh line starts on a new line and is added with lines 12-20.
How can I do this in Vim!
I'm not 100% sure I grok what you're after, but I'll give you a few hints and perhaps they'll help.
To join lines together you can use J (that's capital J or Shift-J). You can precede this with a number, such as 10, and join 10 lines together. For example, typing 10J will give you:
Now, what you really need is a macro. This sounds fancy but it's just a way or recording a set of commands. So for example you could take 10J and record it to a macro. Repeat that macro 10 times and then you do that to 100 lines.
I think that the solution to your problem would therefore be something like this:
qa10Jjjq99#a
This does this (see below for an explanation):
Explanation:
q starts recording a macro. Everything that you type afterwards, until you type q again will be recorded. The next character a records the macro to the character a. Thus, qa of qa10Jjjq99#arecords 10Jjj to a. Now, to use the macro that you have stored to a you use the # symbol followed by the letter of where you stored the macro (in this case a, because we typed qa). The 99 means repeat this 99 times, thus, 99#a means repeat what's stored in macro a 99 times.
All together qa10Jjjq99#a means: record a macro q and store it in a. Then, join 10 lines 10J and move down two lines jj before stopping the recording q. Then repeat the macro stored 99 times 99#a.
The documentation for . says:
Repeat last change, with count replaced with [count].
Is it possible to repeat one change without replacing count and without resorting to macros?
Alternately, is it possible to repeat a change multiple times without the number being interpreted as the count value to the command?
Context:
I was working on this VimGolf challenge where the objective is to reverse the alphabet:
abcdefghijklmnopqrstuvwxyz becomes zyxwvutsrqponmlkjihgfedcba
My approach was to jump to z and use "Ax to append each letter to then of register a, and then paste register a, which would contain the reversed string. To repeat "Ax, recording a macro and using 25#q works, pressing . 26 times works, but 26. and 26"Ax do not work because 26 is applied to the count of x, which makes the command attempt to delete 26 characters to the right of the cursor.
what you need here is macro, not [count]. because . repeat the last cmd. and if you give it count, as you have seen, the count would be applied on the last cmd, so it would be 26x, not pressing x 26 times. usually there is no difference, but if your cursor is at the end of the line, 26x does the same as x.
What you need is a macro, if you don't want to qx...q, there are some tricks you may want to know: (without thinking about the count of keystrokes too much)
trick1
use the = register:
26#='"Ax'<cr>
this does what you want.
trick2
use the . register.
Assume at the start point, your cursor is on the line1 col1 so, press: A"Ax<esc>U26#.
the first A jump to EOL and entering INSERT, big U undo the inserted text of current line, and save into '.' (dot) register.
Now you can "replay" the reg 26 times.
regarding this challenge, the most straightforward solution (might not be the shortest) would be: set revins option, in short: se ri.
hope it helps.
Suppose I have the piece of text below with the cursor staying at the first A currently,
AAAA
BBB
CC
D
How can I add spaces in front of each line to make it like, and it would be great if the number of columns of spaces can be specified on-the-fly, e.g., two here.
AAAA
BBB
CC
D
I would imagine there is a way to do it quickly in visual mode, but any ideas?
Currently I'm copying the first column of text in visual mode twice, and replace the entire two column to spaces, which involves > 5 keystrokes, too cumbersome.
Constraint:
Sorry that I didn't state the question clearly and might create some confusions.
The target is only part of a larger file, so it would be great if the number of rows and columns starting from the first A can be specified.
Edit:
Thank both #DeepYellow and #Johnsyweb, apparently >} and >ap are all great tips that I was not aware of, and they both could be valid answers before I clarified on the specific requirement for the answer to my question, but in any case, #luser droog 's answer stands out as the only viable answer. Thank you everyone!
I'd use :%s/^/ /
You could also specify a range of lines :10,15s/^/ /
Or a relative range :.,+5s/^/ /
Or use regular expressions for the locations :/A/,/D/>.
For copying code to paste on SO, I usually use sed from the terminal sed 's/^/ /' filename
Shortcut
I just learned a new trick for this. You enter visual mode v, select the region (with regular movement commands), then hit : which gives you this:
:'<,'>
ready for you to type just the command part of the above commands, the marks '< and '> being automatically set to the bounds of the visual selection.
To select and indent the current paragraph:
vip>
or
vip:>
followed by enter.
Edit:
As requested in the comments, you can also add spaces to the middle of a line using a regex quantifier \{n} on the any meta-character ..
:%s/^.\{14}/& /
This adds a space 14 chars from the left on each line. Of course % could be replaced by any of the above options for specifying the range of an ex command.
When on the first A, I'd go in block visual mode ctrl-v, select the lines you want to modify, press I (insert mode with capital i), and apply any changes I want for the first line. Leaving visual mode esc will apply all changes on the first line to all lines.
Probably not the most efficient on number of key-strokes, but gives you all the freedom you want before leaving visual mode. I don't like it when I have to specify by hand the line and column range in a regex command.
I'd use >}.
Where...
>: Shifts right and
}: means until the end of the paragraph
Hope this helps.
Ctrl + v (to enter in visual mode)
Use the arrow keys to select the lines
Shift + i (takes you to insert mode)
Hit space keys or whatever you want to type in front of the selected lines.
Save the changes (Use :w) and now you will see the changes in all the selected lines.
I would do like Nigu. Another solution is to use :normal:
<S-v> to enter VISUAL-LINE mode
3j or jjj or /D<CR> to select the lines
:norm I<Space><Space>, the correct range ('<,'>) being inserted automatically
:normal is probably a bit overkill for this specific case but sometimes you may want to perform a bunch of complex operations on a range of lines.
You can select the lines in visual mode, and type >. This assumes that you've set your tabs up to insert spaces, e.g.:
setl expandtab
setl shiftwidth=4
setl tabstop=4
(replace 4 with your preference in indentation)
If the lines form a paragraph, >ap in normal mode will shift the whole paragraph above and below the current position.
Let's assume you want to shift a block of code:
setup the count of spaces used by each shift command, :set shiftwidth=1, default is 8.
press Ctrl+v in appropriate place and move cursor up k or down j to select some area.
press > to shift the block and . to repeat the action until desired position (if cursor is missed, turn back with h or b).
Another thing you could try is a macro. If you do not know already, you start a macro with q and select the register to save the macro... so to save your macro in register a you would type qa in normal mode.
At the bottom there should be something that says recording. Now just do your movement as you would like.
So in this case you wanted 2 spaces in front of every line, so with your cursor already at the beginning of the first line, go into insert mode, and hit space twice. Now hit escape to go to normal mode, then down to the next line, then to the beginning of that line, and press q. This ends and saves the macro
(so that it is all in one place, this is the full list of key combinations you would do, where <esc> is when you press the escape key, and <space> is where you hit the space bar: qai<space><space><esc>j0q This saves the macro in register a )
Now to play the macro back you do # followed by the register you saved it in... so in this example #a. Now the second line will also have 2 spaces in front of them.
Macros can also run multiple times, so if I did 3#a the macro would run 3 times, and you would be done with this.
I like using macros for this like this because it is more intuitive to me, because I can do exactly what I want it to do, and just replay it multiple times.
I was looking for similar solution, and use this variation
VG:norm[N]I
N = numbers of spaces to insert.
V=Crtl-V
*** Notice *** put space immediate after I.
What's the easiest way to delete the first 2 spaces for each line using VIM? Basically it's repeating "2x" for each line.
Clarification: here the assumption is the first 2 characters are spaces. So the question is about doing indentation for multiple lines together.
Enter visual block mode with Ctrl-V (or Ctrl-Q if you use Ctrl-V for paste);
Select the area to delete with the arrows;
Then press d to delete the selected area.
Press Esc
Some more options. You can decided which is the "easiest way".
Remove the first 2 characters of every line:
:%normal 2x
Remove first 2 characters of every line, only if they're spaces:
:%s/^ /
Note that the last slash is optional, and is only here so that you can see the two spaces. Without the slash, it's only 7 characters, including the :.
Move indentation to left for every line:
:%normal <<
You could also use a search and replace (in the ex editor, accessed via the : character):
Remove first two characters no matter what:
%s/^.\{2}//
Remove first two white space characters (must be at the beginning and both must be whitespace... any line not matching that criteria will be skipped):
%s/^\s\{2}//
Assuming a shiftwidth=2, then using shift with a range of %
:%<
Two spaces, or two characters? (2x does the latter.)
:[range]s/^ //
deletes two blanks at the beginning of each line; use % (equivalent to 1,$) as [range] do to this for the entire file.
:[range]s/^..//
deletes the first two characters of each line, whatever they are. (Note that it deletes two characters, not necessarily two columns; a tab character counts as one character).
If what you're really doing is changing indentation, you can use the < command to decrease it, or the > command to increase it. Set shiftwidth to control how far it shifts, e.g.
:set shiftwidth=2
I'd try one of two approaches:
Do column editing on the block to delete using Ctrl+V (often mapped to Ctrl+Q).
Record a macro on the first row using q1 (or any other number/letter you want to denote the recording register), then replay that macro multiple times using #1 (to use my previous example. Even better, use a preceding number to tell it how many times to run - 10#1 to run that macro 10 times, for example. It does, however, depends on what you recorded - make sure to rewind the cursor 0 or drop one line j, if that's relevant.
I'd also add: learn how to configure indentation for vim. Then a simple gg=G will do the trick.
I have word lists where the word or expression in Spanish is separated by its translation with a colon (":"). I want to make two columns, one for Spanish, the other for English. In vim I tried with
:%s/:/^I^I^I/g
But it does not give the desired output. The different columns are not aligned.
When deleting the colon by hand and inserting the number of tabs with the same amount of tab strokes, it always ends up aligned.
Any idea how to solve this, preferably in vim?
On a Linux/*nix system I use column(1)
:%!column -s':' -t
followed by
:%retab!
I'd probable do a
:s/:/^I/g
followed by a
:set ts=25
Where 25 is the length of the longest word expected + 2. So, if you expect the longest word of your input (on the left side of the colon) to be 12 characters, I'd choose something around 14 instead.
With Tabular.vim it's quite easy, just type :Tab /:\zs and it does the rest.
When in insert mode a setting of yours makes tab to reach a nth column. This is set with the 'softtabstop' settings if I am right.
For those tasks you could use a plugin like Align.vim or Tabularize.
Another option is to insert a huge number of spaces, and then use Visual Block with < operator as many times as necessary, if you have to do it once. Otherwise prefer reusable methods.