Execute command when matches a pattern in vim

There are some situations while working in vim to add/delete/replace some keywords/lines based on matched pattern. Like remove ^M from a file and we explained various methods to do so.
User may face few other situations where they can use vim commands like %s or :g efficiently.

  1. Find a pattern globally and delete the matched lines
    Ex: Delete all lines which matches “No match” in a file

[vim]:%s/pattern//g
[/vim]
Or 
[vim]:g/No match/d
[/vim]
 

  1. Delete all blank lines from a file

[vim]:g/^$/d
[/vim]

  1. Delete lines which have one or more spaces only

[vim]:g /^\s*$/d
[/vim]
or
[vim]:g!/\S/d or v/\S/d
[/vim]

  1. Delete all lines which doesn’t match the pattern
    Delete all lines except lines which have the ‘2013-10-29’ date format in the line

[vim]:g!/pattern/d
[/vim] or
[vim]:v/2013-10-29/d
[/vim]

  1. Copy all lines matching a pattern to end of file.

[vim]:g/pattern/t$
[/vim]

  1. Move all lines matching a pattern to end/top of file.

[vim]:g/pattern/m$
[/vim] or
[vim]:g/pattern/m0
[/vim]
 
Source:  http://vim.wikia.com/wiki/Power_of_g
I hope it will solve another vim users problem in their day-to-day activities.

Read more

How to remove ^M and other non-printable characters from the file in Linux

How to remove ^M and other non-printable characters from the file
If you are working or playing around with files on different O.S., and you are just copying/moving files from one O.S. based system to other. Unknowingly you are inviting non-printable character to come with! This specially happens when you open any file in Linux using any editor (vim in my case), which you got from windows based machines.  You will see ^M at the end of each line (It is usually treated as carriage return/linefeed in Linux environment which we got from its step brother Windows :p ).
Read more

Different methods to split the vim screen

Split the windows under vim effectively
When I wrote my first article on Basic commands for vim editor, then also I pointed out about it but with minimal points.
Here, I have tried to learn and share the splitting of vim screens using different methods. Thanks to internet and various resources that I came across while posting this simple article. Special thanks to StackOverflow 😀
It will surely help when you are using on multiple files or you need to compare two files or you need to edit one file at two different place and wish to see both changes blah blah!

Here are the different methods that may help you to do so:
Method 1 (using command line in Linux)

I'm assuming you are using the command line. From vim –help, we got :

-o[N]                Open N windows (default: one for each file)
-O[N]                Like -o but split vertically
So type this to open files split horizontally, for example:
vim -o3 file1.txt file2.txt file3.txt
 
Method 2 (inside vim editor)
 
Ctrl-W s for horizontal splitting
Ctrl-W v for vertical splitting
Ctrl-w q to close one

Method 3 (inside vim editor)

  1. :sp filename (horizontal)
  2. :vsp filename (vertical)

Method 4 (inside vim editor)

  • vsplit and :vs splits the vim viewport
    :30vs splits the viewport and creates the current window to be 30 characters wide
  • For vertical screens: CTRL+W+ > or  CTRL+W +< (don’t type + while using it. It’s just to tell you that you need to press ctrl key, W (case-insensitive) and > altogether 😀 ) to make wider or narrower respectively.
  • And Ctrl+W+ = will make them equals
  • For  horizontal windows : Ctrl+W + increases the number of lines by one
  • :h for the list of all window commands (help command)

CTRL-W H           move current window to the far left
CTRL-W J           move current window to the very bottom
CTRL-W K           move current window to the very top
CTRL-W L           move current window to the far right

I hope it will help the developer in using vim screen effectively. Comments and critics are most welcome \m/

Read more

How to use multiple files efficiently in vim editor

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I prefer using vim editor.

After doing some experiments and knowing some important commands, I though to share with you. So i have written few commands point wise which may be useful for you while editing multiple files using vim (not GVIM, although these commands are valid there too.)

Read more

Basic useful vim commands for everyone

I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands.
This post has only most commonly used vim commands which we use in our day today development activities. This post will be very helpful for those who wish to learn vim editor from the scratch and it can be useful for all other vim users too.

So, first open the file by using vim filename (single file at one time). I will be posting very soon about manipulation on multiple files using vim editor.

Read more