Reputation: 2183
I want to delete the first column of a text file using vim. I read these SO answers where visual mode is used, but they only delete the first character (as for the answers using patters, I don't understand them so I have trouble adapting them to my situation).
My file contains many numbers and I want to delete the numbers in the first column, not just the first element.
eg
12 34 43143 1
131234 1
1 3443 132
I would like to have
34 43143 1
1
3443 132
but currently, I get
2 34 43143 1
31234 1
3443 132
The numbers are separated by tabs if it matters
Upvotes: 4
Views: 7681
Reputation: 11128
Test this : %s/\v^\d*\s//g
, you can replace \s
with \t
if it is tab delimited,
\v
stands for very magic , it is not turned on so basically its not default and it is used for avoiding masking of parenthesis("(" ),curly brackets("{") e.t.c with slashes, \d
stands for digit, * stands for 0 or more number of times of matches, \s
stands for space and % stands for current file , %s
stands for search the current file and g
stands for global substitute, by the way carret(^
) stands for start of the line, In this case you don't have to use \v
and g
, but i use it anyways.
Edit:
So this is perfectly fine : %s/^\d*\s//
, Thanks to Jeff
You can use vim help for something like
:h \v
:h \d
e.t.c to get their formal definitions
Upvotes: 3
Reputation: 2527
Another alternative is using a Macro:
qq0dawjq
Then you have to use the macro with:@q or 3@q or whatever number + @q
Explanation:
q: Start Recording a Macro
q: Save the macro in register q
0: Go to the beginning of the line
daw: Delete A Word
j: Move down to the next line
q: Finish the recording
Upvotes: 2
Reputation: 172778
(Integer) numbers are represented by [0-9]
in regular expression syntax, you can also write \d
; as there are many digits, the quantifier \+
is appended. A tab it \t
, or generally whitespace is \s
. Now, you want to replace / :substitute
the number at the beginning of a line(^
) with nothing.
:%s/^\d\+\t//
The %
applies this to the entire buffer, see :help :range
.
Soon, you'll want to delete different columns; you can assert certain matches (e.g. ^\d\+\t\zs\d\+\t
would select the second column), or use capture groups (with which you can even re-order columns!) But it'll be more tedious.
A breakthrough happens when you realize that your file format is well-known by the name tab-separated values, a variant of comma-separated values (CSV). There's a great plugin for that (csv.vim - A Filetype plugin for csv files, and it has a :DeleteColumn
command - just what you needed! (And more...)
Upvotes: 6