Skip to main content Skip to navigation

2b.4 Column operations

It is also possible in vim to perform operations on columns. Unfortunately this is not covered by the tutorial. However, it can be very convenient for editing text-files that show data in a column-wise fashion. The way to select column in vim is by using the keystroke CTRL-V . To practise download the prac-col.txt (right-click on it and save it to an appropriate folder that you can access with vim). Edit the file by vim prac-col.txt

Removing columns

Let us try to remove the column containing the letters 'rh' (standing for rhodium). Move the cursor to the first row containing 'rh'. Then press CTRL-V . Now move the cursor to the bottom line and one step to the right. Press now x . You'll see that the column is removed. Undo the change by pressing u to do the next edit.

Moving columns

It is also possible to move a column. In this example we would like to move the first column data to the last column. For this we first add a space that will act as a separator after the last column. Move the cursor to the first line containing 'rh'. Then press A . This will bring vim into insert mode, starting at the end of the line (the A is from Append). Press now the spacebar. Finally press <ESC> . A space is added. Now 'yank' the first column data. This can be done by using the same procedure as with removing a column. After pressing the x , move the cursor to the end of the first line that used to contain 'rh' and press p from paste. If everything went alright you'll see that the 'rh' column is now the last one. Undo the changes by pressing u multiple times to do the next edit.

Altering or adding a column

To change all the values of a column at once, we first select again 'rh' with CTRL-V , move the cursor so that all relevant data is selected, delete it again with x . Now we select the first column (now just a space) again with CTRL-V , but now use I to go into insert mode. Type in 'He'<ESC> . Prior to pressing the escape key only the first line is changing visually. vim will change the other lines for you once you press the escape key. As a result of this change the first column of all the rows should contain the symbol 'He' instead of 'rh'.

This functionality of column alteration is also convenient for commenting or uncommenting large blocks of programming code. For example, in the programming language C everything on a line after two forward slashes '//' will be skipped. If you don't want some lines of codes to be executed, then you can select the first column and first line, hit CTRL-V , move downwards to the last line you want to comment, and then press I// <ESC>