Posts Tagged ‘vim’

Suspend a VI session for shell

Tuesday, November 4th, 2008

Ever want to run some shell commands while doing a vi edit session? Like, “what’s the name of that file” I want to use in this script I’m making?

Instead of exiting vi; doing the ls filenamepart*, and going back into vi, you can from a vi session:

enter “:”, then enter “shell”.

This suspends the vi session and gives you a command prompt where you can use any shell command. When finished, enter the “exit” command and you will be returned to the vi session.

How to go a particular line or word in vi

Tuesday, September 16th, 2008

You can go to a particular line or word in a file using vi in several ways:

  • To make vi start at a particular line in a file, add +line_num to the command you use to start vi. Replace line_num with the line number, for example: vi +36 foo.c
  • If you’re already in vi, you can use the goto command. To do this, press Esc, type the line number, and then press Shift-g . If you press Esc and then Shift-g without specifying a line number, it will take you to the last line in the file.
  • You can also use the ex command line to go to a line. For instance, if you wanted to go to line 14, you could press Esc and then enter: :14
  • To search forward for some text, use the / (forward slash) command. Press Esc and then enter /pattern , replacing pattern with the text for which you want to search forward. For example, to find every instance of the word “blimp”, enter: /blimp To look for the next occurrence after the first, either press n or press / again and then press Enter. To go back to a previous occurrence, press Shift-n . To search backwards in a file, use the ? command.
  • To see what line you’re on at any time, press Ctrl-Shift-g . To number all lines, press Esc and enter: :set number

Splitting Vim’s viewport

Sunday, August 31st, 2008

A really useful feature in Vim is the ability to split the viewable area between one or more files, or just to split the window to view two bits of the same file more easily. The Vim documentation refers to this as a viewport or window, interchangeably.

You may already be familiar with this feature if you’ve ever used Vim’s help feature by using :help topic or pressing the F1 key. When you enter help, Vim splits the viewport and opens the help documentation in the top viewport, leaving your document open in the bottom viewport.

(more…)

Turn syntax highlighting on/off in vim

Saturday, August 16th, 2008

Open file (for example file.c):
$ vi file.c

Now press ESC key, type : syntax on
:syntax on

To turn it back off, press ESC key, type : syntax off
:syntax off

You can edit ~/.vimrc file and add command syntax on to it so that next you will start vim with color syntax highlighting option
$ cd
$ vi .vimrc

Append the following line:
syntax on
Save and close the file.

Search and Replace in Vi/Vim

Wednesday, June 25th, 2008

In Vi or Vim, use the forward slash </> to search. Then type in your search string and hit <Enter>. You can navigate through occurrences of your search string using <n> to move forward and <N> to move backwards.

(more…)

Pages: Prev 1 2 3 Next