Vi (or Vim in it’s modern incarnation) is a text editor. It’s a modal text editor. You can switch from insert mode to normal mode. Insert mode is where you type text into your document, and normal mode is where you type commands that do all sorts of functions. It’s incredibly powerful, incredibly complicated and even exiting the editor is not obvious. 🙂
So it’s always great when I dive into learning something new about Vim. There’s always something new to learn about Vim, even after more than twenty years of using it. I think there should be a Duolingo for Vim.
So, when you’re watched that, and watched it again, and again to understand it, have a look at the following presentation showing off the power of Vim without plugins.
When you just want to use Vim faster …
The power of Vim is in the commands. That also means it requires practice to learn those commands because they’re usually keyboard shortcuts. There’s no Edit menu to remind you to use something you just learned. With just a few core concepts and memorized keys or functions you can be very efficient at moving around or manipulating text.
Where X is the population of developers who read this blog, Y is those that use Vim and Z is those that use vimdiff regularly. I guess this post will only be useful to a tiny minority of my readers, but to them it might be the best thing they’ve read all year. (Well, it is 2016, right? It’s been a weird year.)
Vimdiff allows you to open two files in Vim and side by side compare them, pushing changes from one file to another. I’ve been using it as long as I’ve been working on b2/WordPress and even before then too. It’s supremely useful.
Over the years I’ve used many different terminals, with various settings and colour configurations. My vim settings change over time too as I move from one machine to another. Sometimes the colours look ok in Vimdiff, sometimes they don’t. Sometimes the colours are ok for one file type while conflicting in others.
The problem is that Vimdiff has it’s own colours it uses to show what parts of the files are different or missing. Those colours can sometimes hide actual text in the files. I find myself highlighting those lines with SHIFT-V to see the text.
I could pick a different colour scheme but then there’s no guarantee that a different part of text will be hidden by Vimdiff’s colour scheme. The easiest way to fix this is by disabling syntax highlighting completely when in Vimdiff and you do it like this. Open up your ~/.vimrc and add these lines:
With that in there Vimdiff goes from looking like this to the simplified appearance below.
Ironically, the theme I’m currently using in Vim in the screenshots above isn’t that problematic, but here are two screenshots that show the problem from another machine. In the second screenshot I have highlighted (with SHIFT-V) the line with the function name in the left side. As you can see, the text “function” is still invisible in the right side of the screenshot.
If you don’t want to edit your .vimrc for whatever reason you can also manually do :set syntax=off from within the editor but you’ll have to do that for each of your files.
All the code above is GPLed WordPress code. Thanks to user hildred on Stackexchange for that one. Hopefully someone else will find this useful.
A contributor to the Hackaday blog has a good old rant about how Vim is superior to Emacs.
Of course it is (a silly argument), but he manages to give a quick overview of Vim and describes a few neat tricks beginners will find useful!
And after writing the text above I realised that there are going to be people reading this who have absolutely no idea what either Vim or Emacs are! They’re text editors, and they have passionate users. Yeah, that includes me too. 🙂
The Bash command line can be edited using the cursor keys but for the real power user you need to enable Vi mode:
$ set -o vi
Or add it to one of your Bash startup files.
Now, instead of the slow interactive editing you’ll get the command and insert mode of Vi! Users of Vi or Vim will feel right at home. You start in insert mode by default so it feels the same as before. You can type new text, move left and right with the cursor keys and delete text but press ESC and you can do all the things Vi command mode allows you to do.
I mentioned in a tweet recently that I’ve been using some form of Vi for about twenty years. It all started in college where we had highly advanced green screen monitors attached to a large Unix box. I can’t remember what Unix it was ran on that machine (it may have been UnixWare) but it was a far cry from the nice GNU interface we’re used to on modern Unix systems. Vim certainly was not a part of the default install.
However, Vim has been my editor of choice all my working life. All this time I’ve known I’m only scratching the surface of it’s functionality but only recently has it become clear how much. I can navigate through it with ease, open numerous files in separate splits, search/replace and of course vimdiff was partly responsible for every single WordPress MU release as I used it to pull over changes from WordPress.
So, thanks to /r/vim I discovered the following today:
snipMate.vim is a snippet plugin for Vim based on the snippets in TextMate. Around the turn of the century I had messed with abbreviations but this is way better. Found that here where there’s plenty more tips to read.
Coming home to Vim is the story of the return of a TextMate user to Vim. Why didn’t I know about daX and diX?
Since I use split files, I’m always tapping CTRL-w w or CTRL-w UP/DOWN to switch between splits. It never occurred to me that I could map the TAB like this to switch split files. TAB switches to the next split file, SHIFT-TAB hops back.
map <Tab> <C-W>w
map <S-Tab> <C-W>p
I am tentatively mapping ; to : with nnoremap ; : but I probably won’t use it. My fingers are too used to LSHIFT-; to stop now. I’ve never used the ; command, I had to look it up to see what it did!
I’m a big fan of SSH and Screen. SSH to connect to a remote server, and Screen is like a workspace organiser for your SSH session.
First of all the backspace key worked in Vim even when not in insert mode, but worst of all, the cursor keys refused to work and only succeeded in ringing bells in the terminal. Previous attempts at fixing the problem failed but I must have searched for the right terms this time. This review of Mac OS X led me part of the way. Sébastien recommended setting the TERM to “dtterm” but Screen didn’t recognise that. Setting it to “linux” fixes my SSH sessions, and also fixes Vim locally as well!
I added the snippet of code above to my ~/.bash_profile to make the change permanent and everything is back to normal now! I have a vague memory of fixing this before but I wish now I had blogged it then!