1. Overview

Deleting a specific column in a file within Vim can be useful in several use cases, mainly when working with structured data or text files.

In this tutorial, we’ll learn how to delete a specific column in a file within Vim.

2. Scenario Setup

Let’s take a look into the sample text file, data.txt that contains columnar content:

$ cat data.txt
a11 a12 a13
b21 b22 b23
c31 c32 c33

We want to delete the second column in data.txt in a generic way so that we can reuse our approach in other similar scenarios.

3. Using Visual Block Mode

In Vim, the visual block mode is a powerful feature that we can use to select and manipulate text in a column-wise (rectangular) fashion. Let’s use this approach to solve our use case of deleting a specific column in data.txt.

First, we must open the file in vim:

$ vim data.txt

Now, let’s place the cursor at one of the corners of the block we want to delete. In this scenario, we can use the w key to place the cursor at the starting position of the second column by jumping over the word in the first column:

Placing Cursor - Deleting Specific Column in Vim

Alternatively, we could also use j, k, h, l, and so on to navigate the file and move the cursor to any corner positions of the second column.

Next, let’s enter into the visual block mode by pressing the ctrl+v keys together:

Visual Block Mode - Deleting Specific Column in Vim

Now, we can move the cursor toward the diagonally opposite corner to select the block that we want to delete:

Section in visual block mode deleting specific column in Vim

We must note that we’ve also selected the white space after the second column.

At this point, we’re ready to delete the second column by pressing the d key:

Delete-Operation-Deleting-Specific-Column-in-Vim

Lastly, we can save the file and exit from Vim:

:wq

That’s it! We’ve successfully deleted the second column from the data.txt file.

4. Using Substitution in Command Mode

Substitution is another popular way to delete the second column in the data.txt file within Vim. In this approach, we can visualize each line into three groups using regular expressions:

  • The first group ([^\t]\+\t){1} comprises the first column followed by a tab.
  • The second group ([^\t]*\t) comprises the content to be deleted. It contains the second column and a (\t) tab character.
  • The third group (.*\) comprises the rest of the content.

Now, let’s open the data.txt file in Vim:

$ vim data.txt

Then, we can execute the substitution command (s) over all lines (%):

:%s/\([^\t]\+\t\)\{1\}\([^\t]*\t\)\(.*\)/\1\3/

It’s important to see that *we escaped a few characters in the search regular expression, such as (, ){, }, ? and* +. Further, we used \1 and \3 in the substitution string as a backreferencing for the first and third groups, respectively.

Lastly, we can use the w command to save the file after verifying the content:

Subsitution - Deleting Specific Column in Vim

Fantastic! It looks like we nailed this one.

5. Using a Macro

Vim offers the powerful feature of macros that we can use to record and replay a sequence of commands and keystrokes. In this section, we’ll record and play a macro to delete the second column in the data.txt file.

First, we need to open data.txt in Vim and start recording the macro in the normal mode:

$ vim data.txt
qd

We pressed the d key after q to record the macro in the d register.

Now, we can move the cursor after the first word by pressing w and then delete the word using dw:

wdw

As a result, the entry from the second column in the first row will be deleted.

Further, we’ve recorded all the necessary keystrokes in the macro. So, it’s time to stop the recording:

q

Next, we can play the macro from the second line to the last line ($) of the file using the @d command:

:2,$normal @d

Great! The delete operation is successful for the remaining lines.

Lastly, we must note that we used the normal keyword because we’re executing command keys from the normal mode in command mode (:).

6. Using External Commands

Vim provides the %! command us to filter text by passing it through an external command, such as grep, awk, cut, etc. In this section, let’s explore a few ways to use an external command to solve our use case of deleting the second column from the data.txt file.

6.1. With cut

The cut command is one of the convenient ways to filter columnar content. We can use the -f option and provide the list of fields that we want to keep:

$ cut -f <field_list> <input>

Further, we can specify the field list in several ways:

  • f1, f2,…, fn for specifying a comma-separated list of fields
  • n for specifying only the nth field
  • n- for specifying all fields from the nth index
  • n-m for specifying all fields between nth and mth indices (included)
  • -m for specifying all fields from 1st to mth index

Accordingly, we can use 1,3- to show all fields except the second one.

Now, we understand how to use cut to select specific fields. So, let’s open the data.txt file in Vim and execute the command:

$ vim data.txt
:%!cut -f1,3-

We must note that % in %! represents all lines from the current open file in Vim. So, we don’t need to explicitly pass the filename as an argument.

Further, let’s verify the content in the file and that Vim filtered three lines:

Filter Using Cut Command - Deleting Specific Column in Vim

Finally, we should save the file by using the w command:

:w

Great! We solved our use case correctly.

6.2. With awk

Awk is another powerful utility that we can use for filtering text. Let’s write a short script where we pass a parameterDELETE_COLUMN parameter, for filtering the rest of the columns from the input:

$ cat script.awk
{
    line = ""
    for (i = 1; i <= NF; i++) {
        if (i != DELETE_COLUMN) {
            line = line $i (i < NF ? OFS : "")
        }
    }
    print line
}

Internally, we’re iterating over the field indices and recreating the record by concatenating all the fields except the one positioned at the DELETE_COLUMN index.

We’ve completed a one-time script development that we can reuse within Vim to delete any specific column. So, let’s open the data.txt file for processing:

$ vim data.txt

Next, let’s set DELETE_COLUMN to 2 while calling the script.awk script for deleting the second column in data.txt:

:%!awk -v DELETE_COLUMN=2 -v OFS="\t" -f script.awk

We also explicitly passed OFS (Output Field Separator) as our file is tab-delimited.

Like earlier, we can save the file using the :w command after verifying the content:

Filter using awk - Deleting Specific Column in Vim

Perfect! We got this one right.

7. Conclusion

In this article, we learned how to delete a specific column in a file in Vim. Furthermore, we explored important concepts, such as visual block mode, group substitution, external command execution, and macro recording, while solving the use case.