Unix Text Editors

From AstroBaki

Jump to: navigation, search

[edit] Short Topical Videos

[edit] Reference Material

Contents


Choosing a Text Editor

Any serious programmer needs a robust text editor to write program, and you (as aspiring scientists) are going to need to be serious programmers. So you need to put some thought into choosing your editor. Here is a quick breakdown of your choices for a serious text editor:

  • vi/vim (gvim adds windows/tabs/mouse capability)
  • free and installed by default on all unix (and OSX) systems
  • steep learning curve, but there will be vim experts nearby
  • arguably faster to control once you master it
  • emacs
  • free and installed by default on all unix (and OXS) systems
  • less steep learning curve, and there will likely be emacs experts nearby
  • Others, including sublime, atom, ultraedit:
  • you’ll probably have to install these yourself, which can be annoying if you are working on a computer you don’t own/control
  • you’re on your own for learning

My suggestion is that you pick between vi/vim/gvim and emacs, at least to start. You should also be warned that people tend to be monogamous with their editors. You may end up using the editor you choose now for the rest of your life. For example, I picked vi in college and have never strayed. If you choose the red pill, go to the section on vi. If you choose the blue pill, go to the section on emacs.

A Beginners Guide to Vi

Vi is a text file editor in Unix environments. This includes our lab which runs a flavor of Linux. You can use vi to quickly edit a simple text file, as an editor for your Python source code, or as a nice front end to typesetting documents in , the required typesetting utility in this class.

The Basics

Opening

Most of the programs that you will be using on these systems will be started from the command line. To begin a new document, simply navagate (via the Unix command line) to the directory where you want the file to be and type:

vim something.txt &

Where something.txt is the new file that you wish to create, or the old file that you wish to update.

Before we go any further, you need to know that vi has three operating modes: COMMAND mode, INSERT mode, and VISUAL mode, which are described in the table below.


COMMAND

the default mode vi starts in. Get here from other modes with ESC.

INSERT

what you type goes into the document. Get here from COMMAND mode with ’i’.

VISUAL

like command mode, but you can highlight text. Get here from COMMAND mode with ’v’ or CTRL+’v’.

Vi Modes

Now that you have vi open (in COMMAND mode), you can press ’i’ to enter INSERT mode and type some text. In the next section we will save the file.

Saving and Exiting

To save a document, go to COMMAND mode (press ESC), then type ’:w’ to write the file under the name you opened it. If you would like to change the name, you can write ’:w newname.txt’. If you’d like to save and exit, replace ’:w’ with ’:wq’. To just exit, type ’:q’. If you’ve made changes to a file and you don’t want to save them, you will need to add an exclamation point to force vi to lose your changes: ’:q!’. In the interest of simplifying your life, this document will concentrate on the usage of keyboard shortcuts in using vi.

Keyboard Shortcuts in COMMAND mode

What makes vi powerful (with a steep learning curve) are the rich set of keyboard shortcuts in COMMAND mode. We’ll cover a few simple ones here, but you should be aware that there is probably a command to accelerate anything you find yourself doing, if you take the time to learn how. Guru-level vi programmers pride themselves in almost never moving their hands away from the keyboard to use a mouse or arrow keys.

Some basic COMMANDs:
:open somefile.txt - Open a file
:w - Save file
:q - Close vi

i - enter INSERT mode
A - move to end of line in INSERT mode
I - move to beginning of line in INSERT mode

x - delete character under cursor (put in buffer)
dw - delete word (put in buffer)
dd - delete line (put in buffer)
p - paste from buffer
yy - yank current line into paste buffer (without deleting)

h,j,k,l - move left, up, down, right, respectively
b - move back one word
w - move forward one word

Integration

Vi has nice integration. If you start a file with the ending .tex, Vi will automatically load up in a mode to highlight special characters and special modes to make life a little easier.

Python Integration

Vi also has a nice mode for integrating with Python. For the purposes of this lab, we will only need to use the highlighting features of Vi. This checks to make sure that you have matching parentheses and will color special keywords separately from other commands.

A Beginners Guide to Emacs

Emacs is a text file editor in Unix environments. This includes our lab which runs a flavor of Linux. You can use emacs to quickly edit a simple text file, as an editor for your Python source code, or as a nice front end to typesetting documents in , the required typesetting utility in this class. This document will outline the basics of using emacs.

The Basics

Opening

Most of the programs that you will be using on these systems will be started from the command line. To begin a new document, simply navagate (via the Unix command line) to the directory where you want the file to be and type:

emacs something.txt &

Where something.txt is the new file that you wish to create, or the old file that you wish to update. After a few moments, a new window will popup with a blank screen that you can type into. You can use the mouse to navigate the menus at the top. See table 1 for information on what the different menu options do.


Buffers

Contains information about the currently open files in this instance of

Emacs.

Files

Contains basic commands for opening, saving, and closing files.

Tools

Contains advanced functions for doing things like version control.

Edit

Contains cut, paste, and spell checking capabilities of emacs.

Search

Contains search and replace functions.

Mule

Advanced editing functions.

TeX

LaTeX specific functions. Only appears when the filename you are

editing ends with .tex.

Python specific functions. Only appears when you are editing a filename

that end with .py

Help

Emacs’ extensive help and configuration area.

Emacs Menu Items

Now that you have emacs open you can type something in the text area. In the next section we will save the file.

Saving

In Emacs, saving a document is as simple as π. You can click on the Files menu, then click on Save Buffer... You can also save by using the keyboard shortcut C-x C-s. In the interest of simplifying your life, this document will concentrate on the usage of keyboard shortcuts in using emacs.

Keyboard Shortcuts

Just about every aspect of emacs can be accessed and configured with keyboard shortcuts. This means that to work on a document, you rarely need to move your hands away from the keyboard and use the mouse. This increases productivity, and just makes things easier. All the keyboard shortcuts that I mention in this document will be summarized at the end.

All keyboard shortcuts are represented in a format like C-x C-f. When you see a -, that means you hold the key that comes before and after the - down at the same time. The capitol C however, does not correspond to the letter c, but rather the control button (abbreviated CTRL) on the keyboard. So, if you see C-x C-f, that means press and hold the CTRL button, then press the x key, then press the f key, then release the CTRL button. If you see something of the form C-x d, then that means to press and hold the CTRL button, press x, release CTRL, then press d. I think you get the picture. Similarly, M represents the ALT button.

In some of the menus, you will see keyboard shortcuts that are labelled as M-%.

On the Sun workstations that corresponds to the \Diamond , and works identically to the CTRL key. On non-Sun workstations this key is simulated by pressing ESC then CTRL-whatever.

Some basic keystroke Shortcuts:
C-x C-f - Open a file
C-x C-s - Save file
C-x C-c - Close Emacs

C-space - Start Selection
C-w - Cut ("kill") selection
M-w - Copy selection
C-y - Paste ("yank") selection
C-k - Kill a line

C-x C-2 - Split window horizontally
C-x C-3 - Split window vertically

C-a - Move to the beginning of a line
C-e - Move to the end of a line

Cutting, Pasting, and Navigation

In Emacs cutting and pasting is simple, yet slightly different from what you may be used to. To do it using keystrokes only:

  1. Select and cut the block of text by
  1. moving the cursor to its beginning, type C-spacebar.
  2. using the arrow keys, move the cursor to its end abd type C-k.
  3. Paste the block by
  1. moving the cursor to where you want to paste
  2. type C-y .

To do it using the mouse: Select the text with your mouse and the double right click in the selection. To paste, move the cursor to where you want to paste and click the middle mouse button.

Integration

Emacs has nice integration. If you start a file with the ending .tex, Emacs will automatically load up in a mode to highlight special characters and special modes to make life a little easier. Emacs also has a couple modes to automatically compile files and display the result in xdvi (refer to the handout if none of this makes sense to you. Below are the basic shortcuts.


Compile file

C-c C-f

View file in xdvi

C-c C-v

- Emacs commands

Python Integration

Emacs also has a nice mode for integrating with Python. For the purposes of this lab, we will only need to use the highlighting features of Emacs. This checks to make sure that you have matching parentheses and will color special keywords separately from other commands.

Personal tools