================================ THE ESSENTIAL LINUX COMMAND-LINE ================================ An introduction to the Linux console, oriented towards the novice-user with no prior experience using a command-line environment. Deepak N. 05 August 2002. http://puggy.symonds.net/~deep/ n.deepak@gmail.com This document is in ASCII text, in order to make it readily accessible from any text editor. It is intended to be read after viewing the presentation introducing you to the Linux operating system. A very basic (essential) coverage of the vi and emacs text editors can be found in the file 4viemacs.txt. This document is distributed under the GNU Free Documentation Licence. -------------------- FILES IN THIS PRIMER ==================== 01. bash! --------- Introduction to the Linux command-line 02. Lonely Shell -- Commandeering your Linux system 03. Yes, Master --- (Helpful) advanced topics 04. C-x C-c :q! --- Appendix: Essential vi and emacs -------------------------------------------- APPENDIX: THE ESSENTIAL vi AND emacs EDITORS ============================================ When I first wrote this command-line tutorial for Linux, it just had a listing of important commands with an example each. Thus it stayed for a few months, when I decided to put in some introductory material as well as advanced topics. This increased the file-size enormously, but I still managed to maintain the tutorial as a single file. And now, as I sit again to revamp the tutorial, I have decided to split into different files, for two reasons: a) The new appendix which you are reading, b) The logical separation of the three sections of the original single file. I never intended to write about editors in my tutorial, since it was only to give you an understanding of the commands. I had clearly stated this in the previous version. But on second thoughts I found that it would be much better if I also introduced the user to an editor, in addition to the commands. After all, much of the work at the console either involves the shell or the editor. The two are very intimately related. And it is exactly here that most new users of UNIX get confused. The cryptic interfaces of most editors, including vi and emacs, are enough to scare them away, their feature-richness notwithstanding. Having decided to introduce an editor, I had to choose one. At first I wrote only about vi, since it is universally present on all UNIX systems. But GNU/emacs is another editor which also has almost a cult following, especially among the users of Linux operating system. So I have added some introduction to emacs as well. It's entirely up to you to use vi or emacs. I have used both, and I like both. It is usually enough if you are good at one editor, with a working knowledge of the other. Learning emacs has the additional advantage that you can use many commands with any other application which uses the GNU readline utility. Note that I have made no attempt to try to help you master either vi or emacs. I have given only the basic commands for the most important editing tasks, and there I have stopped. This is commmon to all the files in this primer; I give only enough detail as is necessary for an ordinary user. ----------------------- THE ESSENTIAL vi EDITOR ======================= -------- CONTENTS ======== o Running and quitting vi o Typing text and saving file in vi o Cutting, copying and pasting in vi o Searching in vi o Navigating in vi ----------------------- RUNNING AND QUITTING vi ======================= To type a new file 'test' under vi, or open an existing file 'test' under vi, you invoke the vi editor with the command: $ vi test ($ is the prompt as usual.) This will create an empty screen, with each line marked by a tilde ('~') and the last line displaying: "test" [New File] Welcome to vi. It has no cascading menus nor splashy dialogue-boxes, yet it is the standard editor on UNIX systems, available since past 25+ years. Right now, we shall not do anything under it, so we shall quit. Type: :q [ENTER] That is, press the colon (':') key. The colon will appear at the bottommost line of the screen. Now press the 'q' key, and hit [ENTER]. Note that vi is case-sensitive -- ':Q' won't do. If you did the exercise well, you will return back to the shell. ***** NOTE: If you just typed 'vi': $ vi you will most probably see a welcome screen. To open 'test', type: :e test [ENTER] ***** --------------------------------- TYPING TEXT AND SAVING FILE IN vi ================================= Once again, load vi with a file test. $ vi test Now if you start typing, you may either see the characters on-screen, or you might see vi behaving 'erratically', depending on what characters you pressed. This is because vi has three modes of operation, and by default you are placed in what is called 'command mode'. In this mode, the keys you press are understood by vi as commands for editing text. Now press the 'I' key. You are taken to the second mode, called the Insert mode. The bottom line says, '--INSERT--'. In the insert mode, you can type text. Go ahead, type some text. Also experiment with ENTER, DEL, BACKSPACE, HOME, END, PAGE-UP, PAGE-DOWN, and the ARROW keys. They work under most vi editors of today. When you are done typing the text, press ESC to return back to the command-mode. Now type ':q' [ENTER]. You will see a cryptic message: No write since last change (use ! to override) This is vi-ese of telling you that you have not saved your text. To save your text, type: :w [ENTER] To save your text under a different file-name, say test2, type: :w test2 [ENTER] To save your text as well as quit vi, type: :wq [ENTER] To abandon the changes you have made to the file and quit vi, type: :q! [ENTER] (Now look at the message and see if you can make out anything.) Before we move on, it is time to tell you about the third mode. You have already used it, and it is called the Ex mode. Ex-mode commands are preceded by the ':' key, and they show up at the last line of the screen. w, wq, and q are all ex-mode commands. ---------------------------------- CUTTING, COPYING AND PASTING IN vi ================================== Until now we have learnt how to open a file in vi, type some text into it, save the file, and quit vi. Now let us see how you can cut / copy / paste text in vi. Load a text file test in vi: $ vi test You are placed in the command-mode. If you are in the Insert mode, press ESC to return to the command-mode. To cut 5 lines of text, press: 5dd What you type is not seen on the screen, but don't worry. Type it. 5 lines will be deleted and placed in the clipboard, and the bottom line will say: 5 lines fewer Similarly you can type dd to cut 1 line, 10dd to cut 10 lines, 100dd to cut 100 lines of text. Now let us try pasting this text. Move to a convenient area. If you want the lines to be pasted starting below the current line, press: p (lower-case 'P') If you want the lines to be pasted starting above the current line, press: P (upper-case 'P') Finally we shall also copy and paste some text. To copy 5 lines of text, press: 5yy Again, what you type is not seen. If you did it all right, vi will say: 5 lines yanked 'Yank' is vi-ese for 'copy'. You can similarly try yy, 10yy, 100yy. To paste the copied text, move to the desired location and use the 'p' and 'P' commands. --------------- SEARCHING IN vi =============== Searching is very simple in vi. Load a file test in vi: $ vi test Now if you want to search for 'hello' in this file, type: /hello [ENTER] When you press '/' it will appear in the last line, and your search string is also displayed. When you press ENTER, vi begins to search in the forward direction and places the cursor at the first match. If you want to find the next match, press: n You can keep pressing 'n' as an equivalent to 'Find Next' in popular Windows text editors. If you want to search in the reverse direction, type: ?hello [ENTER] Behaviour is same as in forward-direction. Use the 'n' key to 'Find Previous'. ---------------- NAVIGATING IN vi ================ You can use the ARROW-keys, PAGE-UP, PAGE-DOWN, HOME and END keys in vi. They work in all the latest vi editors. In addition, * to go to the start of a line, press: 0 * to go to the end of a line, press: $ * to go to a specific line, say line 95, type: :95 [or] 95G * to go to the next word, press: w * to go to the previous word, press: b * to go to the start of file, type: :1 [or] 1G * to go to the end of file, type: G All these work in the command mode. That completes a basic introduction to vi, all that you need to start working. It is by no means complete, and I wish to warn(!) you that almost all the keys have some function in vi in the command mode, and there are numerous commands you can give in the ex mode. But what you have learnt in this file is enough for most purposes, and that's the good part about it. Before I go, here are some more commands which you may find useful: * To undo last action, type: u * To delete a line, type: dd (You can also say 5dd to delete 5 lines.) * To temporarily exit to the shell, type: :sh [ENTER] To return to vi, type 'exit' in the shell. ********************************************************************** --------------------------------------- THE ESSENTIAL emacs EDITING ENVIRONMENT ======================================= Yes, emacs is not just an editor. It is the Swiss army knife of GNU, able to do a lot more than mere editing. For instance, you can browse the Web, look at USENET newsgroups, and even send a mail from within Emacs. I will only show you how to edit files in it, though. :) Many thanks to Karl O. Pinc for his contributions and help in writing this section. -------- CONTENTS ======== o Running and quitting emacs o Typing text and saving file in emacs o Cutting, copying and pasting in emacs o Searching in emacs o Navigating in emacs -------------------------- RUNNING AND QUITTING emacs ========================== To type a new file 'test' under emacs, or open an existing file 'test' under emacs, you invoke the emacs editor with the command: $ emacs test ($ is the prompt as usual.) You will see an (almost) empty screen, and you can start typing right away. We shall not do anything for now. To quit, press: [CTRL]-x and then: [CTRL]-c You will be returned back to the prompt. In Emacs, the CTRL key is usually abbreviated to 'C'. So the above sequence is written: C-x C-c If you just typed emacs and pressed ENTER, you will most probably see a welcome screen. To open a file, press: C-x C-f, and then type the file-name. Don't remember it? Just type the directory name and press ENTER! (Did I say '.' stands for the current directory, and '..' for the parent directory?) You can open more than one file in emacs. Use C-x b to switch to a different file, and C-x C-b to list all the loaded files (called buffers in emacs). You can see the buffer name at the bottom of the screen. To maximise a buffer (if you have a split window), use C-x 1. To close a buffer, press C-x C-k. Emacs has traditionally taken a while to load, so it is better to start with just 'emacs' at the prompt, and then open files from within the editor. Karl has used Emacs for weeks together without ever shutting it down. 'Having all the files there in the buffers is much better than looking for them in the disk,' he says. Note: If you make a mistake while entering a command, or want to break out in the middle, press C-g. ------------------------------------ TYPING TEXT AND SAVING FILE IN emacs ==================================== Emacs doesn't have any modes as in vi. You can type text and be assured that it isn't interpreted as a command, since emacs commands always start with the CTRL or META (usually equivalent to ALT) keys. Once you are done typing the text, you can save the file by saying: [CTRL-x] [CTRL-s] or in emacs style, C-x C-s. To save under a different name, use C-x C-w. If you do not want to save the file, just say C-x C-c. ------------------------------------- CUTTING, COPYING AND PASTING IN emacs ===================================== Emacs is better than vi when it comes to clipboard operations, since it is not line-oriented. You just mark the start position, take the cursor to the end position, and say 'Cut'. ->Let me elaborate. Let us say you want to cut this line upto<- here. a) First, place the cursor at the 'L', and press C-[SPACEBAR]. This sets the starting position. You must see a message called 'mark set' at the bottom of the screen. b) Now go to the 'o' of 'upto' using the arrow keys, and press C-w. The selected region is cut to clipboard. Note that emacs doesn't highlight the selected region. If this seems a bit of too much, you can cut lines as in vi. Press C-k to 'kill' a line. Keep pressing it if you want to cut more lines. Instead, let's say you just want to copy the same text. There is a round-about way of doing this. First cut the lines as above, then paste the clipboard content back! The clipboard is not emptied unless you make another marking, so you have copied something. I haven't yet told you how to paste from clipboard in Emacs. It is rather simple. Just press C-y. ------------------ SEARCHING IN emacs ================== Emacs has a powerful search utility which starts finding matches even as you are typing the term. To search for, say, 'hello', type: C-s hello This looks for 'hello' in the forward direction. If you do a reverse search, type: C-r hello If you get the right match, press [ENTER] to start working from there. Else, use C-s and C-r keys to continue searching, or C-g to abort. ------------------- NAVIGATING IN emacs =================== You can use the ARROW-keys, PAGE-UP, PAGE-DOWN, HOME and END keys in emacs. Note that HOME takes you to the start of file, not line; END similarly takes you to the end of file. In addition, * to go to the start of a line, press: C-a * to go to the end of a line, press: C-e * to go to a specific line, say line 95, type: M-x goto-line (Most of the times it is easier to use the page-movement keys and look at the line number that appears at the bottom.) * to go to the next word, press: [META]-f (ALT-f, abbr. to M-f) * to go to the previous word, press: M-b * to go to the start of file, press: [HOME] or M-< (ALT-SHIFT-,) * to go to the end of file, press: [END] or M-> (ALT-SHIFT-.) In the end, here are a few tips and tricks for you the Emacs user: * To undo last action: C-x u * To repeat the next command times: M-x * To make the lines in a paragraph have about the same length: M-q (The paragraph should be separated by a line space above and below.) * To temporarily exit to the shell: C-x C-z (Type %em[ENTER] at the prompt to return to emacs.) * Emacs has the tab-completion feature introduced with the bash shell. (In fact, bash borrowed this from emacs.) You can use this feature to reduce the amount of typing. *********************************************************************** That finishes a basic working knowledge of the vi and emacs editors. As already said, they are much more than what I have told you. You can read their online documentation if you are curious to learn. There is even an online tutorial for emacs (C-h t). Before I bid good-bye, I would like to remind you that any feedback is most welcome, and would help improve the primer for future users. I would also appreciate it if you could load Emacs, press C-h C-p, and read what shows up on the screen. Bored with tech stuff? Yawning? Get a psychotherapy from emacs. Press M-x (ALT-x), type 'doctor', and hit [ENTER]. Happy Linuxing!