Monday, January 30, 2012

Vim Visual Mode

Assume - You know VIM to some degree
Assume - You want to make your life easier


Believe it or not, there is something -- somewhat reasonable in VIM and that's visual mode -- NO NOT ANOTHER MODE !!! Isn't Command mode and Normal (ya right normal) mode Insert mode enough!!!

Well, relax, Visual mode is actually intuitive and easy to access.  Anytime you are in Normal mode (press ESC to get to it, type:

---------------------------------------------------
v
---------------------------------------------------

(For Visual mode of course) When in Visual mode, any movement key you press -- including cursor keys will highlight text!  That's right, you can see what you are doing. Remember all the commands I'm going to give you require you to first highlight the text.

Well, here are my favorite things to do in Visual mode (you do know to highlight some text right???)

Cut/Copy/Paste
When you have your text highlighted -- do that of course first -- press
---------------------------
d
----------------------------
That deletes text but it does more than that -- it sticks it into the copy buffer for you to use later (sort of like copy and paste)
-------------------------
y
-------------------------
Yank (y) will take your highlighted text and stuff it into the copy buffer keeping the original on screen.

ONCE you have done either command...move the cursor somewhere else and press:
------------------------
p
-------------------------
This will paste it into your document.  There you have cut/copy/paste...but there is one more.

Indent
Nothing is more important or more frustrating that looking at code where someone refuses to indent.  You know the type:

if ($Fred == 5)
{
echo ("Foo");
if ($Bob == 4)
{
echo ("Stew");
}
}
Hideous!!!
Now highlight all the text you WANT TO INDENT.  Once you do that type:

<< OR >>
All the text moves giving us something like this:

if ($Fred == 5)
{
      echo ($Foo);
      if ($Bob == 4)
     {
          echo $Stew;
      }
}
Much more readable.  Good luck!

Tuesday, January 24, 2012

PHP String Heredoc

Assume:
You know how to program in php

Any Web Server programmer will encounter PHP and one of the biggest headaches in PHP (IMHO) is understanding strings. 


First, this is an excellent article on php strings.

http://www.trans4mind.com/personal_development/phpTutorial/quotes.htm


Today though I want to concentrate on a tool that can help out.  As you know when you insert HTML or Javascript code in PHP things can get ugly in a hurry mainly due to the problems with quotes.   Sadly a basic keyboard only has two characters for quotes :  ( double "  and a single ')  But look at this example:

echo ( "onclick='DoAJavaScript ( 'Fred', 'Bob')' ");

This code won't work for several reasons but as you notice when we came to the variables of (Fred and Bob) we needed yet another set of quotes to cover that issue.  The problem is we used the double quotes to start the string and then the single quotes to start the Javascript call 'DoAJavaScript' leaving nothing left for the parameters. 

WHAT DO I DO????
Well there are a couple of solutions.  You can take advantage of the ESC '\' character sequence.  The key idea is that you use \" instead of a " to wrap a string and now the code gets really ugly:

echo ("onclick='DoAJavaScript ( \"Fred\", \"Bob\" ");

Wow this is getting really ugly and there is another problem.  PHP will only evaluate code with a double quote.  Everything else it takes literally...a real mess!!!

Example:

$Fred = "Hello";
echo ("$Fred");
// You get:   Hello

echo ('$Fred');
// You get: $Fred

However...don't fret.  There is a most convenient but hardly talked about function in PHP that is a lifesaver.  It is called heredoc and despite the convoluted name it works magnificently.

$Fred = <<<BLOCK

onclick='DoAJavaScript ('Bob', 'Fred');

BLOCK

Sweet.  With this crazy block of code you don't need starting and ending quotes saving you a serious headache.  Now take a look at the format.

BLOCK -> Notice that BLOCK starts and ends the string.  They have to be the same name and have to be upper case...AND have to be on the left hand side of the document.  (In other words, don't indent otherwise it won't work.

<<<  (This sends the information to the string.)

$Fred (The string variable.)

Now take advantage of this the next time you create a messy string structure.

Vim and Tabbed Windows

Assume:
You know how to use Vim,
You want to continue to use Vim,
You have a streak of nerd in you!

OK, in Vim, there is a typical way to split windows but want I want to talk about today is how to show multiple files as if they are tabbed windows.  Granted, its not FireFox but it works very well and will save you a lot of trouble. 

(Of course the problem with Vim is that nothing is every easy)

First, let's launch vim preferably with a file.

---------------------------------------------------------------------------------------------------------------------------------
vim (name of file)

---------------------------------------------------------------------------------------------------------------------------------

Now once in Vim we now need file number 2.  The problem is if we just load it, we delete file number 1.  Not good!  
Instead we type this into our Vim editor.


---------------------------------------------------------------------------------------------------------------------------------
:tabnew (name of file)
---------------------------------------------------------------------------------------------------------------------------------

Now you should see two tabs on the screen.  Yes!

There are a few ways to get to the file in question but my favorite is this:


---------------------------------------------------------------------------------------------------------------------------------
tabn 1 (name of file)

---------------------------------------------------------------------------------------------------------------------------------
 In this example it will go to tab 1.

---------------------------------------------------------------------------------------------------------------------------------
tabn 4

---------------------------------------------------------------------------------------------------------------------------------
This will go to document 4.

To close a tab, its the standard:


---------------------------------------------------------------------------------------------------------------------------------
:q

---------------------------------------------------------------------------------------------------------------------------------

(or :wq) if you need to write the file to disk.

That's it. 

To be complete:

:tablast
:tabfirst
:tabprior
:tabnext

Again all you're doing is tab + (first,last,next,prior)

Enjoy your tabs!

Wednesday, January 18, 2012

Check a log of user logins

Assumptions: You can type a command at the command prompt


Ever wanted to simply find out who logged in when and what users you have on the system?  Easy!!!

You can type:


------------------------------------------------------------------------------
lastlogin
-----------------------------------------------------------------------------

It will give you a detailed list of users and when/if they ever logged in.  Want to filter the list?  Easy!!!


------------------------------------------------------------------------------
lastlogin | grep (name of user)
-----------------------------------------------------------------------------

The command grep, as you know (or don't know) is an amazing command for filtering information displayed on a screen.  You simply pipe out the information from lastlogin into grep and grep will filter the content based on that user.  For example, if you have the user Fred, type this:


------------------------------------------------------------------------------
lastlogin | grep fred

-----------------------------------------------------------------------------

That's it.  No quotes or special characters required!

Tuesday, January 17, 2012

Change The Name of your Ethernet Card in Linux

Assumptions:
- You know about editing files:
- You know about the command line:
- You know what (eth0, eth1) is:


As you know (or don't) linux automatically assigns a name for any Ethernet card in the system.  This can be helpful -- unless you change cards and discover all your links to that card are...gone.  How do you fix it???

Well, Linux has one directory that you should know about and its called /etc.  That directory is off of  / (main root directory).  If you want to change anything, try to find the file in there first. 

First using your favorite editor type this:

------------------------------------------------------------------------------------------------------
vim /etc/udev/rules.d/70-persistent-net.rules
------------------------------------------------------------------------------------------------------

Now I'm using vim for an editor but you can use anything you want.  Inside the file though will contain
obvious entries with eth0, eth1, etc.  Change the names as desired and save the file.

THAT IS IT!!!!

First Entry:

This Website (Blog) is really about me recording information so that users don't have to deal with the headaches I dealt with.

I will:

Give you tested tips:

Give you detailed steps:

Try not to be too serious: