glasses-logo

<dominikgorecki> Web development on the Microsoft Stack <dominikgorecki>

Using Vim in Windows

I have a love affair with Vim. It’s the programmers text editor: with just a few key-strokes you can do just about anything. This is what programmers live for! We share a collective belief that the answer to all of the world’s problems, lay just a few key-strokes away. Vim–ever so slightly–confirms our mutual delusion. Without moving your hands away from the keyboard, you can edit, paste, search, replace, undo, redo, automate, and this and more, and so much more.

Vim is especially popular among the Unix\Linux crowd where Vi(m) originated. However, there’s no need for windows power-users to lag behind. gVim (graphical vim) is a great port of Vim for windows. In this article I will go over my _vimrc (windows version of .vimrc) and my basic setup.

I based my vimrc file on Yan Pritzker’s, and I recommend you check his setup up (especially if you’re running on a mac).

  • The first thing you want to check is to ensure that your $HOME directory is set to C:/Users/[username]. If it’s not, then some of the features in this setup may not work properly.
  • Next you’ll want to create the folder $HOME/vimfiles/, $HOME/vimfiles/backups, $HOME/vimfiles/ftplugin, $HOME/vimfiles/views
  • Finally, you’ll want to create a “_vimrc” in the $HOME directory

 

_vimrc Contents with Detailed Comments

” Begin with ensuring that we are running in vim mode not vi
set nocompatible” Set the color scheme of choice.
” In vim ‘:colorscheme [tab]‘ to see more options
colorscheme pablo

” Start in documents folder [WINDOWS specific]
” (this is not as useful if you start vim from cmd line since
” in that case you may want the pwd to be the dir where you
” loaded vim from; however, since most of us start gVim from
” the start menu then starting off in yoru documents folder
” is a good choice)
cd ~\Documents

” The following two lines turn on highlight for search (1st
” line) and allow you to use to turn off highlight
” for that search. The highlight will return on your next
” search.
set hlsearch
nnoremap :nohlsearch:echo

“Line numbers are good–turn them on
set number

“Allow backspace in insert mode
set backspace=indent,eol,start

“Store lots of :cmdline history
set history=1000

“Show incomplete cmds down the bottom
set showcmd

“Show current mode down the bottom
set showmode

“No sounds
set visualbell

“Reload files changed outside vim
set autoread

“Modeline option enabled for custom vim settings per file
set modeline

” This makes vim act like all other editors, buffers can
” exist in the background without being in a window.
” http://items.sjbach.com/319/configuring-vim-right
set hidden

“turn on syntax highlighting
syntax on

” ================ Turn Off Swap Files ==============
set noswapfile
set nobackup
set nowb

” ================ Persistent Undo ==================
” Keep undo history across sessions, by storing in file.

set undodir=~/vimfiles/backups
set undofile

” ================ Indentation ======================

set autoindent
set smartindent
set smarttab
set shiftwidth=2
set softtabstop=2
set tabstop=2
set expandtab

filetype on
filetype plugin on
filetype indent on

set list listchars=tab:\ \ ,trail:·

set nolist

“Don’t wrap lines
set wrap

“Wrap lines at convenient points
set linebreak

” ================ Folds ============================

set foldmethod=indent “fold based on indent
set foldnestmax=3 “deepest fold is 3 levels
set nofoldenable “dont fold by default

” The following lines save the view of the file including
” the folds and automatically saves and opens them. The
” next time that you open up the same file it will
” remember where you closed and open all the folds!
set viewdir=~/vimfiles/views
autocmd BufWinLeave *.* mkview!
autocmd BufWinEnter *.* silent loadview

” ================ Completion =======================

set wildmode=list:longest
set wildmenu “enable ctrl-n and ctrl-p to scroll thru matches
set wildignore=*.o,*.obj,*~ “stuff to ignore when tab completing
set wildignore+=*vim/backups*
set wildignore+=*sass-cache*
set wildignore+=*DS_Store*
set wildignore+=vendor/rails/**
set wildignore+=vendor/cache/**
set wildignore+=*.gem
set wildignore+=log/**
set wildignore+=tmp/**
set wildignore+=*.png,*.jpg,*.gif

” ================ Scrolling ========================

“Start scrolling when we’re 8 lines away from margins
set scrolloff=8

set sidescrolloff=15
set sidescroll=1

References

http://yanpritzker.com/2012/01/20/the-cleanest-vimrc-youve-ever-seen/

That’s it.

Some Geeky Resolutions

It’s another new year. I enjoy this day because it feels like a blank slate to begin new habits, and drop old ones. We even make lists–resolutions. And so here is my list of computer-related resolutions.

1. Learn Linux Well

Modern Linux distros make it very easy to use. The fist time I installed Linux, it already had a GUI interface (KDE, GNOME, and XWindows) but the installation process was still console based, and any customization needed to be done manually in the terminal; as well, most applications needed to be compiled (I don’t remember any RPMs). I ran LILO, played with the distro a bit (Fedora, I believe) and dropped it. I regret that now. Linux is becoming ever more important as a desktop OS, while still holding on to its supremacy as a web server. For this reason, I want to learn the ins and outs of the kernel and terminal usage (not so much any X-Windows GUI).

2. Learn VIM

This of course, goes hand in hand with (1). I already started to learn this amazing text-editor in 2012, and I love it. It’s quick, powerful, and entirely customizable.

3. Learn Basics of C++

I want to have a working knowledge of C++ by the end of 2013. I have a basic grasp on the language’s syntax, but I want to become more familiar with its OOP capabilities, and have the ability to quickly adapt to any API.

4. Expand on my C# and ASP.NET Expertise

To break from the GNU theme, I also want to build on my C# and ASP.NET expertise. Mainly I want to learn more about MVC and multithreading in C#. I still think that Microsoft has a huge role in the web of the future, especially with enterprise systems. I want to be part of it. No one can convince me that there’s anything better for enterprise web applications (for the developer at least) than Microsoft. We’re spoiled rotten by the support, easily the best IDE, and awesome frameworks with built in capabilities.

5.  Become a Web UX Specialist

In addition to expanding my knowledge of java\html5\css3, I want to learn more of the theory behind UX, its implications, and its the best possible implementations. The user interface—and in turn the user’s experience–is such an important part of any application (web or otherwise) because it’s how we communicate with the application. Linux may have been a faster, more stable and cheaper alternative to WIndows and Macs, but they lost the UX war. It is only after distributions became easy to install, configure, and use through an excellent GUI, did Linux gain a larger market share in the desktop world. User interfaces are always important—even if they are not graphical. An application like VIM may be entirely devoid of a graphical UI, but it still has a UI: the commands (and how they are implemented) is extremely important.  It is one of the things that makes it a great application. The placement of every key is considered when the command line controls were programmed.

To summarize, it may seem like there’s a real dichotomy between (1,2,3) and (4,5), but I see these skills as complimentary. Having a greater knowledge and experience of lower level programming will give me a better understanding (and appreciation) of the abstraction layer I’m so very used to. And while 4 and 5 will probably contribute most to my career (since I’m already employed as an ASP.NET web app developer), knowing a whole other world can only expand my horizons.

Powershell Mirror Directories

A Custom Back-up Solution for external drives

powerShellIconRecently I re-installed windows and re-initiated my development environment. However, when it came to finding a back-up solution for data files (that is, no-system user-files) I was not satisfied with my options: Windows back-up does not support non-windows drives and external hard-drives, and the free-software out there isn’t what I’m looking for. Perhaps there’s a good software option out there, but I didn’t find what I was looking for.

I want my back-up tool to do the following:

  • Back-up the specified directories when I want it to
  • Be efficient. I.e. work quickly
  • Be aware of back-up location. I.e. external hard-drives where the drive-letter might change.
  • Little-to no overhead and extra bes

The closest thing I came to, is a PowerShell script by Mathew Painter. His excellent article and script can be found here.

Mathew’s script is excellent: it checks to ensure that the drive-letter of the external drive did not change, if it did change, it updates the back-up destination (if it still exists), and with robocopy it runs very efficiently and quickly. However, the solution lacked the first pre-requisite as it only backed-up a whole drive. For me, this is unnecessary as I’m only concerned with 2 folders on my D: drive so I endeavored to re-fashion his script.

Essentially the script mirrors a directory of your choosing into a directory of your choosing. Sounds simple, right? It is, but it’s not.

  • You could just drag and drop your files, but that’s something you cannot automate. Plus it’s not as fast as robocopy and it doesn’t truly mirror the files. I.e. directories and files that were deleted in the source location will not be deleted in the destination (back-up) location.
  • You could just use robocopy with the appropriate arguments, but then what if the letters of the external hard-drives change (when re-plugging them in a different order).

Running the Script

Here’s how this script works. When you run it for a first time, it will ask you the input details:

  1. Source Drive letter. E.g. If you are copying something for the C:\ drive, type in “C”. NOTE, do not type anything in but the letter (Not “C:” or “C:\”.
  2. Source path. E.g. If you are copying “C:\Users\username\Documents”, type in, “Users\dardawk\Documents”. Note that you need to drop the drive letter, collon and backslash.
  3. Destination letter: E.g. “F:\” would be “F”
  4. Destination path: A pre-existing path where your directory will be mirrored to. So if you have a “F:\backup” folder, you should type in, “backup” here.

An example of backing up D:\testMirror to J:\backup-mirror\testMirror:

PS-Screen01

And that’s it.

The script will generate a mirror-directories.xml file that will be used to automate the process so that the next time you run the script, no user input will be necessary (except to click continue). As well, robocopy will generate a report for you that will be saved in the source directory.

MORE…

Since I customized this script so that it works on directories, it may be necessary to have multiple copies of the script (with multiple set-up xml files). I added in this feature so that if you have multiple copies of the script with different names in the same directory (let’s say backup1.ps1, backup2.ps1, backup3.ps1, …) the script will create the following setup set-up xml files after running the first time: backup1.xml, backup2.xml, backup3.xml.

Simple as that.

After you run the backup for the first time, you can set-up run it regularly through Windows Scheduler.

Download

A warning: This script is provided as-is, and it’s recommended that only power-users utilize it. Personally, I would never run a powerShell script which code I didn’t read through at least once.

People who should not download this script:

  • If you don’t know what a PowerShell is
  • If you don’t know how to enable unblock a PowerShell script that’s downloaded from the internet (For this reason, I did not illustrate how to do this in this post)
  • If you don’t know how to change the PowerShell execution policy. (For this reason, I did not illustrate how to do this in this post)

Having said that, here is the download link:

Download Link

Reference Links

Open Menu Button

Menu