Vim Tip: Setting File Name in the Title Bar (Not the Status Bar)

When I first started using Vim in Ubuntu, I noticed that while editing a file, title bar of the terminal window would display something like this:

sampson-chen@ubuntu: ~/src/reviewboard/reviewboard/reviews/ui

which falls just short of being helpful because it omits the most important part: which file exactly is being edited. It is not realistic (and indeed wasteful of mental resources) to keep track of this information especially when often several files are open simultaneously across 5-6 terminal sessions. The problem is yet more pronounced when these files happen to reside in the same directory.

After a bit of digging, I found a solution. Add the following to your ~/.vimrc:

let &titlestring = expand("%:p")
if &term == "screen"
  set t_ts=^[k
  set t_fs=^[\
endif
if &term == "screen" || &term == "xterm"
  set title
endif

(Modified from the example at http://vim.wikia.com/wiki/Automatically_set_screen_title – To create ^[, which is escape, you need to enter CTRL+V <Esc>)

This gives you the full path in the title bar:

/home/sampson-chen/src/reviewboard/reviewboard/reviews/ui

which worked well enough for about a week, until I came across a simpler (and better) solution. Add the following to your ~/.vimrc instead:

set title

And it will give you the file name first, then the prefixing path, and then “VIM”:

base.py (~/src/reviewboard/reviewboard/reviews/ui) - VIM

TL;DR – add “set title” to your ~/.vimrc

Misconceptions: Serialization vs. Serializability

I recently uncovered a blunder of mine: conflating serialization with serializability in common CS usage.

In short:

Serialization is the process of converting data structures / objects (e.g. in memory) into a format suitable for storage or transmission, such that it can be reconstructed later (e.g. Converting each node from a doubly-linked list in memory into JSON, where each serialized object has URI pointers identifying who its neighbours are, or where it belongs in the sequence.)

Serializability has to do with DBMS concurrency control: whether a series of concurrent transactions can be ordered and executed in parallel such that the end result is guaranteed to be the same as if they were executed in sequence.

Also, the definition of Serialize in common English usage:

1. Publish or broadcast (a story or play) in regular installments.

2. Arrange (something) in a series

The idea of serialization wasn’t new to me, but it’s good to finally put a name to it.

Sampson’s Notes, Flashcards, and other Academia Stuff from CS at UWaterloo

This post will be a mix of links to notes and flashcards, among other things. I’ll add new links to here periodically as I find and backdate through archives, or as I make new ones.

===== Flashcards =====

I find self / peer-quizzing with flash cards & doing sample exams to be the most effective ways to study. Memory retrieval exercises work much better than just reading material over and over (not much retention that way).

How to study the decks on QuizLet:

  1. Uncheck “Both Sides”
  2. Check “Term First”
  3. Click to flip the card
  4. Use left / right arrow keys to move between cards

Alternatively, you can download these study decks from QuizLet.com with iOS apps, just search for “CS 458 UWaterloo”, I found it helpful to quiz myself during commute / in between classes:

http://itunes.apple.com/us/app/flashcards*/id403199818?mt=8

http://itunes.apple.com/us/app/flashcards+/id408490162?mt=8

===== CS 458 – Computer Security and Privacy =====

CS 458 – Up to Midterm

CS 458 – After Midterm to Final Exam

===== CS 349 – User Interface ===== 

CS 349 – Midterm to Final Exam

===== CS 446 – Software Design & Architecture ====

(These are arranged by slide numbers, but the course content vary from prof to prof:)

http://quizlet.com/12736879/cs-446-lecture-notes-4-5-flash-cards/

http://quizlet.com/12994648/cs-446-lecture-notes-6-software-design-patterns-part-1-flash-cards/

http://quizlet.com/12995256/cs-446-lecture-notes-7-software-design-patterns-part-2-flash-cards/

http://quizlet.com/12997661/cs-446-lecture-notes-8-refactoring-detailed-design-flash-cards/

http://quizlet.com/12994479/cs-446-lecture-notes-9-software-design-metrics-flash-cards/

(If you really wanted the flashcards for slides #1-3: the online version got deleted – I can upload a local copy, just ask)

===== PHIL 215 – Professional and Business Ethics ====

Link to all of the lecture notes on Google Docs

Flashcards – Midterm

Three Shortcuts to Quickly Change Directories in Linux

While working on projects in Linux, I often have to navigate through long directory paths with cd to arrive at the working directory where the most-accessed files are kept. For example:

slc@linux:~$ cd SmileForward/smileforward-backend/sfsite/smileforward/

Smart auto-complete with tab helps, but it’s still rather inefficient, especially when you have to repeat this sequence across multiple terminals (and again every time you restart the OS).

My initial reaction was to write a script with a short name:

slc@linux:~$ vim sf

~/sf:

#!/bin/bash
cd SmileForward/smileforward-backend/sfsite/smileforward/

But this apparently doesn’t work, as the shell stays at the same directory after executing the script:

slc@linux:~$ chmod +x sf
slc@linux:~$ ./sf
slc@linux:~$

After a bit of digging, I discover the problem:

Each time a script is run with dot slash “./”, a brand-new subshell is spawned from the current shell to run whatever is in the script, and then it terminates without affecting the parent shell. So the new shell does change to the specified directory, but then it terminates and control is returned to the parent shell.

To remedy this, here are three different shortcuts:

1. Run the script with . or source:

slc@linux:~$ . sf
slc@linux:~/SmileForward/smileforward-backend/sfsite/smileforward$
slc@linux:~/SmileForward/smileforward-backend/sfsite/smileforward$ cd
slc@linux:~$ source sf
slc@linux:~/SmileForward/smileforward-backend/sfsite/smileforward$

Running a script with . or source executes it within the current shell (without spawning a subshell).

2. Name the shortcut as an environment variable and save it in ~/.bashrc (so bash shell loads it every time a new bash shell spawns):

slc@linux:~$ vim ~/.bashrc

~/.bashrc

export sf=~/SmileForward/smileforward-backend/sfsite/smileforward
slc@linux:~$ cd $sf
slc@linux:~/SmileForward/smileforward-backend/sfsite/smileforward$

3. Make an alias for the cd script (best solution)

slc@linux:~$ vim ~/.bashrc

~/.bashrc

# run the command closed by single quotes when sf is encountered
alias sf='cd ~/SmileForward/smileforward-backend/sfsite/smileforward' 
slc@linux:~$ sf
slc@linux:~/SmileForward/smileforward-backend/sfsite/smileforward$

Alternatively, the alias can be:

~/.bashrc

# Run the sf script from option 1
alias sf='. ~/sf'

~/.bashrc

# cd to the sf environment variable from option 2
alias sf='cd $sf'

Hopefully this saves you some time when navigating between commonly visited directories in Linux.

Another very popular solution for changing directories is z (https://github.com/rupa/z). I’m new to z, so it’s left up to the reader to explore.

(P.S. you can also symlink to the directory: ~/SmileForward/smileforward-backend/sfsite/smileforward/, but this leads to a less versatile result as there’s no easy way to move up to a parent directory)