Posts

  • Vim Functions

    One of my programming guilty pleasures is Vim. While there is much discussion around whether or not Vim actually makes you more productive, I like using it for the simple fact that I find it fun use. Now that I’ve used it for a few years, I think I’m finally at the point where I can code in Vim faster than I would be able to in something like Sublime or Atom. I’m sure that says something about the productivity argument even if I’m not sure what. Maybe in another year or two I’ll feel like I have something to add to that discussion.

    There are two things I love about “the Vim experience” that I haven’t been able to find in a “normal” text editor. First is it’s customizability. When I first started learn Vim it was recommended that I start by reading Learn Vimscript the Hard Way. One of the things Steve recommends in his book is to set up a custom .vimrc file and fill it with your own custom keybinds and shortcuts which then give you a totally custom built editor. I’ve been doing that for two years now and have a file that is pretty personal and makes it so when I use Vim - it’s my Vim. You can check my vimrc out on Github.

    The second thing that I love about Vim is it’s depth. Since my initial read-through of Learn Vimscript the Hard Way, I’ve read it several times through and each time through I’ll find something I’d missed previously. It blows my mind that I’ve been using Vim daily for school and work for about two years now and I’m still finding out new things.

    Case in point last night I wrote my first function:

    function! BradyFlip()
        let wordUnderCursor = expand("<cword>")
        let pairs = ["TRUE", "FALSE", "Good", "Bad", "0", "1", "+", "-"]
        let i = 0
    
        while i < len(pairs)
            let queryWord = get(pairs, i) 
            
            if wordUnderCursor ==# queryWord
                let save_cursor = getpos(".")
    
                if (i % 2 == 0)
                    let targetWord = get(pairs, i+1)
                else
                    let targetWord = get(pairs, i-1)
                endif
    
                put =targetWord
                normal diw
                call setpos('.', save_cursor)
                normal viwp
                normal <esc>
                normal jdd
                call setpos('.', save_cursor)
            endif
    
            let i += 1
        endwhile
    endfunction

    I’ll admit - it’s more than a little crude not all of which can be blamed on Vim’s archaic scripting language. The gist of what the function is doing is looking at the word under the cursor and checking to see if it has been defined in the function’s pairs array. If the word is found, the function swaps it with the word’s pair: 0 being replaced by 1, true being replaced by false and vice versa.

    Interestingly the bottom half of the function is my hacky solution to the dilemma of trying to get Vim to insert a variable at the cursor as using <C-R>=variable wasn’t working in normal mode from a script. Feel free to shoot me an email or open an issue on my Github if you know a good refactor for that particular problem.

    As far as better solutions go: I’m sure there’s an already made Vim plugin that does this better than the way I’ve implemented it here. However, much for the same reasons that I have my own Vimrc instead of using something like spf13 - sometimes there’s no better way to learn than to do it yourself.

  • Hello World!

    First post! (ha. ha.)

    This blog is the implementation of something that I’ve wanted to do ever since I went back to school for Computer Science. The idea behind it is that I’m hoping it’s going to become a record of my progress as I work to become a better developer - some sort of public record for myself that reminds that I am improving.

    I’m hoping that it’ll remind me that I’m always going to be learning and that over the next few years I’ll be able to look back and realize how wrong I was, how right I was and just how horrible my javascript really was.

    I’m not setting rules about what I’m going to post about or how often I’m going to update, but instead, I’m going to write about things that I’m proud of, ideas that are important to me and problems that I’ve solved.

    Let’s see what’s going to happen.

    echo "Hello World!"