How to Record and Repeat Key Sequences in Vim

When using Vim, I find it very handy to record and automatically repeat key sequences over multiple lines. E.g. suppose you want to extract the content from HTML markup, you could use this technique to record how you extract the text from the first line (in a way that can be repeated throughout), then repeat your actions over all the other lines. Such a recording is known as a macro and here is how to use it.

First, we want to store our macro in a register, let’s say we want to store it in 1. We start recording by clicking q1. On the status bar, the text recording @1 should appear indicating that your actions are now being recorded.

Then we perform our key sequence on the first line.

Once done, press q. The recording @1 text on the status bar should disappear.

Now, to repeat the actions saved on another line, move your cursor to that line and press @1.

If you want to perform the actions over, say the next 10 lines, then simply press 10@1.

References

Macros. http://vim.wikia.com/wiki/VimTip398

How to Edit Multiple files with VIM

This tutorial is a simple technique for editing multiple files in VIM which I stumbled upon very recently.

Suppose you have 3 files you want to edit at a go, you can simply run

$ vim file1.txt file2.txt file3.txt

If you want to edit all the files in a directory, you can cd to that directory and run

$ vim *

While editing the files, you can switch to the next file using the command :next of :n. :wnext can be used to write changes and move to the next file in one go.

To move to previous file run :previous . To save and move to previous file in one go run :wprevious.

To see where you are in the list of files open, run :args

gVim

gVim is a powerful editor that makes a lot of editing work a piece of cake. Here’s a cheat sheet that can help you get the most off this editor. For those who would like to get the work done without using a mouse, it may be the perfect editor. This cheat sheet is geared towards beginners who use editors a lot, perhaps programmers, and will like an easy introduction.

First, for those from a Windows background, there are 3 modes of working in gVim, command mode, insert mode, and visual mode. On opening gVim, you start off in command mode. To type in text, you must go to insert mode. To select text by highlighting, as you do in Windows, you go to visual mode. Without further ado, to the commands.

Opening, saving, moving between files, and closing files

  • e c:\path\to\your\file  – opens the file, or creates one at that path file doesn’t exist. If you use this on a directory, it opens the directory while showing error message on command bar
  • tabnew c:\path\to\file – opens (creates) file at specified path in a new tab
  • w – save file
  • sav c:\path\to\file – save file as the name given
  • q – close file
  • wq – save file and close it
  • q! – close file without saving (gVim normally issues an error if you try ‘q’ without saving last changes)
  • ctrl pg up – go to previous tab
  • ctrl pg dn – go to next tab

Switching between differnt modes

Switching from command to insert mode

  • i – places the cursor behind it’s current position.
  • a – places cursor after current position

Switching from insert to command mode or from visual mode to command mode

  • esc

Switching from command to visual mode

  • v – to selct text
  • shift+v  – to select line

Moving around documents

  • k – moves up a line
  • j  – moves down a line
  • h – moves left on current line
  • l – moves right on current line
  • w – moving to beginning of words in forward direction
  • e – move to end of words in forward direction
  • b – move to beginning of words in backwards direction
  • gg – move to first line of file
  • G – move to last line of file. Same with :$
  • ^ – move to beginning of line
  • $ – move to end of line.
  • :## – move to line ##

Moving accross screen

  • pg up – moves to previous screen
  • pg dn – moves to next screen

Copying and pasting

  • yy – copies (yanks) current line
  • 3y – this example with yank 3 lines starting from current line
  • P – paste copied text before cursor
  • p  – paste copied text after cursor.
  • “+y – copies to clipboard (making content available to other apps)
  • “+gP – pastes from clipboard. eg from your web browser into gVim

Inserting and Replacing text

  • O – creates(opens) a blank line before cursor and goes to insert mode
  • o – creates(opens) a blank line after cursor and goes to insert mode
  • ggVG – select everthing in current document
  • cw – change current word (deletes current word and puts you in insert mode)
  • ci{ – deletes everything within the nearest matching braces and puts you in insert mode. eg. if you had a function – function x(){ /* old st */ } and you want to quickly change the entire implementation of that function, you move your cursor into that {} and use ci{. Note: ci} accomplishes same task.

Deleting

  • dd – cuts current line into gVim’s clipboard (making content available only within gVim for paste)
  • 3d – example that deletes 3 lines starting from current line

Indenting

  • shift v > – adds indent on current line
  • shift v < – removes one indent from current line

Copy and Paste between gVim and other applications

  • “+y – copies to clipboard
  • “+gP – pastes from clipboard

Searching and Replacing

  • /searchpattern – searches for text with patter ‘searchpatter’
  • rs/searchpattern/replacepattern/o – this does a search and replace where r and s are options of various values. r can be nothing, in which case only current line is searched, % – in which case whole file is searched, a-b (where a and b are line numbers) in which case the range defined by a and b are worked on.   o stands for a series of one or more options (g, c, i,l). g- means all occurrences on lines are worked on. c- means you are asked for confirmation before each replace. i- case insensitive search, l-case sensivitive search

That’s all for now.  Happy gViming.