How to delete second column in Vim?

vim
46

Solution 1

In vim, you should be able to use the command

:%s/\t[^\t]*//

(substitute TAB followed by zero or more occurrences of any character except TAB with nothing). If your file has only two columns you could use a slightly simpler :%s/\t.* or :%s/\t.*$which replace the first TAB and any following characters up to the end of the line.

Solution 2

The accepted answer is much more elegant than this (I upvoted it!) but if you do not remember it you can use vim visual block mode directly. Open vim and go (normal mode) to the first corner of the column, like this:

enter image description here

Type CTRL-V and you can move the cursor to select the column, this is midway:

enter image description here

To go at the end, press G:

enter image description here

the block seems broken because we are on the last line which is blank; simply go up one line (with the up arrow or k) to see it again...:

enter image description here

Now you simply press x to delete the block:

enter image description here

Solution 3

I would use cut for this

cut -f1,3- file.txt > newfile.txt
mv newfile.txt file.txt

You can use this as a filter within vim, too (this will replace all the lines in the file; you could also use (for example) 2,9 instead of % to process lines 2-9, or select the lines you want with V):

:%!cut -f1,3-

-f1,3- means 'print field one, followed by field three and all fields until the end of the row'. By default, cut uses a tab as its delimiter; if you need something else, use the -d option (see man cut).

Solution 4

You can try either:

:%norm WdW

or manually using visual mode:

  1. Jump into second column (e.g. by pressing: W).
  2. Start visual-block by Ctrl+v.
  3. Select second column (e.g. by pressing: W, G).
  4. Delete it by pressing d.

To do it in-place from the command line, try:

$ ex +':exe ":%norm f\<Tab>dE"' -scx file

Related: How to write literal for Tab key to use for motion?

See also:

Share:
46

Related videos on Youtube

Sheng
Author by

Sheng

Updated on September 18, 2022

Comments

  • Sheng
    Sheng over 1 year

    I have three numpy matrices x, r and r. Whose values are:

    x = np.array([[4,2],
                  [0,-1],
                  [-2,5],
                  [2,6]])
    
    y = np.array([[1,7],
                  [2,6],
                  [5,2]])
    
    r = np.array([[2,2,1],
                  [2,3,1],
                  [9,5,1],
                  [2,0,4]])
    

    What I'm going to do is:(it's hard to describe by words so I use code to present what I want to do)

    K = r.shape[1]
    D = x.shape[1]
    
    v = np.zeros((K, D, D))
    for k in range(K):
        v[k] = (r[:, k] * (x - y[k]).transpose() @ (x - y[k]))
    print(v)
    

    The final v is what I need and v is equals to

    [[[103.  38.]
      [ 38. 216.]]
    
     [[100.  46.]
      [ 46. 184.]]
    
     [[111. -54.]
      [-54.  82.]]]
    

    Is there any elegant or pythonic way to achieve this without for loops?

    Thanks

    • Admin
      Admin over 9 years
      @KasiyA the OP's original used \t to indicate tabs.
    • Admin
      Admin over 9 years
      @steeldriver Ok you are right. I thought it's part of line. removed. But I edited to correct one according to first post.thanks
    • Admin
      Admin over 9 years
      vim visual-block mode doesn't solve the problem? (ctrl-v in normal mode, select the column, x to delete)?
    • Admin
      Admin over 9 years
      @Rmano it helps thanks, but could you tell me how to go to the end of the file in visual mode? (I tried ctrl G, but it doesn't work).
  • Rmano
    Rmano over 4 years
    Yes, +1, but beware that if you have blank lines at the end step 3) can go awry...