Developing A Game Engine with Perl: Part 8 - Vim
Shawn Holland
Posted on December 6, 2021
As Always... I DO NOT KNOW WHAT I AM DOING.
If you want to start reading from the beginning. Check out the first article in this series
Let's step back from typing code for a moment and talk about how that code is typed. There are many options for a developer to code with, and we all have a preference. It's not so much a matter of right and wrong, as it is of comfort and enjoyment. For me, I code in Vim.
...and Coding in Vim is fun
I was introduced to Vim back in college. Our school used AIX for their server OS and we were taught to use Vim. This was also my first introduction to a Unix environment. Coming from a home Dos/Windows environment and entering the Unix world came with challenges.
Using Vim twisted my brain and took a bit to get use to. But today, after using it for many years, it often feels very rhythmic and fun to code. Also, not gonna lie, it kinda makes me feel that sense of "coolness" I got when watching Hackers for the first time.
Hahaha... That movie does NOT hold up well! Teenager me was so impressionable.
$me->hides_face_in_shame();
We can verify my shame level using this code:
use strict;
use warnings;
use Shameful::movies;
my $movie = shift;
my $me = new Shameful::movies();
my $shame = $me->watched($movie);
my $return = "Great Movie!";
if ($shame) {
$return = $me->hides_face_in_shame($movie);
}
print $return;
exit;
Running that code prints out the following.
localhost:~/Fake_But_Should_Be_Real_Scripts/ #
perl i_loved_watching.pl Hackers
Hack the planet!... More like Hide your face bro!
ANSI in Vim
Working with ANSI codes directly in Vim I see a lot of ^[
.
Now this ^[
isn't the same as shift+6 and [
. No, this represents the escape character and is highlighted blue in my Vim. If it was shift+6 and [
it would be coloured the same as regular text. Something very useful to know, you can type the escape character in Vim by pressing ctrl+v
and then the escape
key.
I split Vim up to open multiple files
I use the :split
and :vsplit
command in Vim often to load in other files. Using split
will stack files above/below each other (split horizontally) and vsplit
does side by side (vertically). Sometimes I need to combine split
and vsplit
to have 3 files at once. Switching between each file in (v)split can be done with ctrl+w+w
So Colourful
Vim supports colour schemes. It's something I don't change often, so I always forget and have to look it up. Since I just did it recently after upgrading the dev/test server, I'll share here with you all. You can change it with the :colorscheme command. The one I'm using is :colorscheme peachpuff. I also have syntax highlighting turned on with the :syntax on. I made it permanent by adding the commands to ~/.vimrc I code in OpenSuSE Linux, Vim config files may vary with your OS.
Don't try this at work
Running a bulk search and replace can cause unexpected results if you're not careful. I know I'm thankful for whoever invented undo.
There are many ways to search and replace the contents of a file in Linux and Perl. However, when I'm in Vim, I often find it quickest to use the :%s command. Typically when I need to apply a search and replace to a whole file I use a command like this:
:%s/\^[//g
The format is :%s/search_pattern/replace_with/g
The search pattern I used in the above example is to find the escape character ^[
I mentioned earlier and replace every occurrence with nothing //g
. I have to precede ^[
with \
so the regular expression search pattern knows it's a literal escape character. I type ^[
using ctrl+v and pressing escape.
How about you?
Do you code in Vim? What sort of tricks and tips have you learned? What colorscheme do you prefer?
If you have any suggestions or comments please share constructively. Also please visit our social media pages for lots of fun videos and pictures showing the game engine in action.
ANSI Game Engine on Instagram
ANSI Game Engine on Facebook
Prev << Part 7 - Fork
Next >> Coming Soon
Posted on December 6, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.