Friday, March 11, 2016

Vim Tips (2016 Edition)

This entry is all about my favorite text editor: vim. Every year it seems I'm learning something new about it and I wanted to put all of my top vim features into one place so they are easily located.

Number 3: Replace ^M's in GVim

I don't much care for GVim's weird combination of mouse interaction and vim editing. It really complicates the simplicity of vim. However I prefer GVim to Notepad++, notepad, or any other text editors because it is still more simple and memory efficient. In a pinch I'll open it up to modify a file quickly.

One of my big items in years past has been getting rid of ^M's in files (see the following article):

http://www.tech-recipes.com/rx/150/remove-m-characters-at-end-of-lines-in-vi/

However, this method doesn't work out of the box in GVim for some reason. Therefore there is another solution: use # to select ^M characters.

1) Find a ^M in your file that is easily navigated to.
2) Press the # key while in command mode.
3) At this point the ^M's in the file should be highlighted.
4) Press the colon key to type in a command.
5) The command entered should look like this:

              :1,$ s//

6) At this point all of your selected items should be gone.

Number 2: VsVim is Your Ticket to Great Vim Editing in Visual Studio

Most of my day is spent editing in Visual Studio. Visual Studio has the best debugger of any IDE (in my opinion). It feels like the industry standard. It's got awesome tools for viewing threads, breaking on exceptions, etc. It's so smooth. The only thing it was lacking was Vim- until a coworker recommended a plugin called VsVim:

https://visualstudiogallery.msdn.microsoft.com/59ca71b3-a4a3-46ca-8fe1-0e90e3f79329

This is as close to real Vim as it gets! All of the key bindings I've tried work. The only thing I found that didn't work was the regular expression find/replace. That is easily accomplished through the Find/Replace dialog so it's no big deal. This is a great plugin that doesn't hurt Visual Studio's performance.

Number 1: Insert the Current File Name

When it comes to bulk editing headers it's nice to be able to just come up with a macro and run it. With Vim it's easy:

1) Go into edit mode.
2) Hold Ctrl, press the "r" key. (at this point a quote shows up)
3) Release Ctrl key.
4) Press the % key.

At this point the name of your file is inserted. It's that easy!

Zombie Port Blocks

Recently I had the privilege of trying to debug a highly distributed system- there were two client processes that end up spawning other processes. Let's call the two client's Process A and Process B. While debugging in Visual Studio each of the processes crashed.

Subsequent times debugging the server/clients would outright fail! It was pretty bad. After some quick googling around, I found this stack overflow question:

http://stackoverflow.com/questions/8688949/how-to-close-tcp-and-udp-ports-via-windows-command-line

Yeah, the accepted answer isn't the right one. Check the steps listed on the most popular answer. Here's what you do:

> netstat -a -n -o

That will print out all of the processes that have opened ports. For me it showed that port 9001 (Process A) and 9002 (Process B) were open. Here is the caveat: the PID for each of those processes listed from netstat did not exist! The task manager did not show them.

What happened was that Process A and Process B both launched more processes before they crashed- this caused Windows to hold onto the ports until the processes completed. The way I solved my issue was finding the spawned processes and killing those. That ended up finishing Process A and Process B.

See this stack overflow question for more info:

http://stackoverflow.com/questions/15216881/pid-exists-in-netstat-but-does-not-exist-in-task-manager