Useful Vim Mapping For duplicating word or character above the cursor

Updated: I found out that Ctrl+y in the insert mode can duplicate whatever’s above the cursor. So there’s no need to use the mapping at all.

____________________

Outdated:

imap <F1> @<Esc>kyWjPA<BS>
nmap <F1> @<Esc>kyWjPA<BS>
imap <F2> @<Esc>kvyjPA<BS>
nmap <F2> @<Esc>kvyjPA<BS>

The following mapping can help to copy the word or character above the cursor and paste it at the current cursor position by pressing F1(for word) or F2(for single character).

Example:

class User < ActiveRecord::Base
attr_accessible :email, :name
has_many :microposts
^
end

If my cursor is at the ^ location, and I want to duplicate has_many above the cursor into the current position, I press F1. Pressing F2 can duplicate h, pressing F2 again and again will duplicate a, s, _, m…etc.

How to get started with Vim

Some Background about me and Vim

Knowing that Vim is a beast that won’t allow me to type straight away, I never want to use it until I started doing embedded programming in Ubuntu, where I can only control the embedded system through telnet/RS232, I’m forced to learn Vim just because it can function in command line.

Hearing about all the good things about the Vim editor on the internet, I tried to use it as my daily editor for editing text and programming. But I quickly gave up as I realize 2 major problems that reduced my productivity significantly:

1. Too many things to learn about Vim
2. Mental focus switch to Vim instead of my on hand task

I never give Vim a second try as my daily editor after that. I only use it to edit a few words in my embedded system. Not until I change my job and met a co-worker who is using Vim and is pretty good at it. Although he’s not using most of the advanced features of Vim, he’s good enough to fly around in Vim and search files properly, split windows and get everything under control. Looking at someone that can actually use Vim so fast is really inspiring for me to try it again. This time, the change is a success! I’m not a Vim pro yet, but I can at least use it as my daily default text editor.

Looking back at why I got so fed up with Vim is due to the wrong approach I use to learn it. So I thought of making a post to address them here.


Don’t try to learn everything, just learn the bare minimum

There are too many things to learn in Vim, so we should learn the bare minimum just enough to get started without reducing our productivity greatly. Until we are comfortable enough with those basics, then we we move on confidently.

When learning something new in Vim, place them into 2 categories: must-learn, and just-know-it-exists. For example, look at this:

  • Forward word movement in Normal Mode: w, W, e and E
  • Backword word movement in Normal Mode: b, B, ge, and gE.

Please DON’T try to memorize all in one shot! Doing so will make your life miserable… So what you should do is to remember w and b, just good enough to move around. 🙂

Remember the commands in category

Our brain memorize organized data better than random ones, so I believe that remembering the commands in categories actually help. When remembering the commands like i, d, w, etc in Vim, try to place them under the category of different modes in your brain, see below to learn about the 4 modes.


My bare minimum tutorial:

*note that I use MacVim, so if you’re using a different version of Vim, things might be different.*

The 4 modes you need to know

There’re actually 4 modes in Vim: Normal, Insert, Visual and Command Line mode. There are more modes, but ignore those for a start.

  • Insert Mode – is the usual mode that we can type text into it like Notepad, TextEdit ,Gedit or etc. Press i from Normal Mode to enter this mode.
  • Visual Mode – is used for copy and paste. Press v from Normal Mode to enter this mode.
  • Normal Mode – is the common mode of Vim, when you first enter Vim, you will be in this mode. Press ‘esc’ button to go to this mode from other mode.
  • Command Line Mode – is a mode where you can type command, for example, to find a file, to search a text. I will talk about how to use it on later section.

So when you first open Vim, you usually want to go into Insert Mode by pressing ‘i’ from Normal Mode.

The Insert Mode 

Nothing much to learn for now, just use up, down, left, right, and type into it and use backspace to cancel stuff.

 The Normal Mode

In this mode, we can navigate around and manipulate the text. Remember, in this mode, whatever we typed, will not appear in Vim as we expect it to be like in Insert Mode or Notepad. In this mode, every single alphabet is a Command. The few things to remember in this mode:

  • Undo and redo – u and Ctrl+R
  • Up, Down, Left, Right – use your arrow key or (Optional:K, J, H, L)

More on navigating:

  • Move by one word – w and b
  • Deleting a word – dw or db
  • Deleting a line – dd
  • Delete a character – x

Combo-action: you can multiply an action my placing a number in front, try:

  • Up for 5 times – ‘5 <arrow-key-up>’
  • Delete 3 characters – 3x

Optional to learn for now:

  • Go to Line 13 – 13G
  • Place cursor front or back of a line – 0 or $, note that it’s a zero
  • Go to next ‘a’ found in the same line – fa, a can be any character
  • Go to previous ‘a’ found in the same line – Fa, a can be any character

The Visual Mode

If you’re using a graphical Vim, like MacVim, you can use your mouse and do a command+c or paste with command+p. Then you can forget about visual mode for now and go to the next section, if you’re interested to find out how to do it the Vim way in Visual Mode, read on.

I only use this mode for copy and paste at the moment. Remember, in this mode, your keyboard will not function like in Insert Mode or Notepad mod as well. It will function very similar to Normal Mode, except that it will highlight your text as you navigate around.

To perform a copy-paste: First, move your cursor to the place you want to copy in Normal Mode, then press ‘v’ to enter Visual Mode, then move your cursor to wherever you want to highlight, then press ‘y’. To paste, go back to Normal Mode, and press ‘p’.

Don’t forget that u can use things you learned in normal mode to navigate the highlighting! For example, start at Line 1, enter Visual Mode, and press ‘5G’, it will highlight all the way from Line one to Line 5.

So, that’s it.

Command Mode

The command mode is where you can type in the Vim Command Line. Remember, in this mode, what you type will appear under the bottom most of Vim. I use this mode to save, save and quit, to search for words and files.

Save – ‘:w’

Quit – ‘:q’

Save and Quit – ‘:wq’

Quit without saving – ‘:q!’ , this is dangerous, as you might lost content of your current file, so you need a ! behind.

Searching for a word in a file – go to Normal mode, then press ‘ / ‘, followed by ‘anyword’, that you’re looking for, then hit the Enter key, then press n to go to next matching word, and N to previous matching word.

Below is optional for now:

Searching a word in your current directory :Ack -a anyword . 

Take note of the ‘:’ and ‘.’ when you type ‘:’ you will enter the command mode, then type in ‘Ack -a anyword .’ means calling the command ‘ack’ to search for ‘anyword’ in the current directory ‘.’ with an argument ‘-a’ meaning searching for all file type.

Take a little while to learn the bare minimum, then use them until you’re very comfortable with it. When you can do it without even paying much attention, then you are probably good enough and prepared to learn something new with Vim.