Creating and Running a Code Snippet
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
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.
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 we 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.
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: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: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.
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 opened 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.
After doing that, you can see in here we have in your 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.
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.
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.
Become a Member to join the conversation.