Chapter 9 Text editor in Linux

In Linux, we sometimes need to create or edit a text file like writing a new perl script. So we need to use text editor.

As a newbie, someone would prefer a basic, GUI-based text editor with menus and traditional CUA key bindings. Here we recommend Sublime, ATOM and Notepad++.

But GUI-based text editor is not always available in Linux.

A powerful screen text editor vi (pronounced “vee-eye”) is available on nearly all Linux system. We highly recommend vi as a text editor, because something we’ll have to edit a text file on a system without a friendlier text editor. Once we get familiar with vi, we’ll find that it’s very fast and powerful.

But remember, it’s OK if you think this part is too difficult at the beginning. You can use either Sublime, ATOM or Notepad++. If you are connecting to a Linux system without Sublime, ATOM and Notepad++, you can write the file in a local computer and then upload the file onto Linux system.

9.1 Basic vi skills

As vi uses a lot of combination of keystrokes, it may be not easy for newbies to remember all the combinations in one fell swoop. Considering this, we’ll first introduce the basic skills someone needs to know to use vi. We need to first understand how three modes of vi work and then try to remember a few basic vi commonds. Then we can use these skills to write Perl or R scripts in the following chaptors for Perl and R (Figure 9.1).

Three modes of vi.

Figure 9.1: Three modes of vi.

9.2 Create new text file with vi

mkdir test_vi  ## generate a new folder
cd test_vi     ## go into the new folder
echo "Using \`ls\` we don't expect files in this folder."
ls 
echo "No file displayed!"
## Using `ls` we don't expect files in this folder.
## No file displayed!

Using the code above, we made a new directory named test_vi. We didn’t see any file.

If we type vi test.pl, an empty file and screen are created into which you may enter text because the file does not exist((Figure 9.2)).

vi test.pl
A screentshot of the vi test.pl.

Figure 9.2: A screentshot of the vi test.pl.

Now if you are in vi mode. To go to Input mode, you can type i, ‘a’ or ‘o’ (Figure 9.3).

A screentshot of the vi test.pl.

Figure 9.3: A screentshot of the vi test.pl.

Now you can type the content (codes or other information) (9.4).

A screentshot of the vi test.pl.

Figure 9.4: A screentshot of the vi test.pl.

Once you are done typing. You need to go to Command mode(Figure 9.1) if you want to save and exit the file. To do this, you need to press ESC button on the keyboard.

Now we just wrote a Perl script. We can run this script.


perl test.pl
## Hello Bioinformatics World!

9.3 An example for using editor R

qnorm(.975)
## [1] 1.959964
xval<-seq(-3.2,3.2, length=1000)
yval<-dnorm(xval)
plot(xval, yval, type="l",axes=T,lwd=3,xlab="",ylab="")
x<-seq(qnorm(.975), 3.2, length = 100)
polygon(c(x,rev(x)), c(dnorm(x), rep(0,length(x))), col="salmon")
text(mean(x),mean(dnorm(x))+0.02, "2.5%", cex=2)
text(qnorm(.95), 0.01, "1.645",cex=2)

x<-seq(-3.2, qnorm(.025), length =100)
polygon(c(x,rev(x)), c(dnorm(x), rep(0,length(x))), col="salmon")
text(mean(x),mean(dnorm(x))+0.02, "2.5%", cex=2)
text(qnorm(.025), 0.01, "1.645",cex=2)