There is a long-running battle between Linux users about which command line editor is the best. In one camp vi is the editor that rules the roost but in another, it is all about emacs.
For the rest of us who just need something simple to use to edit files, there is nano. Don't get me wrong vi and emacs are very powerful editors but sometimes you just need to open, amend and save a file without remembering the keyboard shortcuts.
The nano editor has its own set of keyboard shortcuts of course and in this guide I aim to help you to understand the meaning of all those special keystrokes you can use to make your life easier when using nano.
How to Get Nano
Emacs mode that lets you run R from within You can use Emacs to write code and run it immediately. Checkmarks or etc to indicate included dummies or sets of variables.
The nano editor is available by default in all the most popular Linux distributions and you can run it with one simple command:
The above command will simply open a new file. You can type into the window, save the file and exit.
How to Open a New File and Give It a Name Using Nano
While simply running nano is okay, you may want to give your document a name before starting. To do this, simply name the file name after the nano command.
You can, of course, supply a complete path to open a file anywhere on your Linux system (as long as you have the permissions to do so).
How to Open an Existing File Using Nano
You can use the same command as the one above to open an existing file. Simply run nano with the path to the file you wish to open.
To be able to edit the file you must have permissions to edit the file otherwise, it will open as a readonly file (assuming you have read permissions).
You can, of course, use the sudo command to elevate your permissions to enable editing of any file.
How to Save a File Using Nano
You can add text to the nano editor simply by typing the contents directly into the editor. Saving the file, however, requires the use of a keyboard shortcut.
To save a file in nano press ctrl andat the same time.
If your file already has a name you just need to press enter to confirm the name otherwise you will need to enter the filename that you wish to save the file as.
How to Save a File in DOS Format Using Nano
To save the file in DOS format press ctrl and o to bring up the filename box. Now press alt and d for DOS format.
How to Save a File in MAC Format Using Nano
To save the file in MAC format press ctrl and o to bring up the filename box. Now press alt and m for MAC format.
How to Append the Text From Nano Onto the End of Another File
You can append the text in the file you are editing to the end of another file. To do so press ctrl and o to bring up the filename box and enter the name of the file you wish to append to.
The next bit is very important:
Press alt and a
This will change the save filename text to filename to append to.
Now when you press return the text in the open editor will be appended to the filename you have entered.
How to Prefix the Text From Nano to the Beginning of Another File
If you don't want to append the text to another file but you want the text to appear at the beginning of another file then you need to prefix it.
To prefix a file press ctrl and o to bring up the filename box and enter the path to the file you wish to append to.
Again very important:
Press alt and p
This will change the save filename text to filename to prefix to.
How to Backup a File Before Saving It in Nano
If you want to save the changes to a file that you are editing but you want to keep a backup of the original in your Linux file system, press ctrl and o to bring up the save window and then press alt and B.
The word [backup] will appear in the filename box.
How to Exit Nano
After you have finished editing your file you will want to leave the nano editor.
To exit nano simply press ctrl and x at the same time.
If the file hasn't been saved you will be prompted to do so. If you select 'Y' then you will be prompted to enter a file name.
How to Cut Text Using Nano
To cut a line of text in nano press ctrl and k at the same time.
If you press ctrl and k again before making any other changes then the line of text is appended to the virtual clipboard.
When you start typing more text or delete text and press ctrl and k then the clipboard is cleared and only the last line you cut will be available for pasting.
If you wish to cut just part of a line press ctrl and 6 at the beginning of the text you wish to cut and then press ctrl and k to cut the text.
How to Paste Text Using Nano
To paste text using nano simply press ctrl and u. You can use that keyboard shortcut multiple times to continually paste the lines again and again.
How to Justify and Unjustify the Text in Nano
Generally, you won't be using nano as a word processor and so I'm not overly sure why you would want to justify the text but to do so in nano press ctrl and j.
You can unjustify the text by pressing ctrl and u. Yes, I know this is the same shortcut for pasting text and as there are many more shortcuts available I don't know why the developers didn't choose a different shortcut.
Displaying Cursor Position Using Nano
If you wish to know how far down a document you are within nano you can press the ctrl and c keys at the same time.
The output is shown in the following format:
This lets you know exactly where you are in the document.
How to Read a File Using Nano
If you opened nano without specifying a filename you can open a file by pressing the command promptctrl and r at the same time.
You are now able to specify a filename to read into the editor. If you already have text loaded into the window the file you read in will append itself to the bottom of your current text.
If you want to open the new file in a new buffer press alt and f.
How to Search and Replace Using Nano
To start a search within nano press ctrl and .
To turn off replace press ctrl and r. You can turn on replace again by repeating the keystroke.
To search for text enter the text you wish to search for and press return.
To search backwards through the file press ctrl and r to bring up the search window. Press alt and b.
To force case sensitivity, bring up the search window again and then press alt and c. You can turn it off again by repeating the keystroke.
Nano wouldn't be a Linux text editor if it didn't provide a way to search using regular expressions. To turn regular expressions on bring up the search window again and then press alt and r.
You can now use regular expressions for searching for text.
Check Your Spelling Within Nano
Again nano is a text editor and not a word processor so I'm not sure why spelling is a key feature of it but you can indeed check your spellings using the ctrl and t keyboard shortcut.
In order for this to work you need to install the spell package.
Nano Switches
There are a number of switches you can specify when using nano. The best ones are covered below. You can find the rest by reading the nano manual.
- nano -B (backs up the file prior to editing it)
- nano -E (converts tabs to spaces when editing)
- nano -c (constantly show the cursor position stats)
- nano -i (automatically indents new lines to the same position as the previous line)
- nano -k (toggle cut so that it cuts from cursor position instead of the whole line)
- nano -m (provides mouse support to the editor)
- nano -v (opens file as readonly)
Summary
Hopefully this will have given you a better understanding of the nano editor. It is worth learning and it commands much less of a learning curve than either vi or emacs.
< Common Lisp | First steps
Start up your Lisp implementation. You will most likely see a window with a prompt waiting for your input. This prompt is called a REPL for Read-Evaluate-Print Loop. At this point, Lisp is waiting for the expression to read and then evaluate, which in simple words means to calculate its result.
Type '2' and press Return (or Enter)
The lisp interpreter looks at 2 and evaluates it. It recognizes it as a number, and applies the rule that numbers evaluate to themselves, so the answer is 2.
- 1Addition
- 6Variables
Addition[edit]
Type '(+ 2 2)' and press Return
The computer sees the opening parenthesis and realizes it is being given a list. When it reaches the closing parenthesis it is able to work out that it has seen a list of three elements. The first is the + sign, so it knows to add together the values of the remaining items on the list. So it evaluates them, the number 2 having the value 2 as before. Answer: 4
Things to do wrong[edit]
- Forget to type return. You always have to type return at the end to tell the computer that you have finished your turn and it's its turn to go.
- Leave out the first space:
- Think it's a typo and try:
- Think it's a typo and do the infix thing
- Put in spaces
Clarification exercises[edit]
Explanation[edit]
Instead of writing 1+20+300+4000+50000, one writes the plus sign as the first item of a list that can be as long as you wish.
The list appears to be laid out like one might lay out a shopping list: (potatoes carrots onions bread milk) with no concession to the idea that + is part of arithmetic and a bit special. Be careful though. The first location on the list is special, and + has to come first.
Multiplication[edit]
For multiplication we use the ‘*’ function.
Can you use a list as long as you want? Yes.
In fact, you can use lists as short as you want.
Why? There are depths here that must be deferred until later.
Subtraction[edit]
Subtraction is as clunky in Lisp as in any other language.
In other words,
is the same as
Division[edit]
Division contains a surprise.
Lisp does fractions
This is potentially confusing:
If you try (/ 2 3) you get 2/3 which is probably an unpleasant surprise, if you were expecting 0.6666667. If you had tried (/ 8 12) you would also have got 2/3, which mighthave been a pleasant surprise. If you don't want a fraction, you can always say
or
Division works the same way as subtraction with
being the same as
This works OK in practice. A calculation such as 6×5×4/(3×2×1) gets translated to the following piece of Lisp:
Binding[edit]
Binding is the act of specifying a place holder for a value. The concept is analogous to that of a local variable in C or Java. You often want to do this because it is cumbersome to write out long expressions multiple times, or if a computation needs to be done in small parts where a binding needs to be updated at various times during execution. The main way to create bindings is via the “special form” LET.
Here, 5-SQUARED and 10-SQUARED are place holders ('local variables') for the results of the calculation (* 5 5) and (* 10 10), respectively. It is good to note at this time that there are very few rules regarding what can be used as a place holder. These place holders are called symbols and it can have a name that includes most any characters with the exception of quotes, open or close parenthesis, colons, backslashes, or vertical bars (‘|’). These all have special syntactical meaning in Common Lisp. It is good to note that all of these things actually can be in the name of a symbol but they require special escaping.
Bindings have a limited scope. Once the LET form closes, the binding is invalidated. This means that this is an error, because a is referred to outside of the enclosing LET form.
It is interesting to note the behavior if you bind a symbol that has already been bound. Once the inner binding is released, the outer one is in effect again.
The story gets a bit more complex, there are two types of way to make bindings in Common Lisp, lexical, which we have just seen, and dynamic. For our purposes at this point, dynamic bindings are not much different from lexical bindings, but they are made in a different way and do not have the same finite extent of the LET form. We can use DEFVAR and DEFPARAMETER to make dynamic bindings. These can hold a value in between inputs.
Variables[edit]
In Lisp, variables have some extra features, and are called symbols. A variable is a box containing a value. A symbol is a somewhat larger box, with its name written on the side. A symbol has two values, a general purpose value, and a function value used instead in particular circumstances. You can use the symbol as a thing in itself, without regard toits value.
setf[edit]
We start by setting a symbol's general purpose value. There are several commands for setting the values of symbols, set, setq, setf, psetq, psetf. One can get a longway with just setf so we start with that one
This sets the general purpose value of the symbol MY-FIRST-SYMBOL to 57, and returns 57. Now we can type
and
Well, plainly this has performed the calculation, and returned the answer, but what did the general purpose value of our second-symbol get set to? Have we used it to recordthe calculation we requested, (+ 20 3), or the answer that the computer calculated?
If we want to record the calculation for future reference we must 'quote' it. Think of the computer as a horse and the quote as a bridle, reining it in, stopping it from rushing on to evaluate things before you want it to.
Now
the general purpose value of our third symbol contains a calculation, which the computeris champing at the bit to execute.
eval[edit]
If quote pulls on the reins, how do we get started again? The answer: eval.
It is controversial to use quote in the first lesson because it is seldom typed in explicitly. One types
Notice that this is a very special abbreviation. Not only are the five letters of quote shortened to the single character ', but the brackets are also omitted.It should be noted that when we use a lisp interpreter, we are essentially in an infinite READ-EVAL-PRINT loop. Thus, we are really using eval all the time.
list[edit]
We have set three symbols so far and are perhaps in danger of forgetting what they contain. The function list builds a list, e.g.
so lets build a list of the values of our three symbols
There are two potential confusions here. One of them is figuring out which value is which.Perhaps we should turn off evaluation using quote:
The second and more serious confusion arise from comparing
and
It looks as though list is somehow deciding whether or not to evaluate its arguments, refraining in the first instance and rushing ahead in the second.
With quote and eval we can investigate this. Let us evaluate 1 zero times, once, twice, and three times
Compare this to evaluation third, zero, one, two, and three times.
Numbers are not symbols. There is no box containing two values. They just are, and they evaluate to themselves. Since numbers are not symbols,
does not work. You can get a feel for what is going on by typing
Now my-symbol-1 evaluates to itself. It shrugs off evaluation just as numbers do.
I'm going to belabor this point. I have a reason. The metaphor of a variable as a box that contains things is a good one. Much of the time the metaphor works well. You keepsomething in the box for a while. Then you throw the contents away and use the box to keep something else instead. Unfortunately the metaphor is fundamentally flawed. Both the box and its contents are immaterial. Consider
Has the list describing a simple calculation been put in the box 4th?
Yes.
Has it been taken out of third?
No.
Has it been copied? No. You make a copy like this:
Has it been moved? No. To the extent that the metaphor of motion works at all, you move things like this
There is a command (shiftf 6th 5th nil) which writes nil to 5th after moving the contents to 6th, but it returns the old contents of 6th, so it cannot be used on shiny new variables with no contents.
If it has not been copied and it has not been moved, what has happened? Something special to the immaterial world of tangled boxes, which we will not explore today.
For a striking example do
now
Now for the key test. What happens with (+ 1 (* 2 3)) and(+ 1 '(* 2 3))?The second of these is easy to understand. We have used quote to prevent evaluation, so (* 2 3) is a list of three items, giving instructions for an arithmetical calculationto be carried out at some later time. It is not the result of the calculation it describes and is not a number. Sure enough:
By contrast
appears cleverer than it really is. It looks like theinterpreter looks at its arguments and decides which ones toevaluate. For example it looks as though
is realizing that it needs to evaluate argument 2 and 3 while leaving 1 and 4 alone.
In fact
only looks like it is doing what you think. It is evaluating 2 and 3, getting 2 and 3 as the results of the two evaluations, then multiplying the two results to get six.
When the interpreter evaluates (+ 1 (* 2 3)) it evaluates 1 and (* 2 3). 1 evaluates to 1, (* 2 3) evaluates to 6. Then it adds them to get 7.
There is something worth pondering here. Get a cheap, old, pocket calculator out of a drawer and try 1 + 2 x 3 Typically, when you press x, the calculator, lacking an extra register to hold pending results, carries out the addition of the one and the two. One ends up calculating 3 x 3 and getting 9, rather than 7. Modern calculators follow the standard rules of precedence, and defer performing the addition until after they have multiplied 2 by 3, eventually arriving at 7, as desired.
Computer languages have many more operations than addition and multiplication and often have elaborate systems of precedence controlling which operations are carried out first. Lisp has no such subtlety. One either writes
(1+2)x3 as
or one writes
1+(2x3) as
there is no way to preserve the ambiguity of 1+2x3
This turns out to be for the best in practice.
Obscure note:You could try (+ 1 * 2 3). At the top level, * is used for recalling the result of theprevious command. If that was a number it will give the wrong answer. If that was not a number the interpreter will signal an error. Within a program, (+ 1 * 2 3) will generatean error message saying that * has no value. More on this later.
Let us return to third. Remember that we set our third symbol to a list of three items. We can see the whole list by typing third
Lisp has functions for extracting items from a list. First gets the first item
The function second gets the second item on the list.
If we would prefer multiplication we can change the symbol at the start of the list
We can change the second item
and, mysteriously, we can do the same with the third item
How does this work? Remember what I said earlier 'A symbol has two values, a general purpose value, and a function value used instead in particular circumstances.'
The particular circumstances are when eval is evaluating the first symbol in a list. The function that eval applies, to the result of evaluating the other items in the list, is the function value of the symbol, not the general purpose value of the symbol.
symbol-function, symbol-value[edit]
To make this clear, use symbol-function and symbol-value
This is very confusing. The interpreter stores the last command executed as the general purpose value of the symbol +, so (symbol-value '+) depends on what you did last. Inside a program (symbol-val '+) will do something like
just the same as
boundp[edit]
All these error messages are quite annoying. Is there any way to avoid them? Yes. boundp checks whether there is a general purpose value, and fboundp checks whether there is afunction value,
T is used for true
NIL is used for false, rather than F
Introductions to Lisp usually keep quiet about symbol-function. I understand why. Now that I have told you about it, you are equipped to wreck havoc, and undertake all sorts of devious mischief.
For example
and
give access to the functions that add and multiply.
Lets save them for later
![Emacs practice online Emacs practice online](/uploads/1/2/5/5/125579588/387605261.png)
Notice that you can set as many symbols as you want with a single setf.
Notice also that I have put these functions in the general purpose values of the symbols
Notice that
works as well. I'm using symbol-value to make the parallel to symbol-function more apparent.
You can store functions in general purpose value of a symbol. It really is the general purpose the value of the symbol, not the data value of the symbol.
doesn't work. When eval tries to evaluate a list that starts with a symbol, it looks for the function value of the symbol, and signals an error if it cannot find one.
works. So does
and
and
It is worth remembering that apply subsumes funcall, i.e. all of these work
Back to mischief
He he, you've got it, I'm swapping round + and *
I'd better put them back
phew, that's better.
symbol-function is heavily used. So not only is there aneasier way of writing it, (function +) instead of(symbol-function (quote +)), but the simplified way even hasits own abbreviation #'+. Err, that's not quite right, but itwill have to do for now.
Retrieved from 'https://en.wikibooks.org/w/index.php?title=Common_Lisp/First_steps/Beginner_tutorial&oldid=3237657'