MoonPoint Support Logo

 


Shop Amazon Warehouse Deals - Deep Discounts on Open-box and Used ProductsAmazon Warehouse Deals



Advanced Search
December
Sun Mon Tue Wed Thu Fri Sat
         
18 19 20 21 22 23
24 25 26 27 28 29 30
31            
2017
Months
Dec


Fri, Nov 17, 2017 9:25 pm

Removing all lines containing a string in vi

To remove all lines containing a particular string in the vi or Vim text editors, you can use the g command to globally search for the specified string and then, by putting a "d" at the end of the command line, specify that you want all lines containing the specified string deleted. E.g., If I wanted to remove all lines containing the string "dog", I could use the following command.

:g/dog/d

That command would also remove any lines containing "dogs", "dogged", etc. If I just wanted to remove lines containing "dog", I could use :g/dog /d.

You can, of course, specify the pattern on which you wish to search using regular expressions. E.g., if I wanted to remove any lines containing either "dog" or "hog", I could use the command below.

g/[dh]og/d

By putting the leters "d" and "h" within brackets, I indicate to vi that it should remove any line that has either a "d" or an "h" followed by "og".

[ More Info ]

[/software/editors/vi] permanent link

Tue, Jun 06, 2017 9:22 pm

Removing whitespace from lines in Vi

To remove whitespace characters, such as spaces and/or tabs from a line while editing a file in the vi and Vim text editor you can use a regular expression (regexp) that incorporates \s (lowercase letter "s"), which respresents a white space character. E.g., supposing the lines below appear in a file:

450 SN/GN Tech  Edit Delete
ACE     Edit Delete
ADO             Edit Delete
AGO     Edit Delete
AGS     Edit Delete
AIM     Edit Delete
ASF         Edit Delete

I could hit the colon key and type s/\s*Edit Delete// to delete the white space after the project name that appears at the beginning of the line and the "Edit Delete" at the end of the line, so that only the project name remains. To perform the substitution for all lines, I could use 1,$ s/\s*Edit Delete//.

The 1,$ represents every line from the 1st to the last and 1,$ s/old_pattern/new_pattern/ would indicate that a substitution is to be performed on every line with new_pattern replacing old_pattern on each line. The \s represents any whitespace character and the asterisk after it indicates to look for zero or more occurrences of any whitespace character. So s/\s*Edit Delete// indicates to delete white space characters followed by the words Edit Delete.

I could also use \W, instead; \W represents any non-alphanumeric character.

Related articles:

  1. gVim Portable for Windows

Reference:

  1. Lesson 9: All this whitespace
    RegexOne - Learn Regular Expressions with simple, interactive exercises.

[/software/editors/vi] permanent link

Sat, Feb 25, 2017 10:48 pm

Determining the differences between the current version and a vi swap file

When I attempted to edit a file, index.php, using the vi editor, I saw the following message:

E325: ATTENTION
Found a swap file by the name ".index.php.swp"
          owned by: joe   dated: Mon Feb 20 19:36:11 2017
         file name: ~joe/www/UVNC/index.php
          modified: YES
         user name: joe   host name: example.com
        process ID: 19776
While opening file "index.php"
             dated: Mon Feb 20 19:38:44 2017
      NEWER than swap file!

(1) Another program may be editing the same file.  If this is the case,
    be careful not to end up with two different instances of the same
    file when making changes.  Quit, or continue with caution.
(2) An edit session for this file crashed.
    If this is the case, use ":recover" or "vim -r index.php"
    to recover the changes (see ":help recovery").
    If you did this already, delete the swap file ".index.php.swp"
    to avoid this message.

Swap file ".index.php.swp" already exists!
[O]pen Read-Only, (E)dit anyway, (R)ecover, (D)elete it, (Q)uit, (A)bort:

I hit the q key to return to the Bash shell prompt. When I compared the time stamps on the current version of the file and the .swp file, I saw that the current version had a time stamp 2 minutes after the time stamp for the .swp file.

$ ls -al | grep 'index.php'
-rw-rw-r--. 1 joe joe   7571 Feb 20 19:38 index.php
-rw-r--r--. 1 joe joe  20480 Feb 20 19:36 .index.php.swp
$

[ More Info ]

[/software/editors/vi] permanent link

Fri, Jan 13, 2017 10:27 pm

Deleting up to a word or back to a word in the Vi or Vim editor

In the vi or Vim text editor, you can delete all characters on a line up to a specified word by placing the cursor on the line at the point where you wish to start the deletion then hit the d key followed by the slash key followed by the word up to which you wish to remove the characters on the line. E.g., suppose you have the following line:

If you can force your heart and nerve and sinew To serve your turn long after they are gone, And so hold on when there is nothing in you Except the Will which says to them: “Hold on”;

From the poem If— by Rudyard Kipling (1865-1936)

If you wished to delete all of the text on the line from the word "To" up until, but not including the word "And" in "And so hold on...", while in command mode, not insert mode, you could move the cursor to the "T" in "To" and then hit the d key followed by the forward slash key (/) and then type And (make sure you use the matching capitalization). The line would then appear as shown below.

If you can force your heart and nerve and sinew And so hold on when there is nothing in you Except the Will which says to them: “Hold on”;

Suppose, instead, you had the cursor at the word "To" as before, but wanted to delete backwards through the word "If", i.e., all the way to the beginning of the line, instead. You could then hit the d key while in command mode, then hit the question mark (?) key and type If. You would then have the text below.

To serve your turn long after they are gone, And so hold on when there is nothing in you Except the Will which says to them: “Hold on”;

For the backwards deletion, the word you type after the question mark is included in the deletion. In this case, if you wished to delete backwards to the beginning of the line, you could also have hit the d key followed by the ? key and then hit the caret (^) key, which represents the beginning of the line. Likewise, you could hit the dollar sign ($) key to delete forward to the end of the line, which it represents, or you could just hit the D key, instead of the lower-case "d" to delete from the current cursor position to the end of the line.

[/software/editors/vi] permanent link

Mon, Aug 22, 2016 11:45 pm

Substituting characters for a matched regular expression in vi

The vi editor is a screen-oriented text editor that supports regular expressions for pattern matching and character substitution. Vim which stands for "Vi IMproved" is a clone of vi and recognizes similar commands.

If you want to replace or insert characters at the beginning of a line, the line beginning is represented by the caret character, i.e., ^ (Shift-6 on a standard computer keyboard), and line endings are represented by the dollar sign character, i.e., $.

E.g., the following quote has three occurences of the word "us". If I wanted to replace only the "us" at the end of the line with "ourselves", I could hit the colon key while not in vi's insert mode, which would give me a colon prompt and then enter s /us$/ourselves/ which would replace the occurence of "us" at the end of the lne with "ourselves".

What lies behind us and what lies before us are tiny compared to what lies within us

~ Ralph Waldo Emerson

[ More Info ]

[/software/editors/vi] permanent link

Wed, Dec 09, 2015 11:43 pm

Wrapping text and viewing the current line and column in the vi editor

If you use the vi or Vim text editors and wish to have lines automatically wrap to the next line when you get close to the margin, i.e., the 80th column, rather than having to hit the Return key, you can use the wrapmargin parameter, e.g., wrapmargin=10. To enter the command, while not in input mode type :wrapmargin= followed by the number you wish to use for the wrap margin.

The wrapmargin parameter is set to an integer number of characters. When set to any nonzero number, vi will watch the lines that you type in the input modes. When you get to within the "wraprmargin" number of characters of the 80th character on the line, the next space character that you type will cause vi to enter a carriage return for you. This parameter gives vi a word wrap feature for entering text data. When set to zero, the wrap margin feature is disabled.

Source: Unix for Application Developers by William A. Parrette ©1991, page 320

If you wish to have vi display the current line and column number as you type, use :set ruler. If it is turned on you will see the current line number and column number displayed at the bottom of the vi window, e.g., 24,67 if you were on line 24 at column 67. The numbers will change as you move the cursor whether you are in input mode or not. You can turn it off with :set noruler.

Alternatively, you can also use control-G on a Mac OS X system or Ctrl-G on a Linux system or with Vim on a Microsoft Windows system, i.e., hit the control, or Ctrl, and G keys simultaneously to see the current line and column numbers. You will see something like the following displayed at the bottom of the window:

"temp.txt" [Modified] line 30 of 34 --88%-- col 32

In the above example, while editing the file temp.txt the cursor was on line 30 of the 34 lines in the file and was at column 32 when I hit control-G. Since 30 is 88% of 34, the cursor was at a point 88 percent of the way through the file. Each time you hit control-G, the status information will be displayed for the current line and column when you hit the key combination.

With vi on a Mac OS X or Linux system, you can also use !}fmt when not in input mode, to automatically wrap the text in a paragraph so that a line doesn't exceed column 80 in that paragraph from the point where you enter that command.

[/software/editors/vi] permanent link

Wed, Oct 09, 2013 10:39 pm

Inserting text at the beginning and end of lines with vi's regexp

The vi and Vim text editors support the use of regular expressions (abbreviated regex or regexp) for editing files.

In a regular expression ^ signifies the beginning of a line and $ specifies the end of a line. To have an operation apply to multiple lines at once, you can specify a range of lines, e.g. 16,25. To have the operation apply starting with the current line, you can use a period, ., to represent the current line, so .,25 would mean apply the operation to the current line and all subsequent lines up to and including line 25. Or you could specify that the operation should be applied from the current line to the end of the file by using $ to represent the last line in the file, e.g., .,$.

Or you can have an operation apply to every line in a file using %. E.g., the following line would insert 123 at the beginning of every line in a file.

:% s/^/123/

The s indicates a substitute operation will follow and the forward slashes, / delineate the pattern to be replaced and the replacement pattern. In the above case, the ^ indicates the beginning of the line and the replacement pattern is 123.

When you are using / to delineate the patterns, it has a special meaning and if you want to use it in a pattern you have to "escape" its special meaning with an escape character, which is the backslash, \.

E.g. to insert </td> at the end of every line from the current line to line 25, the following regular expression could be used:

.,25 s/$/<\/td>/

Supposing that I wanted to put a <td> at the beginning of each line and a </td> at the end of each line from the current line through line 25. In that case I need to store the characters in between the beginning and end of a line. You can specify text to be stored by using parentheses, ( to mark the beginning of the area on the line to be stored and ) to mark the end. Since a period, ., represents any character and an asterisk, * represents multiple occurrences, ^(.*)$ would store all the characters on the line between the beginning and end of a line. You must also "escape" the ( and ) with the backslash escape character as well. To reuse the characters you have stored you recall them with \1 ("1" is the number one). If you had multiple occurrences of characters enclosed in parentheses, the second instance could be recalled with \2.

The line below would insert a <td> at the beginning of lines 16 through 25 and a </td> at the end of the lines.

:16,25 s/^\(.*\)$/<td>\1<\/td>

If you wished to have the editor prompt you as to whether you wanted the change made on a line, you could add a /c option at the end of the command to the editor. The command below would perform the same action as the one above on every line from line 16 to the end of the file, but would prompt you at each line as to whether the change should be made.

:.,$ s/^\(.*\)$/<td>\1<\/td>/c

You would see each line highlighted one by one as you progressed through the file with the following prompt each time.

replace with <td>\1<\/td> (y/n/a/q/l/^E/^Y)?

Answering y would result in the replacement occurring whereas answering n would result in the line remaining unchanged.

In this case, there is also a simpler means for inserting the table td tags at the beginning and end of each line as below:

s/.*/<td>&<\/td>

Again the .* represents the search pattern, which is every character on the line. The ampersand, &, also has a special meaning; it is the text that matches the search pattern, so putting it between the two tags results in all the text originally on the line remaining on the line. The / at the end of the replacement pattern can be eliminated if there is nothing following it such as a /c.

References:

  1. Adding characters at the start and end of each line in a file
    May 2, 2012
    Stack Overflow
  2. Search and replace
    Vim Tips Wiki

[/software/editors/vi] permanent link

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:39 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

Once You Know, You Newegg AliExpress by Alibaba.com

Shop Amazon Local - Subscribe to Deals in Your Neighborhood

Valid HTML 4.01 Transitional

Privacy Policy   Contact

Blosxom logo