Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Editing and viewing text files (cat and VIM)

Learning Objectives

Creating files

Which editor? For this part of the lecture, we will learn the basics of a text editor called Vim. The first thing that you might ask is “Do I really need to learn this? It seems pretty outdated...” I have been using Vim since I started coding as an undergrad just like you. I could spend a whole lecture convincing you that should still use Vim even when there are many modern text editors available (such as VS Code, Sublime, and Notepad++), but I won’t. Instead, I will share a few reasons why I use Vim.

In this course, you will need some basic Vim skills, so this is what we will focus on. No matter what career path you choose, using Vim to open a file, add and delete text, and save it is something that will be useful to know. Advanced Vim can be extremely powerful and I encourage you to check out this Vim cheat sheet, the official Vim documentation as well as this interactive Vim tutor if you are interested in learning more.

Getting started with vim

Now let’s navigate to our data directory and create a text file using Vim.

$ cd iodp-logging-data
$ vim README.md

The syntax $ vim filename can be used to create or open an existing file, where filename represents the target file you want to create or modify. The most important thing about Vim is that it has multiple modes. Here are three that we will use in this course:

ModeDescription
NormalDefault; for navigation and simple editing
InsertFor explicitly inserting and modifying text
CommandFor operations like saving, exiting, etc.

If you try to type, nothing will happen because Vim is in its default mode (normal). To be able to type, we switch to " insert" mode by typing i, which stands for " insert". Let’s go ahead and try to type some information about our data.

## General information
last modified: YYYY-MM-DD
date created: YYYY-MM-DD
author: Blaster
## Dataset Information
- General Description: files containing well-logging data from the International Ocean Drilling Program (IODP)

Now, to go back to normal mode we hit esc. Now, we would like to save our file. To do that, we enter : to go to command mode. Note that a colon appeared at the bottom of the page. This indicates that Vim is ready for our command. In this case, we want to “write” (which is the same as save) and the command for that is w. To run the command we hit “return” To exit vim we run q, from “quit”.

Now, if you run

$ ls -F

you should see the file that we just created.

Checking the content of a file

We can use the cat command to quickly view the content of a plain text file. For example:

$ cat README.md

This prints the entire file content to the screen, which is not ideal for large files. For a partial view, use head and tail.

$ head 372-U1520B_den-nscope.csv

head displays the first few lines of a file, while

$ tail 372-U1520B_den-nscope.csv

tail displays the last few lines.

You can also specify how many lines you would like to see using the flag -n

$ tail -n 10 372-U1520B_den-nscope.csv
$ head -n 2 README.md 

Another simple task that might be handy is to count how many lines there are in a file.

$ wc README.md
4    11    90    README.md

The wc command displays the total number of lines, words, and bytes for the input (in this case, README.md).

Key Points