ITIC:Using a text editor - Exercises

From Juneday education
Jump to: navigation, search

Preparations and assumptions

Create a new directory where you can work with these exercises. We suggest you create a directory structure as follows;

edu
`-- tig015
    `-- editor-exercises

This can be done by the following command:

mkdir -p edu/tig015/editor-exercises

Now, enter the same directory by Typing cd followed by a blank, press esc followed by a . . Your command line should now look like:

cd edu/tig015/editor-exercises

Editor used throughout the suggested solutions

We do not want to force you to use any specific editor. However we will gedit which might give the impression that we want you to use that editor. This is not the case. An important thing to discuss here is that in some exercises the behavior of the editor you're using might differ from that of Gedit.

Get on with it! Ehum, sorry for the delay.

Starting the editor from the command line

When the argument file doesn't exist

Start the editor with no argument supplied.

Expand using link to the right to see a suggested answer/solution.

gedit &

The terminal now looks like:

Term-after-gedit.png

and your gedit windows looks like this:

Gedit-no-arg.png

Note that there is a made-up name, Untitled Document 1 in the title bar of gedit.

Add some text in the buffer and save

Enter some text in the buffer (you should now see that there is an asterisk * in front of the Untitled Document 1</source> and press <code>Ctrl S. You will now see a pop up dialog where gedit asks you to pick a name for the file (where to put the content of the buffer). Gedit suggests the name Untitled Document 1 - which really is not a good name so go for first-exercises.txt instead. You may argue and think that this is not a very good name either ... but let's go for that name. Now, press the Enter or the Save button.

Where is the file stored? Why is the file stored here?

Expand using link to the right to see a suggested answer/solution.

The file is stored in the same directory, ~/edu/tig015/editor-exercises, as you were located in when starting gedit. This is a strategy chosen by the developers of gedit. All editors work, at least should work, this way so everyone expects this to be the case. But it is also good since we can via the command line and the directory used to start gedit and not having to click your way around gazillionz of directory buttons in the GUI.

When the argument file exists

Make sure to close the editor before you proceed with the following exercise. Start the editor with a file name supplied as argument. What do you think the title bar will say (it said Untitled Document 1 in the previous exercises)?

Expand using link to the right to see a suggested answer/solution.

You can start gedit like this:

gedit another-exercise.txt &

The title bar will now say another-exercise.txt.

Add some text in the buffer and save

Enter some text in the buffer (you should now see that there is an asterisk * in front of the another-exercise.txt and press Ctrl S. You will now NOT see a pop up dialog where gedit asks you to pick a name for the file. Now, press the Enter or the Save button.

Where is the file stored? Why is the file stored here?

Expand using link to the right to see a suggested answer/solution.

For the same reasons as above the file is stored in the same directory, ~/edu/tig015/editor-exercises, as you were located in when starting gedit.

Starting the editor a second time

Make sure to keep the editor running before you proceed with the following exercise. Start the editor with a new file name supplied as argument. What happened?

Expand using link to the right to see a suggested answer/solution.

You can 'start' gedit (again) like this:

gedit another-file.txt &

Gedit keeps the previously opened file another-exercise.txt and opens the new file another-file.txt in a new tab. Here's an image showing the tabed gedit:

Gedit-two-tabs.png

Add some text in the buffer and save

Enter some text in the buffer (you should now see that there is an asterisk * in front of the another-file.txt and press Ctrl S. You will now NOT see a pop up dialog where gedit asks you to pick a name for the file. Now, press the Enter or the Save button.

Where is the file stored? Why is the file stored here?

Expand using link to the right to see a suggested answer/solution.

For the same reasons as above the file is stored in the same directory, ~/edu/tig015/editor-exercises, as you were located in when starting gedit.

Starting the editor a third time - somewhere else

Make sure to keep the editor running before you proceed with the following exercise. Create and enter a a new directory, useless:

mkdir useless
cd useless

Note: you could use the esc . trick to let bash write useless for you in the cd command.

Start the editor with a new file name supplied as argument. You can 'start' gedit (again) like this:

gedit third-file.txt &

Enter some text in the buffer (you should now see that there is an asterisk * in front of the third-file.txt and press Ctrl S. Now, press the Enter or the Save button.

Where is the file stored? Why is the file stored here?

Expand using link to the right to see a suggested answer/solution.

For the same reasons as above the file is stored in the same directory, ~/edu/tig015/editor-exercises, as you were located in when starting gedit. Even though gedit was first started in one directory and the third file was opened from another directory gedit is kind enough to keep track per file where it (gedit!) was opened. Nice!

Open up the second file from the new directory

Make sure to keep the editor running before you proceed with the following exercise. Also make sure you're located in the new directory, useless.

We can now use tree to show our directory structure (from the top directory and down):

$ tree  .. --charset=ascii
..
|-- another-exercise.txt
|-- another-file.txt
`-- useless
    `-- third-file.txt

1 directory, 3 files

Open up the second file we created, another-file.txt by invoking the editor with that file as argument:

gedit another-file.txt &

Check the editor - there is a fourth tab open. Why?

Add some text and save the file. Where is it stored? Why?

Check the directory structure again (issue the same tree command as above).

Expand using link to the right to see a suggested answer/solution.

This time we open another-file.txt from another directory so we're creating a new file in the directory useless. This file has the same name, another-file.txt, as the one in the directory above.

$ tree  .. --charset=ascii
..
|-- another-exercise.txt
|-- another-file.txt
`-- useless
    |-- another-file.txt
    `-- third-file.txt

1 directory, 4 files

Conclusions from the above

We've now created two files with the same name - in different directories. This could lead to a situation where you think you're editing one file but you're really editing another file. Keep this in mind! Really, think about this for a while.

Existence of the buffer

Create new file

Go to the directory for these exercises and open up the editor with a new file name buffer-test.txt. Now, check in bash if the file exists.

Expand using link to the right to see a suggested answer/solution.

$ ls -al buffer-test.txt
ls: cannot access 'buffer-test.txt': No such file or directory

From this we can see that the file does not exist.

Enter text

Enter some text text in the text and DO NOT SAVE the file. Now, check in bash if the file exists. Why is it so?

Expand using link to the right to see a suggested answer/solution.

$ ls -al buffer-test.txt
ls: cannot access 'buffer-test.txt': No such file or directory

From this we can see that the file does not exist. This is because we have not yet saved the file. The content (the stuff you wrote in the window) exists only in the buffer.

Save file

Enter some text text in the text and, finally, save the file.

Expand using link to the right to see a suggested answer/solution.

$ ls -al buffer-test.txt 
-rw-r--r-- 1 hesa hesa 34 sep.   1 15:31 buffer-test.txt

From this we can see that the file does exist. If we want to look at the content of the file we can use Cat.

$ cat buffer-test.txt 
This is some sample text I wrote

Enter more text

Now, add some more text. Use cat to show the content of the file and contrast this to the content in your editor's window (actually the buffer). Why is the content of the file and editor window different?

Expand using link to the right to see a suggested answer/solution.

Since we have not yet saved the content of the file to disk. </source>

Save under a new name

Sometimes it is useful to change the name of a file. Let's say you would like to change the name of file from first-name.txt to second-name.txt. You could do this in bash by either typing:

mv first-name.txt second-name.txt

or

cp first-name.txt second-name.txt

What is the difference between after executing the commands above?

Expand using link to the right to see suggested solutions

In the first case (mv) the file first-name.txt will no longer exist. The second file second-name.txt will be created with the same content as first-name.txt had.

In the second case (cp) the files first-name.txt and the second file second-name.txt will exist with the same content.

If you instead use your editor things could differ between editors but our experience says that your editor will create a copy (second-name.txt) and keep the original (first-name.txt).

Find out how to do this in your editor.

Expand using link to the right to see suggested solution.

In gedit you can use either the menu option Save as ... or use the shortcut Ctrl-Shift-S.

Find text

It is common when programming to find and go to a place in the source code where a certain word is written. To do this you use the search feature found in most (all?) editors. Find out how this is done in your editor.

Expand using link to the right to see a suggested answer/solution.

In gedit you can use either the menu option Find ... or use the shortcut Ctrl-F. Gedit will ask for the word to search for, by entering a word and pressing Enter gedit will find the occurances of the word you entered.

Find and replace text

Let's say you want to to change the word Carl to the words Carl Sagan. Find out how to do this in your editor.

Find out how to do this in your editor.

Expand using link to the right to see suggested solution.

In gedit you can use either the menu option Find and replace ... or use the shortcut Ctrl-Shift-S. Fill in Carl in the Find field and the words Carls Sagan in the Replace with field and press Enter. If there are many occurances of Carl you can chose to replace Carl with Carl Sagan one by one or all of them at once.

Syntax highlight

Create a file, source-code.txt, in your editor with the following content:

public class Example {
  int size;
}

Make a copy of that file, using the command cp. The new file should be called Example.java.

Expand using link to the right to see how to copy the file.

cp source-code.txt Example.java

When you look at the files, with exactly the same content, they are presented differently. Why?

Expand using link to the right to see a suggested answer/solution.

Even though the content is the same, gedit (the authors' are using gedit) sees (probably from the file suffix .java) that it is a Java file and presents the content with highlighted parts depending on the so called syntax of the programming language Java.


Links

Further reading

Where to go next

The next page is ITIC:Introduction_to_Bash_scripting.

« PreviousBook TOCNext »