Skip to main content

2.4 Column operations

The in-built emacs tutorial is pretty useful for learning emacs but it does not cover several topics that are helpful in scientific computing. One of these neglected topics is column operations .

Column operations are a way of deleting, copying and pasting in entire columns of your text/data/numbers/etc. These operations build on your knowledge of the 'kill' , 'delete' and 'yank' operations you've already tried out on lines of your file. The only difference with column operations is that you have to mark out a rectangle, using an emacs keystroke you've already met, which is Ctrl-space

The best way to learn is to practice - so download the file 'prac-col.txt' by using a Shift-click on the right-hand link. When you have downloaded and opened this file under emacs, follow the guided tutorial below.

Putting it into Practice:

1. Deleting a column of data. In this example you will delete the first column of the file (comprising the rh label).
Place your cursor over the first rh symbol and click. Now type Ctrl-space . Now move with the arrow keys down to the bottom rh, such that your cursor is to the right of this rh symbol. Now type Ctrl-x rd . This should delete the whole column of rh symbols.
Type Ctrl-x u to undo this editing.
So, the Ctrl-space marks the top left-hand corner of your chosen rectangle (column), and where you subsequently move your cursor should mark out the bottom right-hand corner of your chosen rectangle .
You can think of the rd in Ctrl-x rd as standing for 'rectangle delete'
 
2. Killing and yanking a column of data. In this example you will kill the first column of the file (comprising the rh label), and yank it such that it now forms the last column of the file.
Mark out your column in the usual way, ie, place your cursor over the first rh symbol and click. Now type Ctrl-space . Now move with the arrow keys down to the bottom rh, such that your cursor is to the right of this rh symbol. Now type Ctrl-x rk . This should delete the whole column of rh symbols. Now, move to the end of the first link of coordinates and advance one space further (move your cursor one space to the right). Finally, type Ctrl-x ry . This should paste your column of rh symbols so it appears after the Cartesian coordinates.
Type Ctrl-x u to undo this editing.
You can think of the rk in Ctrl-x rk as standing for 'rectangle kill'
 
3. Altering a column of data. In this example you will replace the first column of the file (comprising the rh label) with the symbol zn
Mark out your column in the usual way, ie, place your cursor over the first rh symbol and click. Now type Ctrl-space . Now move with the arrow keys down to the bottom rh, such that your cursor is to the right of this rh symbol. Now type Ctrl-x rt . Look at the bottom of your emacs window - you will be prompted with the sentence String rectangle: Click your cursor next to this sentence and type zn and hit return. All of the rh symbols will be changed to zn symbols.
Don't undo this change - leave it as it is.
 
4. Creating a column of data. Instead, say you need to change your file to conform with a new file format. This time the format is atomic element and then atomic number and then the Cartesian coordinates . To comply with this file format, yu will need to insert a column of atomic numbers, such that it is the second column in the file. The atomic number of Zn is 30.
Place your cursor two spaces after the first zn symbol and click and then type Ctrl-space . Now mark out your rectangle by moving your cursor with the arrow keys down to the last line, one space before the first Cartesian coordinate on the last line. Now type Ctrl-x rt . At the bottom of the emacs window, type 30 and hit return. A column of 30's should now be inserted between the zn column and the Cartesian coordinates.