Gigi Labs

Please follow Gigi Labs for the latest articles.
Showing posts with label vim. Show all posts
Showing posts with label vim. Show all posts

Sunday, September 15, 2013

Linux: vi Essentials

Hi people! :)

I've already briefly discussed vi in my article "C: Hello World on Linux". vi is a text editor, and a pretty versatile one at that. It takes a bit of time to learn because there are so many commands you can use, but once you learn it, you can do things really fast.

Learning to use a text editor like vi on Linux is important because you can use it for many things, from editing configuration files to writing code. Even better, make sure you have vim (vi improved) which brings syntax highlighting and better keyboard navigation (and probably more):


Now, there are many tutorials and references on vi, including this great cheat sheet. However, instead of teaching you every vi command, I'm going to go through the most important commands in a practical manner, in the usual spirit of Programmer's Ranch.

So let's start off by opening a file called test.txt:

vi test.txt

Note that the file does not actually exist until you save it. You can save it at any time using the :w command. You can save and quit using the :wq command. You can try to quit only with the :q command, but if there are pending changes, you'll get a warning (as below). In that case, you can quit and discard your changes with the q! command.



Basically, in vi you can be either in insert mode (in which you're actually writing stuff in the file) or in command mode (in which you write commands, such as those I just mentioned, and press ENTER to execute a variety of operations. You start off in command mode, and can switch to insert mode using various commands (which I'll mention in a minute). You can go back into command mode by pressing the ESC key.

To try out a bunch of commands, go into insert mode (press the 'i' key) and paste the following lyrics from The Eagles' song Lyin' Eyes:

City girls just seem to find out early
How to open doors with just a smile
A rich old man
And she won't have to worry
She'll dress up all in lace and go in style

You can move the cursor around the text using the arrow keys, whichever mode you're in. If you're in command mode, you can also use the h, j, k and l keys to move left, down, up or right (these are especially important if you have some old version of vi that doesn't support the arrow keys.

The basic keys to go into insertion mode are 'i' and 'a', meaning 'insert' and 'append' respectively. Try them both by moving your cursor over the 'r' in 'rich' while in command mode, and pressing the respective key. You'll find that 'i' goes into insert mode right before the 'r', while 'a' goes into insert mode starting right after it.

To copy and paste a line of text, go on that line and press yy. Then, move to another line and press p to paste the line of text right after it:



You can use dd to delete an entire line, or D to delete from the cursor until the end of the line. Try that by pressing dd while you are on the last line, and pressing D (Shift+d) while the cursor is at the beginning of the word "lace".

You can delete single characters by pressing 'x'. One of the great features about vi is that you can instruct it to repeat commands for a number of times. So, the command 5x will delete 5 characters. This also goes for other commands, for example you can delete 5 lines by using either d5d or 5dd.

The 'u' key will allow you to undo your last command - great if you accidentally deleted something. The '.' key, on the other hand, will allow you to repeat a text edit - but not in the way you're used to it. What it does is remember what your last command was, and reapply it at the cursor's current position. So if your last command was 'x', pressing '.' will delete the character at the cursor position.

At this point we've covered the very basics, and you can already get to work with these few commands. However, I'd like to add a few other commands that I've found particularly great when using vi on the job. Start over with a fresh copy of the lyrics at the beginning of this article.

Let's say we want to replace the word "lace" with the word "red". There are many ways we can do this (e.g. 4x on "lace", i for insert mode, and then type "red). A faster way is to hit cw (change word) while the cursor is on the beginning of "lace". That deletes "lace" and goes into insert mode automatically, so then you just have to type "red":



Another great command is 'o'. This will create a new line below the one your cursor is on, move the cursor to the beginning of it, and switch to insert mode. A variant is 'O' (Shift+o), which starts a new line directly above the one your cursor is on.

As an exercise in these extra commands, start off with this text:

select *
from yourtable
where 1=1

and find a quick way to change it to:

select *
from mytable
where 1=1
and 2=2

Great! :) I hope you find this useful, and don't forget to like the Programmer's Ranch Facebook page to stay up to date with articles as they are posted.

Wednesday, August 7, 2013

C: Hello World on Linux

Hello people!

Today I'm going to describe how you go about writing, compiling and executing C code using the Linux command line (also known as shell or terminal).

You will first need to make sure you have the tools necessary to compile C code. In particular you need gcc. You can get it by installing the build-essential package. On a Debian-based Linux distribution such as Ubuntu, you'd use the following command in the terminal:

sudo apt-get install build-essential

...and then, after entering the root password (if necessary), you proceed with the installation:


To actually write the C code, you can use the vi editor. Even better, you can make sure that you have the vim (vi improved) by using the following command:

sudo apt-get install vim

Then use the following command to edit a file called hello.c (it doesn't need to exist yet):

vi hello.c

This opens the vi editor. vi is very powerful but takes a little getting used to. You start off in command mode, and just about any letter you type has a particular meaning. If you press the 'I' key, you go into insert mode, and can type code to your heart's content. Do that, and enter the following code:

#include <stdio.h>

int main(int argc, char ** argv)
{
    printf("Hello world!\n");

    return 0;
}

Over here we're first including the stdio.h library, which allows us to do input/output (I/O) such as outputting text to the terminal. The actual code goes into the main() function, which returns an integer. You can ignore the argc and argv bits, and just use this as a template for your code, for the time being. We actually write something to the terminal using the printf() function. The only thing that might seem a little special here is the \n thing - it's an escape sequence that outputs a newline.


Once you're done typing the above, press ESC in vi to go back to command mode. Then type :wq and press enter - this saves the file (w) and quits (q).

Back in the terminal, type the following command to compile the code in hello.c:

gcc hello.c -o hello

The -o part means that the next argument (in this case "hello") is the name of the executable to be produced by gcc. On Linux, executables don't need to have extensions such as .exe, although it's perfectly fine to include them to make it easier to recognise them.


Finally, type ./hello and press ENTER to run the program. You should see "Hello world!" as the output, as above.

Note: just in case you can't run the program because of some permissions, try the following command:

chmod 777 hello

Great! This simple tutorial showed how to write C code using the vi editor, compile it using the gcc program, and execute it from within a Linux shell. Come back for more tutorials!