Since we'll be programming, we need a text editor. There are many different types of editors available and many programmers become emotionally attached to their preferred editors. Since we embrace all lifestyle choices, we attempt to be editor agnostic.
The one editor that will always be available is vi. In particular, if an embedded Linux has an editor, it is most likely vi. For example, busybox provides the vi editor to uClinux, which runs on the Blackfin. Consequently, we'll give a quick overview of vi, with no insistence that it is the best editor, merely that it is omnipresent. The references at the end of this chapter give more thorough expositions. Also note that the GNU version of vi (called vim, but will answer to vi) provides a tutorial. Further, it will operate in what you might consider to be a more friendly fashion than classic vi. If vim is available on your system with its documentation, you can run the tutorial by entering:
If you haven't encountered a modal editor before, vi will seem strange at first (maybe later on, too). It provides three modes of which we'll discuss only the first two:
Upon startup, vi is in command mode. Our approach here will be to demonstrate how to edit a file, save it, close it, and then reopen it for subsequent editing. We will not explore vi in any depth.
To edit a file (let's say
vi at the command line as follows:
If the file does not yet exist, this will allow you to create, edit, and then save the file with this name. If the file does already exist, it will be opened for editing with
vi. As mentioned earlier,
vi wakes up in command mode, but we want to enter text and to do so we enter a command which puts us into insert mode where we can enter text. The command is simply to press the
i (for insert) key. You can now enter text. The vim implementation allows you to delete characters with the backspace key and navigate with the arrow keys while in insert mode. Classic vi is less friendly, requiring that you return to command mode for character deletion and navigation - so the insert mode is for text insertion only. To return to command mode press the
Esc key. To then navigate in classic
vi, one uses these keys to move the cursor:
To move the cursor, press the h,j,k,l keys as indicated.
^ k Hint: The h key is at the left and moves left. < h l > The l key is at the right and moves right. j The j key looks like a down arrow v
For text deletion in classic vi, we have these keys:
xdeletes the character under the cursor
dwdeletes the word the cursor is on
dddeletes the line the cursor is on
Note that vim can perform its deletion and navigation tasks in the classic fashion as well as in its more contemporary style. The upshot is that editing with vim can be done remaining in insert mode; whereas editing with classic vi requires constant moving back and forth between command and insert modes. The current uClinux comes with the classic vi. Your Linux workstation likely has a variety of editors and the implementation of vi is probably vim, but if you're not a vi devotee you'll possibly prefer a different editor. However, vi is much more powerful (many more commands etc.) than our terse description suggests.
Saving and quitting require that vi be in command mode. Each of these commands begins with a colon. Here are the possibilities:
:qquit vi, but only if the file is unmodified - the file will not be saved by this command
:q!quit vi whether or not the file is modified - the file will not be saved by this command
:wsave the file, but not quit
:wqsaves the file and quits
Other editors, each with its own following, include
plus many more. We'll later see that you can do virtually all of your editing tasks on the Linux workstation because your working directory can be nfs mounted on the target uClinux file system hierarchy. Nevertheless, there might be an instance where the classic vi on uClinux is what you need.