3

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

Community
  • 1
  • 1
bigTree
  • 2,103
  • 6
  • 29
  • 45
  • 1
    No need of visual mode. How about a regular expression? – Birei Feb 20 '14 at 14:57
  • @Birei could you give me a clue of how to do that please? – bigTree Feb 20 '14 at 14:58
  • How about `%s/\v^\d*\s//g` , not tested but should work – PKumar Feb 20 '14 at 15:00
  • Could you explain me these commands please? (I'd like to learn how to use RE) – bigTree Feb 20 '14 at 15:00
  • 1
    \v stands for very magic , it is not turned on so basically its not default, \d stands for digit, * stands for 0 or more number of times, \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 file. – PKumar Feb 20 '14 at 15:03

5 Answers5

6

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

and beyond

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

Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
6

This is a job for cut!

:%!cut -f 2-
Peter Rincker
  • 43,539
  • 9
  • 74
  • 101
3

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

PKumar
  • 10,971
  • 6
  • 37
  • 52
  • 1
    The `\v` option isn't needed here. It will work the same if those two characters are omitted. – Jeff Feb 20 '14 at 15:50
  • Thanks I know it is not needed , its just my habit to put it. Thanks anyways , i have put that however in my paragraph – PKumar Feb 20 '14 at 15:52
2

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
Rodrigo Gauzmanf
  • 2,517
  • 1
  • 23
  • 26
1

you have to do type

:%s/\d\+\t//
René Nyffenegger
  • 39,402
  • 33
  • 158
  • 293