Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Creating and Running a Code Snippet

00:00 In this video, we’ll create our little code snippet using VIM. Let’s get started. We’ve already mentioned how to open up a new file in VIM, so I’m just going to create it right here, typing vim and test.py.

00:14 I give it a filename and just say “Open it up with vim.” VIM tells me down here we’re creating a [New File] called "test.py".

00:23 Sweet! Okay, so, we’ve already learned in the last video that we’ll have to switch to insert mode, so I’m pressing i for insert. You can see down here the feedback—we’re in INSERT mode now—and now I can start typing.

00:37 So I’ll say for x in range(5)and this is just our simple loop. I’m going to say—uh, and here, I want to show you something. So, one thing that’s important is you want to keep indentation and spaces, like, you don’t want to mix those up, and here it actually makes a big difference in this editor, when using VIM.

00:58 So, when I press the Tab character here, it indents quite a lot to the right. You see, when I start typing here, I’m sitting over there. Okay. If, instead, I just do the spaces—the amount of spaces that are usually part of a Python indentation, which is four spaces—press Space four times, one, two, three, four—I get in only about half the way.

01:19 So, here you want to make sure that you don’t mix those and you can also see that in VIM, it doesn’t give you this automatic indentation. If I press Enter up here, it just takes me into the new line, and that means that I have to know that for a for loop, I’m going to need some indentation, plus I have to press it all by myself.

01:37 VIM’s no help in that one. So one, two, three, four, and I did my indentation, and I continue writing here. I say x = x**2 (x squared).

01:48 And then, again, you see I’m going to the next line. It doesn’t help me with the indentation. And I just want to show you this again, if we would now mix tabs and spaces, pressing Tab takes me here, so that would mess up things, okay?

02:00 So we just stick with making four spaces, again, and then printing this out. Okay, there we are.

02:09 This is our little loop written in VIM. You can see that there’s, like, some annoyances with it. So there’s four spaces, we don’t have automatic indentation. Also, we don’t have any syntax highlighting here—this is just plain text.

02:23 We don’t even see that it’s Python code here at the moment. That’s just what comes with the fact that this is a very plain just text editor that treats code as text, all right? In a later video, we’ll see that there’s a lot of enhancements we can do to VIM, and we can switch on syntax highlighting, we can switch on line numbers, et cetera. We’ll take a look at that, but for now, let’s just stick with this. The very plain version: we open up VIM, we type our code—this is how it works. After this step, we’re going to have to save it, so I exit the insert mode or enter command mode, however you want to say it, press the colon (:) for saying “Now we’re going to do a command,” and I’m going to say “Save this,” that’s the :w, and then quit.

03:05 After doing that, you can see in here we have a new file test.py. Let’s take a look at it and what’s in there. All right, so this is what we just wrote: our code, the little loop we’re working with. And another thing that’s important to think about when using VIM is I can’t really run the file from within VIM.

03:24 It’s just a text editor that allows me to write up text. Now, that could be code, that could be just normal text as well. So if I now want to run this code, I have to go outside into the command-line interface, using Bash, and now type in python test.py, and that’s going to execute my code. Now we see the output coming back here to the command line.

03:45 Yeah, that’s us running the code. So there’s two steps involved. We can both do them inside of just this terminal window. That’s because VIM is built into all these Unix command-line things, so I can just say vim test.py, type my code here, exit, and then next step—so, I created the code, next step is going to be I want to run it, so I say python test.py, and this executes it for me. Now I’m getting the output here.

04:15 All right. So, we saw how to create and run our code snippet using VIM, and that’s it for this video. In the next video, we’ll have a short talk about debugging.

Become a Member to join the conversation.