How to replace the white blanks with four in vim? - vim

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.

Related

delete first 2 white spaces from start of line in gvim

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 %.

vim add text to beginning of multiple lines excluding white space with substitution

I have the following paragraph:
1 sometexthere
2 indented text
3 indented text
4 indented text
I would like to use vim's search and replace command to add some text right before indented text. For example:
1 sometexthere
2 test: indented text
3 test: indented text
4 test: indented text
Is there a way to use vim's search and replace syntax to achieve the following results? I have tried commands like
2,4s/^/test: / but still can't figure out a way to strip off the whitespace.
Matching leading whitespace and replacing it by itself:
%s/^\s\+/&test: /
Or matching lines by :global and editing them by :normal
g/^\s/normal! Itest:
Unless I am misunderstanding you question you can achieve this fairly simply like:
%s/ /\ test:/g
This gives the output you desire from the given input.
For this though, i prefer to use a macro.
qq # Begin recording the macro
/____ # Search for four spaces
4li # Insert four characters to the right
test: # Type the desired text
<ESC>q # Exit insert mode and save the macro to the q register
Then to run this macro on the next indent go #q. Just repeat #q or ## to keep running the macro until everything is indented.
Another alternative route, which is fairly readable, is to use the normal command.
/____ # Search for the indent.
:'<,'> normal 0nitest: # Inserts "test:" 4 characters right of the search result. you can replace the <,> with a range of course.

Matching dashes and newlines with Lua pattern

I am trying to split this file with the delimiter of 2 line breaks, 4 dashes in a row (----) and another 2 line breaks. Basically \n\n----\n\n.
The pattern I'm tossing into gmatch is "([^\n\n----\n\n]+)" but for some reason it's also matching instances of ---- that don't have the line breaks around it.
You need to escape - with %-.

How to select a vertical block with exceeding lines?

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.

Delete whitespaces before selected lines

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.

Resources