MoonPoint Support Logo

Geeks.com - Free Shipping



Advanced Search
February
Sun Mon Tue Wed Thu Fri Sat
     
8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29      
2012
Months
FebMar
Apr May Jun
Jul Aug Sep
Oct Nov Dec


Sat, Jun 27, 2009 12:04 pm

Reformatting a Paragraph in Vi

I often want to reformat a paragraph in Vi or Vim after I've pasted information into a document when the text is wrapping around rather than having line breaks at column 80. On a Linux system, I can place the cursor at the beginning of the paragraph and use !}fmt to reformat the paragraph. But that doesn't work when I'm using Vim on Windows. But I can use gq at the beginning of the paragraph on a Windows or Linux system to reformat a paragraph. When I then use the downward arrow key to move down in the document, the paragraph reformats.

References:

  1. reformatting
    vim tips and tricks

[/software/editors/vi] permanent link

Tue, Apr 14, 2009 9:37 pm

Inserting a Newline Character Using Vi

To insert a newline character, i.e. to create a new line in a file using Vi on a Unix system, you can use ^M (you have to actually type Ctrl-V (i.e. the Ctrl and V keys hit simultaneously) followed by Enter to get the ^M to appear. For example, suppose that instead of commas separating elements in a list you wish to put each element on a new line.

Original lines

Gold, Silver, Bronze

Desired lines

Gold Silver Bronze

You can use the following command in Vi to replace all commas with the newline character starting from the first line in the file to the last (represented by $):

1,$ s/,/^M/g

As noted above, you need to hit Ctrl-V Enter to put the ^M in the command.

References:

  1. How to represent a new line character in a regex in VI?
    By: mbrooks
    Date: December 30, 2006
    Tek-Tips

[/software/editors/vi] permanent link

Sun, Apr 12, 2009 7:22 pm

How to Stop Vim from AutoIndenting in Files

I use Vim for editing files on Windows systems. I edit HTML files with it, but find its habit of automatically indenting lines in those files based on the tags used annoying rather than helpful. Fortunately, that behavior can be turned off. To so so, edit the _vimrc file, which is in the directory where you installed Vim, e.g. C:\Program Files\Vim. For HTML files, i.e. any file with an extension of .htm or .html, you can add the following two lines to stop the autoindentation. Close Vim, add the lines, then reopen Vim and you should no longer have the autoindenation in those files.

autocmd BufEnter *.html setlocal indentexpr=
autocmd BufEnter *.htm setlocal indentexpr=

You can enter similar lines to stop autoindentation in other files.

References:

  1. How to stop auto indenting
    Vim Tips Wiki

[/software/editors/vi] permanent link

Tue, Feb 03, 2009 7:39 pm

Recovering Vim File in Windows

I had been working on a file using the Vim editor on my laptop. I went to eat dinner; when I came back the laptop had powered itself off. I hadn't saved the file even once. One of the reasons I use Vim as my editor on Windows is that, unlike with the Windows Notepad program, if the system crashes I can recover the file I was editing, since Vim periodically updates a "swap" file for documents being edited. For instance, if I'm editing somedoc.txt there will be a somedoc.txt.swp in the same directory from which I can recover the document should the system crash. In this case, though, I didn't think I could recover the document, since I had never saved it with a file name. So I didn't know where a .swp file would have been stored and was doubtful there was one. But after powering the laptop back on and logging into the same account again, I opened Vim, hit the Esc key and typed :recover. Lo and behold there was my work exactly as it was when I went to eat dinner, saving me a lot of time I would otherwise have been forced to spend recreating the document. I immediately saved it to a file.

[/software/editors/vi] permanent link

Wed, Sep 03, 2008 6:27 pm

Remembering Text for a Regexp Replacement in Vi

I needed to insert a space between months and years in text in a document while using Vim, a version of the Vi editor for Windows systems. The text was as shown below:
December1999 Edition
November1999 Edition
October1999 Edition
...
March1996 Edition
February1996 Edition
January1996 Edition
With Vi, regular expressions can be used to search for and replace text. In this case I could use :.,$ s/199\(\d) Edition/ 199\1/ to perform the substitution.

To search from the line I was on to the end of the document I can use .,$. With the substitute s command, you can search and replace text with commands of the form s/old text/new text. You can use the i option, if you don't want the case of letters to be considered, i.e. if you wish "A" and "a" to be treated the same, then you can use s/old text/new text/i. You can use the g option, if you wish to replace all occurrences of old text on the line, for cases where the text may occur multiple times on the same line, e.g. s/old text/new text/g. You can use whatever delimiter you wish to separate the parts of the command, e.g. you can use s:old text:new text:.

The \d in the command indicates that I am only looking for digits, i.e. 0 to 9. By enclosing the \d in parentheses, i.e. by using (\d), I can have the editor "remember" whatever it found between the parentheses. Then I can have it insert what it has remembered in the replacement text by using \1. If I had used multiple parentheses at various parts in the search text, then the second string I wanted remembered would be indicated with a \2. In this case the last digit of the year was all I wanted the editor to remember and insert appropriately in the substitutiong text.

If you wish to search an entire document, you can use 1,$ to represent the first line of the file through the last line, or you can just use % to represent the entire file.

:% s/199\(\d) Edition/ 199\1/

References:

  1. Vim Regular Expressions - Substitute Command

[/software/editors/vi] permanent link

Fri, May 16, 2008 4:19 pm

Displaying Line Numbers in Vi

To turn on the display of line numbers in the vi editor, use the following command:

:set number

To turn off the dispaly of line numbers, use the command below:

:set nonumber

[/software/editors/vi] permanent link

Fri, May 16, 2008 3:20 pm

Delete Lines Containing or Not Containing a String Using Vi

To delete all lines containing a string or all lines not containing a particular string, you can use the global search options in the Vi editor.

Global Search

:g/string/command
command affects lines containing string
:v/string/command
command affects lines not containing string

To delete all lines containing "foo" you could use the following command:

:g/foo/d

To delete all lines not containing "foo" you could use the following command:

:v/foo/d

References:

  1. vi Reference Card
    JILA

[/software/editors/vi] permanent link

Sun, Feb 04, 2007 8:04 pm

Vi Reference

I found a useful vi reference page at http://www.ungerhu.com/jxh/vi.html, which I have copied here. The page was created by Maarten Litmaath and is maintained by James Hu. I found it a useful reference for substitution patterns, though it also has a lot of other vi information.

If you want to replace "Life's but a walking" with "Life's but a walking shadow" you can use :s/Life's but a walking/& shadow/. The ampersand, in the pattern to be substituted in place of the prior one, references the previously found match.

[/software/editors/vi] permanent link

CompuVest - Notebooks

Valid HTML 4.01 Transitional

Privacy Policy   Contact

Blosxom logo