Skip to content

Derek Wyatt's Blog

Vim, OO Design and Piles of Uselessness

Archive

Tag: Vim

(NOTE: There’s a more up to date version of this in the General C++ Settings section.)

Finally! Vim’s decision to add a ‘shiftwidth‘ to everything I type when I’m inside a namespace is thoroughly annoying and there appears to be no “standard” way to fix this in Vim aside from writing your own function for use in the ‘indentexpr‘ option.

Well I finally got around to writing this up and, while extremely crude, it appears to work alright. You’ll find the function definition below as well as in the General C++ Settings section.

" Fix up indent issues - I can't stand wasting an indent because
" I'm in a namespace.  If you don't like this then just comment
" this line out.
setlocal indentexpr=GetCppIndentNoNamespace(v:lnum)

"
" GetCppIndentNoNamespace()
"
" This little function calculates the indent level for C++ and
" treats the namespace differently than usual - we ignore it.  The
" indent level is the for a given line is the same as it would
" be were the namespace not event there.
"
" This function is rather crude but it works.
"
function! GetCppIndentNoNamespace(lnum)
    let nsLineNum = search('^\s*\\s\+\S\+', 'bnW')
    if nsLineNum == 0
        return cindent(a:lnum)
    else
        let incomment = 0
        for n in range(nsLineNum + 1, a:lnum - 1)
            let cline = getline(n)
            if cline =~ '^\s*/\*'
                let incomment = 1
            elseif cline =~ '^.*\*/'
                let incomment = 0
            elseif incomment == 0
                if cline =~ '^\s*\S\+'
                    return cindent(a:lnum)
                endif
            endif
        endfor
        return cindent(nsLineNum)
    endif
endfunction

Ah, now this is complete bliss… I’m writing to you straight from within MacVim using the blogit.vim script. Up until now, the best way I could use Vim to write up on this site was to use MacVim’s great little feature of hacking Cocoa to allow you to edit any NSTextField natively within the MacVim session. But now, I can just write straight within MacVim and then publish directly from here.

I owe a big thanks to Romain Bignon for this one. Excellent plugin!

Snapshot 49 is out! Thanks a lot, Björn and supporters for this great version of Vim for the Mac. Well done.

I use Vim mostly at work on Windows (don’t even get me started on that lovely O/S), working on two different machines – my desktop and my laptop – neither of which are backed up. That presents a couple of problems:

  • How do I keep the two machines up to date with each other?
  • How do I avoid the death crash from hell?

I solve both of those problems with one move – keep the configuration in Perforce. Now, I’m not a huge fan of Perforce but it’s all I’ve got at work, and it certainly does the job when I want to solve these problems. You don’t need much to handle this issue so any version control system should do (so long as the repository is remote and backed up for you, of course :D).

Once you’ve set up your Vim files into your version control system, you just need to tell Vim where they are.  You do this with the $VIM environment variable (or %VIM% if you’re on Windows and you just want to be really pedantic about the details :D).  You don’t need to do this, of course, if your version controlled Vim files map to the same place as Vim would normally look, but in my case they map to somewhere really weird so I need to help Vim out by telling it where the files are.

Then it’s just like versioning any other piece of code.  Check config files out, modify them, check them in, sync them to other machines.  Normally I work on my desktop box, so the first thing I do when I power up my laptop is p4 sync my Vim configuration so I can get the exact same experience as I had on my desktop, right away.

I’ve completed the video series on the Vim BASIC Movement Tutorials.

Pick it up here.  It’s also catalogued on this site, right here.

Ever been inside a code block and want to reformat (via ==) a number of lines but not the whole file and not line by line?

Let’s say you’ve got code like this:

class MyClass
{
public:
    MyClass()
    {
std::cout << "In MyClass constructor" << std::endl;
}

    int somefunc()
    {
for (;;)
    {
std::cout << "breaking" << std::endl;
    }
    }
};

That’s just not ideally formatted, is it? Here’s one cool way to format it:

  • Put the cursor somewhere inside the outer most block – say on public: or on the blank line between the constructor and the function.
  • hit: =aB

That will format “a block” and that means it formats the outer block recursively down to the inner most blocks. Not that this is a great demonstration, but it looks like this:

class MyClass
{
  public:
    MyClass()
    {
        std::cout << "In MyClass constructor" << std::endl;
    }

    int somefunc()
    {
        for (;;)
        {
            std::cout << "breaking" << std::endl;
        }
    }
};

David Fishburn (of vim_use Mailing List and other fame), suggested that I put up some videos regarding the vimrc file, and I thought that this was a great idea, so I started this morning – not with the videos though :) I want to get a decent idea of what I want to talk about before diving into that medium, so I started with a bit of work on this site with some pages on the vimrc. So far all I’ve got is the bare minimum, but I’ll be fleshing that out in the near future.

I suppose with the addition of the Vim pages on the right side of this page, I can say that I’ve now “begun” my new blog. I’ve had others in the past that were all pretty stupid, but I’m trying to make a commitment to Vim, to write about it, Screencast it, and Twitter it.

Watch this space…