Archive for the 'vim' Category

Adventures With s/re/g

January 29th, 2008 by peasleer

While working on our independent study, Dave suggested we use foreign keys to establish relationships between our databases. We were using the MyISAM engine by default, which doesn’t support foreign keys. The fix to the problem was simple. Since our database doesn’t have much useful data in it, popping open the createdb.sql script we are using in Vim and issuing one quick command took care of everything:

:%s/);/) ENGINE=InnoDB;/g

Now all of our tables are using the InnoDB storage engine. I love Vim!

Back To School: Three Vim Tips

September 30th, 2006 by peasleer

I’ve been encouraging people to use Vim instead of our Computer Science Department’s suggested emacs. Two things that improve usability of the editor are marking and making backspace able to remove newlines, and one feature that allows you to adhere to coding standards without having to change your .vimrc.

Marking allows you to set ‘marks’ in the file that you can jump to at any time. This is really useful for when you are editing one area, but constantly have to refer to another area in the same file. To set a mark, issue the following command (replacing with a lowercase letter):

:ma

You can then jump to that point from anywhere in the file by hitting ` (backtick, on your tilde key) followed immediately by you supplied above. So if was a, we could make a mark and move to it like so:

:ma a
`a

The second tip is making backspaces able to jump up to the end of the line above it when you get to the beginning of a line. All you have to do is put the following line in your .vimrc or issue the following as a command:

set bs=2

The third and final vim tip for today is overriding your .vimrc on a per file basis. This is incredibly useful for adhering to coding standards with regard to tabstop width. Vim allows you to put a line anywhere in the file with options it will read in to take precedence over options set in other places on the system - by convention, this line is usually placed at the end of a file and is commented out so your compiler or interpreter doesn’t try to parse the line. For an example, we’ll set the tabstop to 8 instead of my preferred 4, and use C style comments:

// vim:ts=8

Pretty neat, isn’t it?

MIPS and .vimrc Fun, Part One

September 23rd, 2006 by peasleer

I’m in a course that requires us to write programs in MIPS assembly. I’m really enjoying the course, but even this early on I’m seeing repetition regardless of the lab or project. I believe my editor is here to make my life as a coder easier, so I’ve thrown together a couple functions for my .vimrc to handle some common tasks. The first is mapping numerical syscall values to constants so I don’t have to look at them while coding. The first version is for all syscalls, and the second is for syscalls I commonly use.

In your .vimrc:



" Makes constants for syscalls
command AllSyscalls call AllSyscallsAsConstants()
function AllSyscallsAsConstants()
    call append(line("."), "EXIT = 10")
    call append(line("."), "SBRK = 9")
    call append(line("."), "READ_STRING = 8")
    call append(line("."), "READ_DOUBLE = 7")
    call append(line("."), "READ_FLOAT = 6")
    call append(line("."), "READ_INT = 5")
    call append(line("."), "PRINT_STRING = 4")
    call append(line("."), "PRINT_DOUBLE = 3")
    call append(line("."), "PRINT_FLOAT = 2")
    call append(line("."), "PRINT_INT = 1")
endfunction

command CommonSyscalls call CommonSyscallsAsConstants()
function CommonSyscallsAsConstants()
    call append(line("."), "EXIT = 10")
    call append(line("."), "SBRK = 9")
    call append(line("."), "READ_STRING = 8")
    call append(line("."), "READ_INT = 5")
    call append(line("."), "PRINT_STRING = 4")
    call append(line("."), "PRINT_INT = 1")
endfunction


To use them, after saving your newly updated .vimrc, just type :AllSyscalls or :CommonSyscalls to have the constants created on the line your cursor is currently at. And despite how it looks, the function will create them in numerical ascending order.

Fixing Backspaces

September 21st, 2006 by peasleer

One annoyance of working with Sun machines is that the backspace key never seems to map in such a way that vim can understand it. The usual way to fix the behavior of the backspace key, as told by :help fixdel, is to issue the following at the vim command line or put the following in your .vimrc:


:set backspace=2

Or:


:fixdel

However, this doesn’t work if the signal being passed from your keyboard to your terminal to vim is incorrect, as it was for me. So, the symptoms of “My terminal acts appropriately on the command line, but in vim backspace produces ^?, and :set backspace=2 and :fixdel don’t fix it” can be solved with a quick line issued at the command prompt:


stty erase ^?

It is important to note that ^? does not mean typing ^ and then ?, it means hitting ‘ctrl-v’ and then ‘?’. Try it: you’ll see the output matches mine. The reason for this is ctrl+v is telling the terminal to capture the signal recieved by the keypress instead of the actual keypress after it has been interpreted. The command above is actually forcing the terminal to interpret the ^? signal as the erase function, which is the effect you want bound to your backspace key.
Remember, if it works, put it in your rc files (such as ~/.bashrc for you bash users) so it will magically work every time you login.