Recent Posts

VIM

(Nerd) ^M Removing control characters using VIM

hat tip Matthew Turland

Have you ever opened up a file using VI and found the following?

<?php
    echo '<html>'^M
    echo '<head>'^M
    echo '</head>'^M
    echo '<body>'^M
 
...

That stupid control character from some other horrid text editor is trailing every single line. And you are annoyed enough to Google for an answer. ^M

Now, hopefully you are aware of the powerful regex tools built right into VI. But in this case, the fix is pretty simple, and we won’t have to craft any regFu. After you open your file using VI, we will then use the search/replace functionality. And we will eventually end up with a line that looks like this.

:%s/^M//g

But there is a gotcha to this solution, so you may need to read on to find out more. And before proceeding, press esc to make sure you are not in insert mode.

First type the following.

 Shift + :

Which should just print the colon at the bottom of your screen.

:

Then type this exactly how it looks.

%s/

Now we need to type the control character. You may be thinking “Jared, I will just hit shift + 6 and get that character onto my screen in no time.”. And you are more than welcome to do that, but you would be wrong.

You will need to press the following keys.

Control + V

Which will get you this printed out.

:%s/^

Then type the following.

Control + M

Which should have you ending up with this.

:%s/^M

At this point you can type the rest of the characters exactly how they look.

//g

Which leaves you with a finished line that looks like so.

:%s/^M//g

Press enter, and you will now replace the ^M character with nothing.

Part 2(kinda): Small search and replace example.

This would replace Dog with Cat.

:%s/Dog/Cat/g

Cat with Mouse.

:%s/Cat/Mouse/g

Mouse with Trap.

:%s/Mouse/Trap/g

So our line to replace the ^M character is literally saying “Replace the ^M character with nothing”.

(Nerd) Ubuntu + Vim + ZendFramework-1.9.2 + .pthml syntax highlighting

1) From the command line ‘cd’ to your ‘home’ directory


test-box@jbuntu:~$ cd

2a) Check to see if the .vimrc file exists


test-box@jbuntu:~$ ls .vimrc

If the terminal outputs nothing, then that means the file does not exist.

2b) If you get the following


test-box@jbuntu:~$ ls .vimrc
.vimrc

It means that the file does exist and we just need to edit it.

3) If the file does not exist just ‘touch’ the file. If it DOES exist, just skip this step.


test-box@jbuntu:~$ touch .vimrc

4) From this point, ‘vi’ the ‘.vimrc’ file. You primarily need the following lines and you are more than welcome to copy/paste. Write/Quite when finished.


if has("autocmd")
autocmd BufEnter *.phtml set syn=php
endif
syn on

Now the next time you open VI it should have the desired highlighting for .phtml files found in the Zend Framework.

Below is my ‘.vimrc’ file in it’s entirety. Just for the record. It also allows for syntax highlighting to occur in CakePHP .ctp files along with some other settings that I prefer.


set tabstop=2
set shiftwidth=2
set expandtab
if has("autocmd")
autocmd BufEnter *.ctp set syn=php
autocmd BufEnter *.phtml set syn=php
endif
syn on
set ai

Ubuntu + VIM + CakePHP 1.2 + .ctp syntax highlighting

Found this blog post concerning setting up the Cakephp view extension (.ctp) to use the same highlighting as (.php).

blog

I didn’t find his solution that helpful, but one of the comments said to add this to my ‘.vimrc’ file in my home directory.


if has(”autocmd”)
autocmd BufEnter *.ctp set syn=php
endif

And since we are on the topic here is how my .vimrc is setup

set tabstop=2
set shiftwidth=2
set expandtab

if has("autocmd")
autocmd BufEnter *.ctp set syn=php
endif

syn on

-later