Archive for Vim

Vimwiki wiki wild wild west: Tables

Vimwiki

Vimwiki is awesome and it has already done the rounds a few times. If you haven’t heard of it then check it out on the Vimwiki github repository. It’s great for note-taking, to-do lists, meeting minutes, etc. but I came across a use for it this week that I’ve already found myself using a few times since: Tables.

Vimwiki Tables

I’ve used the Tabular vim plugin on and off but I find it a bit tedious. It could well be that my workflow with it isn’t great. Vimwiki’s tables in comparison are super intuitive, to me at least. They have auto-formatting just when needed but also have the power of vim actions.

Demo

The Github repo has all the info you need to create and edit some tables but, as always, I find gifs speak louder than words:
Below, I’ve created a table with 3 columns and 2 rows with the command :VimwikiTable 3 2

Then I can start filling the table with some info about my favourite people ever:

See how it formats as I go? It even adds a new row when I tab on the last cell.

Lastly, I’ve added some incorrect info for Voltaire’s birth year. I can correct that pretty easily by using the \ text object which represents a cell. So, change in cell ci\ deletes the inner contents of the cell and puts me into insert mode, ready to write the correct year:

Too easy.

Remember, check out the Vimwiki github repository docs for more commands.

Sort yourself out! Vim sort

Quick tip

Espresso style tip today. If you have a list, you can easily sort it using Vim using :sort

Copy this into Vim and try it for yourself:
1. list
5. order
2. should
4. in
3. be

It works particularly well with visual mode. Enter visual mode (V) and select the lines above then type :sort

Lovely.

Find Yourself In Vim

Moving on

So, if you’ve used Vim a bit then “hjkl” is hopefully second nature. Moving by objects (w/W/e/etc.) makes things easier but it still lacks finesse. One of the best tools for moving in Vim is f and its variations. When we use f we’re doing a find for a single character on the current line. This allows us to jump straight to (or very close to) the point we next need to be at without using the mouse.

Example

As always, the best way to learn is to see it. In the below sentence, my cursor is on the first character on the line in normal mode (I):
It was the best of times, it was the worst of times.
Now if I wanted to jump to the word best, I could press w a few times. However, fb will find the b with way less effort. As usual, it’s mnemonic so it doesn’t require extra thought to think of the command. It just works:
It was the best of times, it was the worst of times.

F, t, T, “,” and “;”

To make finding extra powerful, here are a few bonus commands:
F – searches backwards on a line.
t – same as lower case f except it stops a character early.
T – same as T except backwards on a line.
, – find next occurrence of character (can repeat until there’s no more).
; – same as “,” except backwards on a line.

Scrabble grandmaster

Ever played Scrabble? If you have then this last tip will make your life a whole lot easier. Luckily, it’s still easy to remember if you haven’t played. Try the above commands in a vim session for a few minutes. Very quickly, you’ll see that it can sometimes be a bit tricky to nail the exact character you’re after and you might find yourself resorting to “hjkl” to get there. One way to solve this is to try to mentally note which “high value” letters are near the place you’re trying to jump to. In Scrabble these letters are the ones with higher points but in English we just mean the letters we see less often (z, x, b, y, etc.). Let me show you an example to get my point across:

Insanity: doing the same thing over and over again and axpecting different results.

Oops! I’ve made a typo. Instead of expecting I’ve written axpecting! What a numpty. My first instinct might be to use f to jump to a right? Notice that high value x right beside it though? Instead of doing 5 or 6 jumps, I can just do one. fx followed by i and backspace once to correct my mistake.

Insanity: doing the same thing over and over again and expecting different results.

Keep an eye out for those high value letters. Don’t forget that capital letters and punctuation can be found too and might be higher value.

Things Vim is fecking great at #912983772: Joining text

Joins

I love Vim. Have I said that before? I want to share another command that I use every single day. It’s extremely simple and, as is often the case with simple tools, very powerful.

J for Joins.

What it does

Simply, if I have two or more lines of text I can use J to join them together on one line. Let’s take some example text (note cursor is on the H):

Here is some
example text, spread over a
few lines.

I can join this in a few ways:
1. Execute 3J (Join 3 lines)
2. My preferred way, especially if dealing with a lot of lines, is to visually select the text first and then executing V2jJ (Visually select this line [V], go down 2 lines [2j] and Join [J]). V}J would also work a treat in this case and is essentially one less key press.
3. Do one of the above with gJ e.g. 3gJ. This actually changes the command to remove spaces which is useful occasionally. The example text would end up looking like this: Here is someexample text, spread over afew lines. That probably isn’t what you want in this case but I’m sure it’s obvious how automatically removing spaces might be useful.

Why it’s good

Like all great Vim commands, it’s mnemonic so you’ll not need to look it up once you’ve used it once or twice. Its real strength is apparent when refactoring. Combined with a good IDE (IntelliJ + IdeaVIM) and the other staple Vim commands, you’ll find that manipulating text becomes a pleasure rather than a chore.

Vim – “Change in quotes” trick

Changing quotes in Vim

One of my most used Vim (or IDEAvim in Intellij) commands when I’m editing code is ci”.
I’ve talked before about text objects in Vim like environments and even mentioned how I use c (change) on the command line in vi mode. I think the Vim verb to “change” offers a lot of power that’s lacking in other editors but I just want to talk about ci” today.

What you might not know

Hopefully if you read the last post you’ve adopted ci” in your daily usage, however you might not have realised that this also jumps to the first set of quotes on a line if you’re not currently in quotes. Example:
System.out.println("My printed statement");
Imagine your cursor is currently on the S. (Remember: ^ goes to start of line). If you do ci” then your cursor will jump to the quotes, delete the contents and enter into insert mode ready to type:
System.out.println("");

Vim text objects in zsh

Vim, not Vi, on the command line

“Vi mode” is well known about. In bash or zsh, it’s possible to move around much in the same way you would in vi (e.g. b goes back a word). What isn’t as well known, is that zsh, 5.0.8 or later, offers support for visual mode and text objects. For anyone who hasn’t yet seen the vim light, this might not mean much but read on…

Example

Say I have a command to create a new git branch but upon copy/paste I notice an error:
git checkout -b CREATURE-my-new-feature
I’m an idiot. That should say FEATURE as per my team’s branching convention. Ok, so in emacs or vi mode I can move back to the “C” in “CREATURE” easily enough using M-b or b respectively but then what? zsh’s Vim text objects allow me to make the corrections in a vim like manner:

  • ciwFEATURE rewrites CREATURE as FEATURE.
  • Even better, ctEF changes to E with an F. This makes the same amendment in a more succinct way.
  • If I wanted to rename the whole branch with something completely different then I could ciWnew-name. (Notice the capital W to change the major text object).

Shell settings

If bash is your shell of choice, then I still recommend trying vi mode for a while, even if it isn’t as fully featured as zsh’s version:
bash setting: set -o vi
zsh: bindkey -v

WTF is a text object

As usual, I’m jumping straight to the point. If you haven’t stumbled across text objects in Vim yet then I recommend opening it up and trying the command:
:help text-objects
Alternatively, there’s a great post here that explains text objects in detail!

Vim fu

Becoming a Vim master starts with being a Vim beginner

One of my tech passions is Vim and its philosophy. I’m even writing this post using it. To become better at something, whether it be martial arts, playing an instrument or programming, you need to be consciously striving to improve. The old cliché “Perfect practice makes perfect” is no less correct just because it’s overstated. Just repeating a movement or series of movements with the hope that one day you’ll wake up the best at it is not the way to do it.

I think using Vim (or an offshoot like IDEAVim in Intellij’s IDEA) is going to make for a more efficient and productive developer. I could think of a million reasons why but I’m planning on just writing a series of posts with simple Vim tips that might demonstrate my reasoning.

Basic macros

q is used to start recording a macro, followed by a designation for the macro for playing it back e.g. qa will create macro named “a”. Press q again to stop recording and @a to play the macro at any time. Why is this useful? If I have a file that has 1000 lines and I need to format each line in the same way, I can record a macro for one line (and move down a line) and finish the recording. 1000@a will play the macro a thousand times and file is done. Magic. The Vim ethos of making commands repeatable combined with macros can create some very powerful functionality. Bonus tip: @@ plays the last macro again.

More to come…

Note this post and and future ones in this series are in category “Vim”. Keep an eye out for updates. If you have no idea what this Vim thing is then have a look over at The Official Vim Site