I have text like this:
This is a normal line.
6 spaces are before me. //line 1
4 spaces are before me. //line 2
3 spaces are before me. //line 3
6 spaces are before me. //line 4
4 spaces are before me. //line 5
Another normal line.
2 spaces are before me. But that is ok. //line 7
Line goes on.
How do I use vim to select and delete all the spaces before line 1 to line 5 ?
I'd use visual line mode (Shift+V) to select the lines I wanted, then run the substitute command on them (hitting : should automatically include the visual mark '<,'> at the start for you):
:'<,'>s/^\s*
This is useful when you're working and haven't figured out the line numbers. In this case as you know it's lines 2 to 6, you can do:
:2,6s/^\s*
A helpful option for figuring out the line numbers quickly is set number.
The substitute command is greedingly grabbing all whitespace (\s*) from the start of each line (^\s*), and replacing it with nothing (equivalent to /^\s*//).
Another quick method: You can also use the << command. Go to line 1 and enter 5<< to shift lines 1-5 to the left. Repeat with the . command until all whitespace has disappeared. This is useful for increasing indent as well by using >>.
If there is no indention defined for this file, you could use ={motion}. If the cursor is on the first line, do =G to indent to the end of the file or =} to indent to next empty line.
To do it for this and the following 4 lines use =4j.
Related
I am reading a file which has 2 unwanted spaces in each line.
import uvm_pkg::*;
`include "uvm_pkg.svh"
And it continues till last line
How to delete first 2 white spaces in each line in gvim?
Enter :%s/^ //, which substituttes 2 leading whitespaces (^ ) with nothing (the nothing between the second and third /) on every line (%).
position cursor at line 1, column 1 (press gg0)
enter visual block mode (Ctrl+v)
select first two columns of all lines by moving the cursor to the bottom and one to the right (Gl)
delete selection (x)
:%s/^ //
substitutes the two first leading spaces on every line of the buffer with nothing, effectively deleting them.
Note that, in this case, you don't even need the replacement part because it is implied: :%s/^ .
See :help :s and :help :range for the %.
There are 5 white blanks in line 3,6 white blanks in line 4,7 white blanks in line 5.
I want to replace them with 4 white blanks,how to write the command?
It is not %3,7s/^\s*/ /g ,how to fix it?
it is a test
it is a test
it is a test
I want change it into:
it is a test
it is a test
it is a test
Based on your example, it sounds like you want to replace all sequences of whitespace at the beginning of lines with 4 spaces.
Please comment if I have misinterpreted your question. You can do this by looking for an arbitrary amount of whitespace at the beginning of line and replacing with four spaces.
%s/^\s\+/ /g
You typed :%3,7s/^\s*/ /g, but % is a range and 3,7 is another range, so it's wrong : you have to choose between them:
:%s ...
will replace in the whole file, or
:3,7s ...
will only replace in lines from 3 to 7.
I am reading/learning a bit about vim, and found that macros can be used for repeating actions. My question is: is possible with vim to achieve following text transformation using macros (or some other vim magic)
Input:
- 1 Show invisibles (2:52)
- 2 Tabs and Spaces (6:22)
- 3 Whitespace preferences and filetypes (3:29)
- 4 Tidying whitespace (4:33)
January 2010
- 5 Indentation commands (5:41)
- 6 Working with buffers (3:28)
- 7 Working with windows (5:32)
- 8 Working with tabs (3:17)
February 2010
Output:
January 2010
- 1 Show invisibles (2:52)
- 2 Tabs and Spaces (6:22)
- 3 Whitespace preferences and filetypes (3:29)
- 4 Tidying whitespace (4:33)
February 2010
- 5 Indentation commands (5:41)
- 6 Working with buffers (3:28)
- 7 Working with windows (5:32)
- 8 Working with tabs (3:17)
There are steps that I would take, but do not know how to write this in vim:
If the line is starting with dash, remember it's line number and move to next line
If the line starting with dash is found again, move cursor until non dash starting line is found
If the line is found which is not starting with dash remember it's position and delete this line (dd)
Skip one line above the line number that was remembered in step 1 and Append (p) the deleted line
Increment the counter which holds non dash line by 2 (there are two spaces between records)
Jump with cursor to this incremented line and start with step 1.
Here is another, more elegant (i think) approach but it requires reading file from backwards:
If the line is found which is not starting with dash delete this line (dd)
If the line which is starting with dash is found move to next line
If empty line is found append the deleted line (p)
Move cursor until the non dash line is found again and start with step 1.
Is this possible in VIM? Thank you
try this cmd:
:g/^[^-]/norm! dd(P
Here is Kent's solution as a macro (<CR> means pressing Enter):
qq/^[^-]<CR>dd(Pq
This records into register q. You repeat via #q.
If you clear the register with qqq beforehand, and end the macro with #qq#q instead of q, you'll have a recursive macro that repeats itself until there's no more work to do. (Here, you'll need to :set nowrapscan, or it'll loop forever.)
In Vim you can use Ctrl+v to select a vertical block of code. This is pretty cool, as this way you can insert a rectangular block of text anywhere in your text. A feature I haven't seen anywhere else yet.
But say I have a text like:
1 abcde
2 abcdefg
3 abcdefg
4 abc
I want to select this full block as vertical block. If I'm on the a of line 1, and start selection, then move down to line 4, I can only move the cursor to the last character c in that line. So the lines above are cut off, giving me this selection:
1 abc
2 abc
3 abc
4 abc
Is there a way to get the full text selected as vertical block?
if you want to select exact 4 lines (including the 1st line), you could:
Ctrl-V$3j
this selects all the texts, but they are not really in a "block", because the first line and the last line have different length.
If you do want have a "block" of text, (appending spaces on those "shorter" lines), you could:
set ve=all
Ctrl-V hhhhh... jjjjj...
by setting ve to all, your cursor could go anywhere. If you like after the selection/copying, you could set the ve back to its original value.
A minute after asking this question i found it out myself. The trick is to press $ on line 4 above. So the full series of keystrokes, if the cursor is on the a of line 1 is:
Ctrl+v3j$
a quick and dirty solution is to insert 4 spaces a the end of line 4.
IS there a way to add 4 lines before the last line of the file in vim?
I'm new to vim and recently found out that vim does multiline search which is awesome. Now only if I could find how to add 4 lines before the last line in the file I would totally save immense amount of time
G 4 O Esc
G jumps to the end of the file. Then press O to insert a line before that last line. Now hit Enter three times and you have four lines before the last line.