Thursday, July 21, 2016

Updating VIM for Go (Golang) Development

Since my last post, setting up Vim for Go development is not only easier but also makes developing with VIM much more powerful.  Just a single plugin, vim-go, is all that is needed for the Go-specific stuff and a host of new tools is now available to handle things like refactoring, linting, error checking, and more.

Go Tools

Make sure that you have Go installed and then get the various Go tools.

goimports

Go provides a lot of use packages that can be imported but it doesn't like it when you import a package and not use it.  Goimports will automatically insert the right imports for you by looking at your code and will remove unused imports from your source.  It's a great time saver!

go get golang.org/x/tools/cmd/goimports
godef

Godef lets you jump to the location where a symbol is defined.

go get -v code.google.com/p/rog-go/exp/cmd/godef
go install -v code.google.com/p/rog-go/exp/cmd/godef
golint

Golint will lint your source and warns you of potential issues.
go get github.com/golang/lint/golint
gorename

Gorename helps to refactor Go code.

go get golang.org/x/tools/cmd/gorename
errcheck

Errcheck checks your code for actual errors that isn't just lint issues.

go get github.com/kisielk/errcheck
gocode

Gocode provides autocomplete of your Go code.  When combined with vim-go and YouCompleteMe plugins, it allows autocomplete to appear as you're typing.


go get -u github.com/nsf/gocode (-u flag for "update")
It's a bit different for Windows so follow the instruction from the link for more details.

gotags

Gotags generate tags for Go code.  Combined with Tagbar, it will provide a pretty display of the tags in your code.

go get -u github.com/jstemmer/gotags
Guru

Guru is a tool that integrates with editors to help it understand Go code.

go get golang.org/x/tools/cmd/guru
go build golang.org/x/tools/cmd/guru

Oracle (Deprecated and replaced by Guru)

Oracle is a source analysis tool for Go program.


go get code.google.com/p/go.tools/cmd/oracle

Vim Plugins

It used to be that you would manually install a vim plugin for each of the various Go tools as well as using the the plugins that comes with Go itself.  Now, everything has been consolidated into a single Vim plugin, vim-go, and that's all you really need when it comes to Go-specific plugins.  A few other plug-ins such as Tagbar and YouCompleteMe are useful to complement your development environment, though.  I highly recommend that you use Vundle to manage your plugins.

vim-go

Vim-go brings together all the various plug-ins and feature for Go development in VIM including autocomplete, snippet support, improved syntax highlighting, go toolchain commands, etc. in a single package.

YouCompleteMe

YCM is a fast code completion engine for VIM that works as you type.  YCM requires VIM 7.3.584 or above and CMake (you'll need to compile the extension after it's downloaded).

Tagbar

A great way to view the tags in your code.  This requires that you have Exuberant Ctags and gotags installed.

Bufexplorer

My favorite plugin for navigating between VIM buffers.

.vimrc


" Go Specific Stuff
                                                             
au BufRead,BufNewFile *.go set filetype=go                                     
autocmd FileType go setlocal softtabstop=4
autocmd FileType go setlocal shiftwidth=4
autocmd FileType go setlocal tabstop=4

" go-def settings
let g:godef_split=2
let g:godef_same_file_in_same_window=1

" go-vim settings
let g:go_fmt_command = "goimports"
let g:go_highlight_functions = 1
let g:go_highlight_methods = 1
let g:go_highlight_structs = 1

" tagbar settings                                                                  
let g:tagbar_type_go = {
    \ 'ctagstype' : 'go',
    \ 'kinds'     : [
        \ 'p:package',
        \ 'i:imports:1',
        \ 'c:constants',
        \ 'v:variables',
        \ 't:types',
        \ 'n:interfaces',
        \ 'w:fields',
        \ 'e:embedded',
        \ 'm:methods',
        \ 'r:constructor',
        \ 'f:functions'
    \ ],
    \ 'sro' : '.',
    \ 'kind2scope' : {
        \ 't' : 'ctype',
        \ 'n' : 'ntype'
    \ },
    \ 'scope2kind' : {
        \ 'ctype' : 't',
        \ 'ntype' : 'n'
    \ },
    \ 'ctagsbin'  : 'gotags',
    \ 'ctagsargs' : '-sort -silent'
\ } 

Sunday, March 27, 2016

Upgraded to Fedora 23

Upgraded from Fedora 22 to 23 using the new dnf upgrade.  It was a pretty smooth upgrade almost like it was just upgrading packages.

Only issue I've noticed so far was that I wasn't able to access any of my other machines on the network using their .local name.

To fix this, I had to edit /etc/nsswitch.conf and change the hosts line to be:



hosts:      files mdns4_minimal [NOTFOUND=return] dns myhostname mymachines
Which was what was there previously but changed in the upgrade.  I might have accidentally brought it on myself by telling Fedora to replace my local changes with what is defined in the package.  Putting it back tells the system to try resolving using Avahli multi-cast name.

Once the change is saved, then restart the network manager:

sudo systemctl restart NetworkManager.service

Sunday, January 10, 2016

Tools of the Trade

Here is what I have in my professional toolkit.   These are things that I always try to have on my system.  Everything here is free with many them being open source.  I originally published this list about 15 years ago and this year I finally revisited it and cleaned things up.  What I discovered was that the open source projects are the ones that most likely survived the test of time.

For my recommended books list for developers check out this post.

Gap Buffer - My C++ source for a gap-buffer implementation.

Programming Languages

  • Go Programming Language - Open source programming language that makes it easy to build simple, reliable and efficient software (Quoted from the Go site but is pretty accurate).  This could become my favorite language as it blends the compile nature of C with the practical aspects of a scripting language.
  • GNU GCC - Open source C/C++ compiler supported on so many platforms now that I can't list them on. There's even ports of it so that you can compile games to run on the GBA.  C is the foundation language of enabled the growth of our profession and is a must learn of every software engineering.
  • Perl - One of the great scripting languages although admittedly I rarely use it now.  In many instances, Python has taken the mantel of Perl, but Go might replace both.
  • Scheme - I don't really do much with Scheme, but I believe it's worthwhile for engineers to have exposure to this form of programming.

Programmer's Editor

  • Vim - Although I started more as an EMACs user, VIM is my defacto go-to programmer's editor.  Initially I used it because it is often more available on production UNIX servers then EMACS.  The more I used it, the more natural it felt to me then EMACs for text editing.
  • GNU EMACS - As much as I admire VIM, I can't say that EMACS is inferior to VIM in any way.  EMACS is more then an editor, it's a way of life!
  • Notepad++ (Windows)

Databases

  • PostgreSQL - Top notch standards-compliant RDBMS
  • FreeToad - GUI client for accessing Oracle databases.

Multimedia

  • VideoLAN VLC - Open source media player that can play just about any format without the hassle of installing a bunch of codecs individually.

Graphics

  • Blender - Open source 3D modler ala Maya.
  • IrfanView (Windows) Fast graphic view that's small, fast, but loaded with features.
  • GIMP - Photoshop-like graphics program.
  • Inkscape - Vector graphics editor. 

Source Control

Communications

  • Pidgin
  • Weechat

Operating Systems

Linux

Windows/DOS

Cloud

  • Google App Engine
  • Github
  • Bitbucket
  • Google Drive

Monday, July 27, 2015

Minecraft 1.8 Forge, Liteloader, Optifine (and throw in Voxel Map --- might as well).

Say you wanted to install the Optifine mod for Minecraft and you've read my previous post on setting up Forge, Liteloader and VoxelMap for Minecraft 1.8 (or maybe you already have working mods).  Installing the Optifine mod could be as simple as getting the latest 1.8 version of Optfine .jar file (specifically the 1.8 version since as of this writing the other 1.8 versions aren't supported yet by Forge or Liteloader) and sticking it into the mods directory.

Now, there is a good chance that this didn't work for you at all especially if you just read the setup post.  I saw a number of posts on the 'net where people are asking why their Optifine doesn't work and people give answers who some say works and some says they don't.  The main reason is that if you got the latest version of Forge and Liteloader there is a good chance that Optifine isn't compatible with it.

Go to the Optifind Download page and you'll see a link to the changelog link besides each version of Optifine.  Read it and it will say which version of Forge it works with.  If you have a version of Forge that is newer then that, Optifine won't work.

The solution is the install the latest version of Forge that works along with the latest version of Liteloader that works with that version of Forge.  Then you can drop Optifine's jar file into the mods directory.

Friday, July 24, 2015

Use VIM-like HJKL Instead Of Arrow Keys in Linux and OSX

Okay, I admit it...  I move around the file even while in the insert mode of Vim instead of going back to normal mode.  I don't always stay in insert mode, but if I'm typing and notices a typo a few characters back I will go back to the typo without leaving insert mode.  I do this by mapping the CTRL-[HJKL] to left, down, up, right so at least my fingers stays on the home row and not move to the arrows (I have disabled arrow keys in normal mode when I was training myself on Vim and never re-enabled it).

I found that after spending time in VIM if  I switch to other apps I will start using HJKL for navigation.  Of course that usually doesn't work and I end up using the arrow keys (e.g. when moving down menu options in the browser).  Naturally if I'm using those apps a lot more then when I go back into Vim I'll get lazy and start trying to use the arrow keys.  I decided what I want is to use HJKL even when I'm not in Vim and I wanted it to be consistent across Linux and OSX.

On OSX, this is easy to do.  Simply install Karabiner and select the option to use VI mode using the control key.  Then, in any app, simply use CTRL-[HJLK] for the equivalent behavior as using the arrow keys.  To add more consistency between OSX and Linux, I remapped Caps-Lock to the CTRL key (which is also easier to type on a mabook pro).

On Linux, it's a little more work.  I use Caps_Lock as the Switch_mode (SM) key and then defined that SM-[HJLK] combination to be like the arrow keys.  To do this:

xmodmap - pke > ~/.Xmodmap

Add this line to the top of the .Xmodmap file to disable the Caps_Lock key:

clear Lock

Then set the Caps-Lock key to be the Switch-mode key:

keycode 66 = Mode_switch Caps_Lock

This means to set the caps_lock to be the mode_switch key (and caps-lock when it is Shift-Caps_Lock)

Next, add the Left/Down/Up/Right to the third column of the key code record that maps to the letters, HJKL.

keycode 43 = h H Left
keycode 44 = j J Down
keycode 45 = k K Up
keycode 46 = l L Right

This means that for key 45, print "k" when the key is pressed, "K" when it is SHIFT-k and Up when CapsLock-k is pressed (since CapsLock is now the mode_switch key).

Once you save the file, load it with

xmodmap .Xmodmap

Most current desktops will load .Xmodmap when logging in.
You can find out what keycodes maps to what by using xev.

Monday, May 4, 2015

Installing Waypoint Map Mods for Minecraft 1.8 for Windows, Linux and OSX (Yosemite)

Once you set up Minecraft for your family, you'll inevitably be asked to install mods to further enhance the game play.

Installing mods is both simple and annoying for a few reasons:

  • Mods aren't officially supported by Minecraft so there is no single way to install mods. 
  • This means that a mod built for version A might not work for version B.
  • Different mods might require different mod loaders and they might be in conflict with each other.
  • Just like apps on your mobile phone, some mods might not do what it claims to do and can be potentially harmful.
  • Many mods are served from sites that also tries to sneak in adware and malware on your machine when you try to download even if the mods themselves are okay.
  • Minecraft is built on Java and given the security issues a lot of OSs no longer comes with Java so you'll have to install it yourself.
  • If you install the official Java from Oracle (whether OSX or Windows), they try to sneak in adware on your machine as well!
I have been avoiding using mods but when the family demands it what can one do but to get it done?  So, after spending an afternoon learning about Minecraft mods, I thought I share with folks how I installed the VoxelMap mod for Minecraft version 1.8.x. on Linux, Windows and OSX.

VoxelMap is a mod that provides you with a map of your Minecraft world and also allows you to set way points so you can easily teleport to specific locations.  There are a number of mods that do similar things which I actually tried to get first but they either made it too painful to safely download (Xaero's Minimap) or wasn't available for 1.8.4 (Rei's Minimap and Zan's Minimap).  VoxelMap is a bit fancy but it's used by Youtubers that my kids watch so there's a degree of familiarity with it.

VoxelMap requires a mod loader called Liteloader to run.  Because mods are not officially supported by Minecraft, the community have come up with various loaders that provides simpler APIs that modders can use to write mods more easily.  However, because each of these API might be different there is a degree of fragmentation and conflicts between the mods that use different mod loaders.  It seems like Minecraft Forge is emerging as the leader (quantity of mods, support, books/tutorial that teaches using it), but a number of popular mods are also built on Liteloader.  Fortunately, Liteloader is compatible with Forge and in this guide I'm going to install both.

A final note before we dive in.  As of this writing, support for 1.8 is still somewhat "beta" for both Forge, Liteload, and VoxelMap (and pretty much any mod) and also I'm only going to install "client" mods since VoxelMap is just for the players and not a "server" mod which is that the clients connect to when playing multiplayers games.  For the server, it's going to be just the vanilla Minecraft 1.8.4 server that is officially released by Mojane.

Microsoft Windows


To install VoxelMap mod for Windows, you'll need:
  1. Java 1.6+ (to run the installers for Forge, Liteloader, etc).
  2. Minecraft 1.8.x client launcher
  3. Minecraft Forge for 1.8 (see below to avoid getting mislead by the download links)
  4. Liteloader for 1.8
  5. VoxelMap for 1.8

Java 1.6+


It's possible that you don't have Java installed on your system (Microsoft suggests that unless you really need Java, don't install it for security reasons.)  You can download Java from Oracle for free, but if you're not careful you'll get stuck with crappy adware (shame on a corporation like Oracle to use such underhanded tactics).  Fortunately, Microsoft now bundles Java with Minecraft launcher and you can use that version of Java to run the Forge and Liteloader installers.

The path to the Minecraft Java is something like this:  
C:\Program Files (x86)\Minecraft\runtime\jre-x64\<version>\bin\javaw.exe
You can find this by looking at the default Minecraft profile from the launcher and see what the executable path value it has.  Then from the command prompt (Run > cmd):
set JAVA_HOME="<path to java minus the javaw.exe>"
set PATH=%PATH%;%JAVA_HOME%

Minecraft 1.8.x Client Launcher


Simply download from https://minecraft.net/download and install it.

Minecraft Forge for 1.8 


When downloading Forge, avoid the obvious download buttons as they lead to the file hosting sites that tries to get you to download a lot of crapware, malware, adware, etc.  Instead, select "All Downloads" and click on the "i" next to the Installer link (we're NOT going to use the windows installer since it won't know where Java is) and select "Direct Download".

Liteloader for 1.8


As of this writing, only the developmental builds supports 1.8 so you'll need to download the latest snapshot.  Again, you just want to download the *.jar file rather then the windows .exe file.

VoxelMap for 1.8


You can find the download link from the VoxelMap page.  Make sure you grab the 1.8 version from the "Recent files" section and not from the "Download" button which grabs the 1.7.x version.

Installing Everything


Now that you've downloaded everything, the installation for Windows is fairly simple:

  1. Click on the Minecraft installer you downloaded from minecraft.net.  This will install Minecraft.
  2. Click on the launcher that it installed to start Minecraft.
  3. Click "Edit Profile" and change the version of Minecraft from "latest" to "1.8" (this is just a one time thing because Forge needs you to have run that version at least once).
  4. Click "Play"
  5. You can quit once the game menu comes up and change the profile back to latest at this point.
  6. Quit Minecraft completely.
  7. At the command prompt, install Forge with:  java -jar <path to .jar file>.  Select "Install client" then OK.  Start up the client again, pick the "Forge" profile and make sure everything works.
  8. Quit Minecraft.
  9. At the command prompt, install Liteloader with: java -jar <path to .jar file>.
  10. From the menu's "extend to" option, pick the drop down that says Forge... and then install.
  11. Start up the client once again, select the "Liteloader 1.8 with Forge..." profile and make sure everything still works.
  12. Quit Minecraft
  13. At the command prompt, copy the VoxelMap mod to the Forge mod directory:  copy <path to VoxelMap's *.liteload file> %APPDATA%/mods/1.8
  14. You're done!  You should now be able to play Minecraft with the minimap on your screen.

Linux


Installing on Linux is the same steps as for Windows except for the following:
  • Linux might already have Java installed, but if not then use the package manager to install openjdk and you shouldn't have to do anything else beyond that for Java.
  • The path to copy the VoxelMap mod (step 13 above) is ~/.minecraft/mods/1.8 (instead of %APPDATA%/minecraft/mods/1.8).

OSX

Updated:  Sept. 27, 2015 

OSX is more of a pain to set up because it only have Java 1.6 (and not even that unless you manually install it on Yosemite).

First, go through steps 1 to 6 as described in the Windows section (You might get prompted to install Java -- see below).

Just like Windows, the Minecraft launcher for OSX also comes with Java 1.8 so it isn't necessary to download Java separately.  OSX only have Java 1.6 so you should use the version that comes with Minecraft then the default OSX Java.

Java 1.6+


To install Java 1.6, you'll get a prompt the first time you try to start the launcher to install Java if it can't find it.  Simply click "More Info" and follow the instructions to installing Java.  This is fine for playing Minecraft, but doesn't work for Liteloader's installer as it requires 1.7+.

Minecraft Forge for 1.8


You should be able to install Forge just by clicking the installer file or you can do it from the terminal just like it is described step 7 above.
java -jar forge-1.8-*-installer.jar
Then selecting "Install client".

Once installed, start up the client and pick the "Forge" profile.

Liteloader for 1.8


Liteloader is a problem because Java 1.6, which is what Apple installs, doesn't work with the installer.  You can download Java from Oracle but just like the Windows version, you have to be careful or adware will get installed.

Instead, we'll let Forge install Liteloader for us so instead of running the Liteloader installer:
  1. Right click on the installer jar file and select "Open With > Archive Utility".  This should create a folder with all the files from the jar file.
  2. Using the Terminal, go to the folder and copy the Liteloader jar files to the mod directory: cp liteloader-*.jar ~/Library/Application\ Support/minecraft/mods
  3. Now start up Minecraft but continue to use the Forge profile.
  4. If everything starts up fine and it Forge is running then quit Minecraft
Liteloader somes with an installer which you can launch by using:
java -jar <liteloader jar file>
Or you can copy the file to the mods directory and have Forge load it since they are compatible.
cp liteloader-*.jar ~/Library/Application\ Support/minecraft/mods

VoxelMap for 1.8


Just like for Windows and Linux, you just need to copy the VoxelMap mod file to the Forge mod directory:
cp <voxelmap mod file>  ~/Library/Application\ Support/minecraft/mods/1.8

You can now start up Minecraft and see that the VoxelMap mod is loaded.

You're Done!


Now you should be able to use the VoxelMap mod whether in your single player game or if you connect to the 1.8.x server.

Thursday, March 19, 2015

Automatically Start Local Minecraft Server on Linux (Fedora) At Boot

Running your own Minecraft server on Linux is pretty simple.  You simply download the jar file from Minecraft.net and run it from the command line.  However, if you run it straight from the command line you'll need to stay logged in and not kill the terminal otherwise it will stop the Minecraft server.

The way to work around it is to use something like tmux or screen and run Minecraft from there.  That allows you to detach the session, logout, and come back to it at a later time so you now have a "headless" Mindcraft server running.

The final step is to make it so that it automatically starts when the server boots up and shut it down when the server shuts down.  On Fedora, that means using systemd.

I'm assuming you're running Minecraft as user minecraft and the Minecraft jar file is located in ~minecraft.

First, let's create a start-up script in $HOME/start_server.sh:

#!/bin/sh
/usr/bin/tmux new-session -s minecraft -d
tmux send -t minecraft "/usr/bin/java -Xmx1024M -Xms1024M -jar $HOME/minecraft_server.1.8.3.jar --nogui" ENTER
This starts a new tmux session in detached mode and calling the session minecraft.  Then it sends to the tmux session the command to start the Minecraft server in text (non-gui) mode.

Let's make a shutdown script in $HOME/minecraft/stop_server.sh:

#!/bin/sh
/usr/bin/tmux send -t minecraft /save-all ENTER
/usr/bin/tmux send -t minecraft /stop ENTER
echo "Killing minecraft session"
/usr/bin/tmux kill-session -t minecraft
This sends a command to the tmux session named minecraft to
  1. Save the current state of the server (save-all).
  2. Shutdown the minecraft server (/stop).
  3. Stop the tmux session.
Make both scripts executable:

chmod u+x start_server.sh
chmod u+x stop_server.sh
To automatically start and stop the tmux session for Minecraft, create /usr/lib/systemd/system/minecraft.service:

[Unit]
Description=Start tmux in detached session running Minecraft.

[Service]
Type=forking
User=minecraft
ExecStart=/home/minecraft/start_server.sh
ExecStop=/home/minecraft/stop_server.sh
WorkingDirectory=/home/minecraft

[Install]
WantedBy=multi-user.target
Now link it to the right place:

cd /etc/systemd/system
ln -s /usr/lib/systemd/system/minecraft.service minecraft.service
Finally, you want to start it and enable it to start at boot:

systemctl start minecraft.service
systemctl enable minecraft.service
I also suggest having a backup script that regularly back up your world:

#!/bin/sh
printf "Starting backup..."
date +%D
cd /home/minecraft
tmux send -t minecraft /save-off ENTER
tar -czvf backup/world-`date +%m%d%y_%H_%M_%S`.tar.gz world
tmux send -t minecraft /save-on ENTER
With a cron job as follows:

0 15 * * * /home/minecraft/backup.sh >> /home/minecraft/backup/backup.log 2>&1

This says to run the job at 3pm every day and copy the output to backup.log.

The saved archive (even compressed) can get pretty big and can easily eat up disk space so you might want to only keep the a few of the most recent save and automatically delete the old ones (or move old archives somewhere else).  That can be done with another cron job with a one line script:

ls -tr world-*.tar.gz | head -n -5 | xargs --no-run-if-empty rm
This will delete all but the 5 most recent files and do nothing if there are less then 5 archive files.  This can be put into a script with some logging /home/minecraft/cleanup_backup.sh:

#!/bin/sh
printf "Deleting old backups..."
date +%D
cd /home/minecraft/backup
ls -tr world-*.tar.gz | head -n -5 | xargs --no-run-if-empty rm -v
and have a cron job as follows to run nightly:

# Delete old backup files at 11:20pm but keeping a few most recent ones.
20 23 * * * /home/minecraft/cleanup_backup.sh >> /home/minecraft/backup/backup.log 2>&1

Now each time your server boots, it will automatically run the Minecraft server as user minecraft and once a day it will back up your Minecraft data while removing old archives.