Web development and useful information

Nathan's Blog

Configuring gVim

A friend introduced me to the excellent Vi editor. His constant talking about how great it is made me curious. I purchased Learning the Vi and Vim Editors – O’Reilly and learnt the basic commands.

My boss prefers Windows: so all our servers are Windows.
I wanted to continue using Vim so started using gVim. gVim is a modified version of the UNIX Vi editor.

Here are some things I’ve needed to configure while using gVim.

Maximising gVim Windows on file open

The default window size when opening a file in gVim is small.
To make gVim maximise windows on file open add the following to you _vimrc  file.

"start maximised
au GUIEnter * simalt ~x

Your _vimrc will be in the root folder of your Vim install, mine’s located in: C:\Program Files\Vim\

The _vimrc file contains optional runtime configuration settings to initialize Vim when it starts.
On Unix based systems, the file is named .vimrc, on Windows systems it’s _vimrc.

Removing ^M characters in gVim

^M characters are DOS/Windows line-ending characters.
Previous developers at my work used Notepad++ to edit files. When I’d open these files in gVim they were littered with ^M characters and appear without line breaks or indents. Hard to read and edit.

To remove these use the ex command

:%s/^M/\r/g

I encountered a problem typing control characters in Windows.
In a UNIX environment you type a control character using CtrlV. In Windows CtrlV is used to ‘Paste’ so you must use CtrlQ instead.
To type ^M in gVim you type (CtrlQ) <– keep your finger on Ctrl then press M.

:help CTRL-V-alternative

*CTRL-V-alternative* *CTRL-Q*
Since CTRL-V is used to paste, you can't use it to start a blockwise Visual
selection.  You can use CTRL-Q instead.  You can also use CTRL-Q in Insert
mode and Command-line mode to get the old meaning of CTRL-V.  But CTRL-Q
doesn't work for terminals when it's used for control flow.

Good explanation of ^M characters copied from here

There is a difference between how a Windows-based based OS and a Unix based OS store end-of-line markers.
Windows based operating systems – thanks to their DOS heritage – store an end-of-line as a pair of characters – 0x0A0D. Unix based operating systems just use 0x0A. ^M is a visual representation of 0x0D.

Changing the location of gVim swap and backup files

When editing a file in gVim a backup file is created after saving changes to a newly opened file.
It is a copy of the file before changing the file.  It is named the same as the file with a ~ at the end.
This creates a lot of clutter in your directories.
Thankfully you can change the default behaviour by disabling swap and backup files or moving the location of these.

To disable swap and backup files add this to your _vimrc file

set nobackup
set nowritebackup
set noswapfile

I like to keep these files as it saves the line location that I was last editing.

To change the swap and backup file location add this to your _vimrc file

"set where to store backups
set backupdir=C:\vimbackups

"set where to store swap files
set dir=C:\vimbackups

Previous

Phone Photos

Next

Dropbox – Tracking shared folder changes

5 Comments

  1. Ben

    *like*. Nice post. vim rules. There's a windows guy at my work who, when he needs to write a script for something, uses (gasp) Notepad. Srsly like zomg.

  2. Stephen

    Thanks for some great stuff here, Nathan. I started using vi in '94 when I moved to a shop with nearly every OS imaginable (well, nearly so… IBM 360/370, VAX/VMS, SunOS & Solaris, HPUx, IBM AIX, dos/netware, banyan vines.. I'll stop.. Anyway — clearly vi was the editor to learn, if not just for all the ux's — but even had it on the VAX/VMS so was worth it (this was a "sustaining engineering" role, i.e. maintenance/bug-fix). When I found VIM, which was end of '94, or '95 at the latest… it was a GODSEND. When I started building Linux in '95/'96 iirc. Now, so many years later, it's just better than ever. Even though I'm often on 'doze, I never enable Win32 stuff ftmp. In fact, I usually write/hack vim keymaps for most other applications I use very often. First thing I do on a new machine, or even a friend/neighbor/sibling's is map CAPSLOCK to CTRL (I usually put it back if it's not my machine ;-0

    Anyway – thanks for a really nice resource. I think I've been here more than a couple times by now, thought I'd holler. Cheers!, Stephen

    • n8kowald

      Hi Stephen, thanks for your great comments!

      I wish I had experience with all of the OSs you mentioned.
      Love your setting vim keymaps for other applications :)
      I've not mapped CAPSLOCK to CTRL but can see how that makes for easier shortcuts. Will set this up. The only time I use CAPSLOCK now is when I accidentally press it.

      I've also learnt a new acronym: ftmp – for the most part. Thanks!

  3. Oh my goodness! an incredible write-up dude. Appreciate it Nonetheless I’m experiencing problem with ur rss . Don’t know why Unable to register for it. Perhaps there is everyone obtaining identical rss dilemma? Anybody who knows kindly respond. Thnkx

Leave a Reply to n8kowald Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Powered by WordPress & Theme by Anders Norén