Tag: Computer science

  • Linux Tutorial Series – 91 – Basics of vi

    Here is the video version, if you prefer it:

    In this tutorial, we will be learning how to use the vi editor. As I said in one of the earlier posts, on some computers, there is no alternative other than to use vi. So you must know the basic stuff. Let’s see what the basics are. I modeled this after (Shotts, 2019)⁠, but I pruned it even more. This is intended as a follow-along guide – follow the instructions in this article and gain a basic understanding of vi.

    I have a file called aba.txt. Let me first show you the contents of the file, so you can replicate it on your computer:

    mislav@mislavovo-racunalo:~/Linux_folder$ cat aba.txt

    Abba Money

    Money Money

    It's the Money

    In the Rich Man's World

    I will open it with vi using the following:

    vi aba.txt

    I could also open multiple files at the same time by typing something like:

    vi file1.txt file2.txt

    When I open up aba.txt with vi, I get something like this:

    Abba Money

    Money Money

    It's the Money

    In the Rich Man's World

    ~

    ~

    ~

    ~

    ~

    ~

    ~

    ~

    ~

    ~

    ~

    ~

    ~

    ~

    ~

    ~

    ~

    ~

    "aba.txt" 4 lines, 62 characters

    The first thing to know about vi is how to quit it. To quit vi, type :q (with the colon in front) and press Enter (Enter on your keyboard). That will exit vi.

    Let’s reopen vi.

    vi aba.txt

    OK. The first thing to realize about vi is that it has two modes – command mode and insert mode. By default, you are in command mode. That means that any characters you type are not going to be interpreted as symbols to be inserted, but rather as commands. Also, note that cursor movement is done in the command mode. You can move your cursor with the arrows on your keyboard. But don’t move your cursor just yet. To enter insert mode, press i on your keyboard. Do it now.

    You can type in something. A word of caution: Backspace won’t be delete the characters you mistype. I typed in “Some stuff “. To exit insert mode, press ESC (escape on your keyboard).

    My file looks like this now:

    Some stuff Abba Money

    Money Money

    It's the Money

    In the Rich Man's World

    ~

    ~

    ~

    ~

    where denotes the continuation of the file.

    Since we pressed Escape, now we are in the command mode again. Now we have to write the changes to the file. To write changes we just made to the file, type in :w and press Enter.

    Now let’s learn how to append to a file. While in command mode (and from this point on it is assumed you are in the command mode when a particular instruction is given for you to do, unless explicitly stated otherwise) type capital A (on your keyboard – I assume this from this point on). That will allow you to append after the line where the cursor is currently located and it automatically puts you in insert mode. If you haven’t moved your cursor around, you will append just below the first line. So let’s add the line “Money is how we transfer wealth”. Feel free to press Enter on your keyboard, and write “It is an IOU”. Then press Escape to exit the insert mode. Your file should look like this now:

    Some stuff Abba Money

    Money is how we transfer wealth

    It is an IOU

    Money Money

    It's the Money

    In the Rich Man's World

    ~

    ~

    ...

    So A (capital A) appends to the file by adding lines after the line where the cursor is located.

    Now let’s talk about inserting blank lines and undoing our changes. If you want to insert a blank line below the current line, press o (lowercase o). If you want to insert a blank line above the current line, press O (capital O). Note that both of these commands put you in insert mode, so you will need to exit them by pressing ESC. Press o now. Uh-oh. Let’s say you didn’t want this. First, exit the insert mode by pressing ESC. Now, press u (lowercase u). It will undo the change you did. Try it with both o and O and then undo the changes.

    Now let’s learn how to delete characters. You do this by pressing x. Let’s do an exercise. First, place your cursor on the end of the line which says “It is an IOU”. Press x. Press x again. Press x until you delete the entire line. Note that no matter how much you press x, the line won’t “merge” with the previous line. That’s OK. We will cover how to do that later.

    Your file should look like this:

    Some stuff Abba Money

    Money is how we transfer wealth

    Money Money

    It's the Money

    In the Rich Man's World

    ~

    ...

    Let us now discuss deleting stuff other than just characters. Here is a list on how to delete various chunks of text:

    • dd – deletes the current line
    • d$ – deletes from the current cursor location to the end of the current line
    • dG – deletes from the current line to the end of the file

    Now, a revelation – the d command (the first letter in the above commands) doesn’t just delete text, it “cuts” text. You can paste it with p. Try placing your cursor on the line “Some stuff Abba Money”, type dd, then type p. That should cut and paste the line. But what just happened? Well, the line “Some stuff Abba Money” is below the line “Money is how we transfer wealth”. Why is that so? p pastes the line below the line where the cursor is, while P (capital P) pastes the line on the line above the cursor.

    Now, let’s talk copying (also called yanking):

    • yy – copies the current line
    • y$ – copies from the current cursor location to the end of the current line
    • yG – copies from the current line to the end of the file

    Try this: type yy while your cursor is on the line “Money Money”, then press P (capital P). To remind ourselves, this pastes the copied line above the line the cursor is currently in. Great! More money! We are rich!

    Here is how our file looks like now:

    Money is how we transfer wealth

    Some stuff Abba Money

    Money Money

    Money Money

    It's the Money

    In the Rich Man's World

    ~

    ~

    ...

    Now one last touch – let’s remove the blank line in the middle of our file. We do that by placing our cursor on that line and typing capital J.

    Aaah! Perfect! Now let’s write the changes to our file. Type :w and press Enter. Then type :q and press Enter to quit vi. A caveat: if you type :w <newFilename> where <newFilename> is the new filename you want for your file, a new file will be created, but in vi you will still be editing the old file.

    Voila! You now know the very basics of vi. Now, there are some things we didn’t cover, such as multiple files and search-and-replace, but we will cover that in the next article. These are the very basics. If you wanted to search and replace some word with some other word, you would have to do it manually and you would have to have separate Terminals for each of the files to be opened in vi. Then, you would manually search and replace. That is tedious, but, you have the basics.

    Feel free to skip the next article, if you wanted to learn only the essentials of vi. The next article covers multiple files in vi, search and search-and-replace functionality in vi. It will make you a more efficient vi-er, but if you just wanted to know the very rudimentary basics, off you can go.

    Hope you learned something useful!

    References

    Shotts, W. (2019). The Linux Command Line, Fifth Internet Edition. Retrieved from http://linuxcommand.org/tlcl.php. Pages 165-176

  • Linux Tutorial Series – 90 – Basics of Emacs

    Here is the video version, if you prefer it:

    I have learned the basics of Emacs by reading the Emacs tutorial found in Emacs itself – when you open Emacs, the tutorial is in the window below the window where file contents are displayed. Also, before that, one of my friends taught me how to use Emacs, but I needed a refresher.

    To install Emacs (which you will most likely have to), type in:

    mislav@mislavovo-racunalo:~$ apt-get install emacs

    I urge you to go through the Emacs tutorial. It will cover all of the things you need to know. Here I will list the keyboard shortcuts to focus on – you don’t need to remember everything about Emacs, just these things, in my opinion.

    Note: C means CTRL and M means ALT. M-<character> means press M and <character> simultaneously, while C-<character> means press C and <character> simultaneously. <Return> means Enter on your keyboard.

    • To go to the beginning of a file, press M-<
    • To go to the end of a file, press M->
    • Stop commands with C-g
    • Leave just one window open with C-x 1
    • To cut (or, as Emacs calls it, “kill”) from the cursor position to the end of the line, use C-k
    • A uniform method to kill a portion of files contents is as follows: Press C-<SPC> (the Space keyboard key), then move your cursor to the end of the text you want to cut (“kill”) and then press C-w to cut (“kill”) it
    • The command for “yanking” (pasting the portion of the text you have cut (“yanked”)) is C-y
    • To access previously “killed” text, use M-y – this is useful if you cut two or more pieces of text, and want to access not the most recently cut one – pay attention to this in the Emacs tutorial
    • To save a file, press C-x C-s (in succession)
    • To find a file to open up in Emacs press C-x C-f and press Return
    • Kill the Emacs session with C-x C-c
    • Undo with C-x u
    • Replace a string by using M-x, typing replace-string and giving it two arguments – first argument is the string to be replaced and the second argument is the string to replace it with (end each argument with <Return>); you can press <Tab> (Tab on your keyboard) when typing in replace-string for auto-completion (so that you don’t have to type all of the characters of replace-string)
    • To search from the current cursor position onward, press C-s and type in the search term. As you type the search term in, Emacs will display the results, refining them with each character you type in. Press C-s again to move forward one search result. Press <DEL> (Backspace on your keyboard) to move back one search result. If you need to edit your original query, place yourself on the first search match, press <DEL> and then you will notice your search term losing characters. Terminate the search with <Return>.
    • To start the search from the current cursor position backwards, all of the above applies, but you start it with C-r, not C-s.

    Those would, in my opinion, be the very basics to focus on while reading the tutorial. Go through the tutorial – shouldn’t take you more than a couple of hours (or, as scientists and programmers like to say – an afternoon).

    Hope you learned something useful!

  • Linux Tutorial Series – 89.1 – Text editors

    Let’s talk about text editing in Linux.

    Resources like (Ward, 2014)⁠ tell you that you should learn how to use either Emacs or vi. I don’t think that you need to know either Emacs or vi if you are a regular Linux user (meaning someone who is using Linux on your desktop machine). You can almost always use your Linux distributions default text editor and that will work great for you.

    To argue my point a bit – a regular Linux user, who is just using Linux for activities such as web browsing, reading PDFs, editing Word-like documents etc. will most likely never need to meddle with vi or Emacs. He or she can always use the default text editor for editing the files that have to be edited.

    However, here are two scenarios where it will be extremely beneficial for you to know how to use vi or Emacs (more likely vi than Emacs):

    • When you are using a computer that doesn’t have text editors with graphical user interface on it. Then you will, in most cases, have to use vi, because Emacs doesn’t come preinstalled on most distributions.
    • When you encounter a tutorial online about how to do something in Linux and they use either Emacs or vi to edit a certain file. This argument is really meek because you can always do the same change in another text editor, but maybe the tutorial isn’t clearly written and you have a hard time following it without following it to the letter. That way, you don’t really know what is going on and that is almost always bad.

    Given the above points, my advice is to learn the basics of both Emacs and vi(especially vi), if and only if you are a person who uses computers professionally (like a software developer). I, as a software developer, have encountered times where I had to use vi, or where knowing the basics of vi would have eased my task at hand tremendously. So it is always a good idea to know how to use vi and you can learn how to use Emacs if you really want to. I would urge you to learn the basics of both Emacs and vi before jumping deep into either one or the other. Once you know both at a basic level, then you can expand to the one you like better (if you want).

    I will guide you through the basics of these text editors in the upcoming articles.

    Thank you for reading!

    References

    Ward, B. (2014). How Linux Works: What Every Superuser Should Know (2nd ed.). No Starch Press. Pages 24-25

  • Linux Tutorial Series – 89 – Checkpoint

    Here is the video version, if you prefer it:

    Going on, we will talk about text editors. This is not that important to you if you are not a software engineer or someone other than a “regular” Linux user, like an aspiring information security expert or a “wannabe” system administrator. If you are just using Linux instead of Windows for everyday things, you don’t need this material, in my opinion.

  • Linux Tutorial Series – 88 – Review

    Here is the video version, if you prefer it:

    We looked at history and variables. In particular, we looked at:

    • history is used to look at history; use CTRL + R for reverse search in history and CTRL + J to copy the command to your clipboard
    • Dot files (files whose filenames begin with a dot) are hidden by default
    • Variables are named parts of computer memory where values are stored
    • Environment variables are variables passed to the shell by the operating system
    • Shell variables are local to the shell
    • Command path stores all of the places to look for executables
    • Interactive shells require user input, non-interactive don’t
    • Login shells exist to do some things only once
    • Your Terminal application is an interactive, non-login shell
    • Place almost all changes you want permanently in .bashrc file in your home directory

    Let’s carry on with our Linux journey!

  • Linux Tutorial Series – 87 – User environment related files

    Here is the video version, if you prefer it:

    I wanted to list out all files related to the user environment and write a short description when each of them is read. By read, I mean “the file is read and its contents are interpreted as commands which are executed.” Why would this be useful? Just so that you have seen the file name at least once, so that when it pops out in some tutorial, you have a vague idea about what it is used for. Of course, Google the name of the file to be sure what it does, but having some general context doesn’t hurt.

    Here we go…

    Files read by login shells:

    • /etc/profile
    • /etc/profile.d
    • ~/.profile
    • ~/.bash_profile
    • ~/.bash_login

    Note that .bash_profile and .bash_login may not exist in your home directory (they don’t exist in my home directory).

    Files read by interactive shells:

    • /etc/bash.bashrc
    • ~/.bashrc

    As stated in one of the previous articles I wrote, write all of the changes of your environment to ~/.bashrc.

    You may wonder what is the difference between the files in the etc directory and the home (~) directory. If you modify the files in the etc directory your changes apply to the entire system (to all users), while if you modify the files in your home directory the changes apply only to your user. (“Diff between /etc/profile and ~/.bash_profile,” n.d.)⁠ Hence the suggestion to modify .bashrc in your home directory, since then it only applies to you.

    Also, a word of caution – it is always a good idea to copy the files you are modifying (using cp for example) so that you can rename them to the original file name if something goes awry with the modified version.

    I refer you again to read the superbly written answer here: (“Why is /etc/profile not invoked for non-login shells?,” n.d.)⁠

    Hope you learned something useful!

    References

    Diff between /etc/profile and ~/.bash_profile. (n.d.). Retrieved February 7, 2020, from https://www.linuxquestions.org/questions/linux-newbie-8/diff-between-etc-profile-and-~-bash_profile-609191/

    Why is /etc/profile not invoked for non-login shells? (n.d.). Retrieved February 7, 2020, from https://askubuntu.com/questions/247738/why-is-etc-profile-not-invoked-for-non-login-shells

  • Linux Tutorial Series – 86 – Shell variables

    Here is the video version, if you prefer it:

    Let’s talk about shell variables today. Shell variables are temporary variables specific to the shell you are currently running. (Ward, 2014)⁠ Think of it like this – shell variables are temporary, environment variables are permanent.

    Here is an example of setting and printing out the contents of a shell variable:

    mislav@mislavovo-racunalo:~$ VARIABLE=stuff

    mislav@mislavovo-racunalo:~$ echo $VARIABLE

    stuff

    If I try to print the contents of VARIABLE in another shell, I get:

    mislav@mislavovo-racunalo:~$ echo $VARIABLE

    What’s that I get? An empty line. Because in another shell (in another Terminal window), the variable VARIABLE doesn’t exist.

    Hope you learned something useful!

    References

    Ward, B. (2014). How Linux Works: What Every Superuser Should Know (2nd ed.). No Starch Press. Pages 21-22

  • Linux Tutorial Series – 85 – Permanent changes to your environment via .bashrc

    Here is the video version, if you prefer it:

    If you want to make permanent changes to your environment, here is how to do it:

    In virtually all use cases, you should modify .bashrc file located in your home directory (i.e. /home/mislav/.bashrc). The reason for that is that both login shells and interactive non-login shells read these files explicitly (and execute the commands within them). (“Why is /etc/profile not invoked for non-login shells?,” n.d.)⁠

    Here are some lines I have added (appended) to my .bashrc file (in /home/mislav/.bashrc):

    # added for Python

    export PATH=$PATH:~/Python-3.7.4/bin

    # added for Java

    export PATH=$PATH:/usr/lib/jvm/jdk1.8.0_231/bin:/usr/lib/jvm/jdk1.8.0_231/db/bin:/usr/lib/jvm/jdk1.8.0_231/jre/bin:/home/mislav/Downloads/hadoop-2.10.0/bin:/home/mislav/Downloads/hadoop-2.10.0/sbin

    These are the lines I added. They add certain directories to my PATH variable. The general syntax for adding to my PATH variable is:

    export PATH=$PATH:newPath

    where newPath is a path (or multiple paths delimited by a colon) where I want to look for executables. The $PATH get substituted for the contents of the variable PATH.

    When writing this article, I found myself Googling quite a lot to make sure I really understood what files were read by what kinds of shells. A really great read on this is found here: (“Why is /etc/profile not invoked for non-login shells?,” n.d.). The main thing I got from the aforementioned reference is to place all of my changes in .bashrc, but it is such a great answer that I would urge you to read it if you have the time.⁠

    Hope you learned something useful!

    References

    Why is /etc/profile not invoked for non-login shells? (n.d.). Retrieved February 7, 2020, from https://askubuntu.com/questions/247738/why-is-etc-profile-not-invoked-for-non-login-shells

  • Linux Tutorial Series – 83 – Modifying environment variables

    Here is the video version, if you prefer it:

    If you ever want to modify environment variables (and you may sometimes) or some tutorial you find on the World Wide Web advises you to do so, it is useful to know what is going on.

    When modifying environment variables, you are, in most cases, adding something to already existing environment variables. Here is how to do it: (modeled after (Ward, 2014)⁠)

    mislav@mislavovo-racunalo:~$ printenv | grep VARIABLE

    mislav@mislavovo-racunalo:~$ VARIABLE=value

    mislav@mislavovo-racunalo:~$ export VARIABLE

    mislav@mislavovo-racunalo:~$ printenv | grep VARIABLE

    VARIABLE=value

    The commands we are focusing on here are VARIABLE=value and export VARIABLE. Those commands introduce a new variable named VARIABLE with the value value and export VARIABLE places variable VARIABLE in the environment.

    The changes you just made to your environment are non-permanent. Meaning, when you close your Terminal window, the variable VARIABLE will disappear from the environment. There is a way to make these changes permanent, but it is a topic for another post.

    Hope you learned something useful!

    References

    Ward, B. (2014). How Linux Works: What Every Superuser Should Know (2nd ed.). No Starch Press. Pages 21-22