A dose of honesty

When a software developer says something is impossible, they are almost certainly lying.

Tom Fine

Recently discovered Tom’s excellent post about the psychology of programming.  Don’t be distracted by the IMAP-specific context — he’s hit every nail for miles around squarely on the head.

Adventures in vim

Just ran across this question on Stack Exchange by DJ McMayhem, which pointed out a great tip for commenting blocks of code. Say you have

int main()
{
  int answer=42;
  string question;
  for(int i=0; i<answer; ++i) {
    question += "what do you get";
  }
  return answer;
}

and want to comment out the question. I used to do that by putting the cursor on line 4 (string question), hitting ma to save my place in mark a, moving down to line 7 (the end of the for loop), and doing :'a,./s/^/\/\//<Enter>.

Thanks to DJ McMayhem, I can now do that by starting on line 4, hitting v3j to select the lines in question, and doing :norm0i//<Enter>. Result:

int main()
{
  int answer=42;
//  string question; 
//  for(int i=0; i<answer; ++i) {
//    question += "what do you get";
//  }
  return answer;
}

Original: 20 keystrokes (counting Enter, but not counting Shift presses as separate keystrokes)

New: 13 keystrokes 🙂

My next adventure is to wrap :normal in a function so I can use :N or some such, for a savings of a further three keystrokes!

Adventures in vim

Forgot to mention — if this is all Greek to you, don’t worry! I’ve been learning from fellow Vim users and sharing tips since the late 1990s. Give it time; it will come.

Following up on last time — you can also use motions like ip with : commands that normally take ranges.

Type v<motion>, where <motion> is ip or G or some other cursor motion command. Vim will highlight the text from the cursor to wherever <motion> takes you. (If only vim had a motion key for second star to the right and straight on ’til morning!) You can then issue any : command and it will operate on the selected text.

I often use external sort or fold utilities via filter commands. Those are even easier – you don’t even have to type the :. For example, vip!fold -s -w 70<Enter> folds long lines in the current paragraph. Sure beats what I used to do, which was {:.,/^$/!fold -s -w 70<Enter>! (Makes total sense, right? 🙂 )

Adventures in vim

Vim is my text editor of choice.  (emacs users, commence flaming!)  Yesterday I discovered ip and today I discovered Control-O.  Best shortcuts ever!

ip selects the current paragraph, delimited by blank lines unless you set the modes otherwise.  I used to select whole paragraphs with {y}, which copied extra lines and lost my cursor position.  Now I can copy with yip, which grabs just the paragraph text and keeps the cursor firmly planted.  Fantastic!  See :help object-select for more.

Control-O is a get-out-of-insert-mode-free card.  When inserting, hit Control-O and you’ll be back in command mode for the duration of one command.  That can be a colon or immediate command.  For example, <Ctrl-O>f, will jump you forward to the next comma — without leaving insert mode.  Esc, w or f or / or what have you, i has been my everyday for years. I am thrilled about saving one keystroke per such action times ~1e100 actions over my vimming life. More at :help ins-special-special.

Stay tuned for more adventures in vim!