Showing posts with label Unix. Show all posts
Showing posts with label Unix. Show all posts

Wednesday, January 8, 2025

Using the vi Editor

It is not every day that I have to edit large blocks of code. Usually I will put together a few lines of SQL or Python and pretty much any text editor will do. Today I put together a 450-line SQL statement and so I pulled out my favorite code editor: vi.

A common mistake with vi is how it is pronounced. Technically it should be pronounced as the two letters "V" and "I" but some try to pronounce it as a single-syllable word that rhymes with hi. Another common mistake is that it is difficult to use. It does have a pretty ugly learning curve for beginners but once you learn how to use it, it is significantly faster than most other text editors. Why? Because you never have to take your fingers off the keyboard. If you know how to touch type, you know not having to pull you hands off the keyboard to use the mouse or cursor keys can be lightning fast.

The vi editor evolved with the Unix operating system and became the de facto editor back in the 1980's. That is when I started doing technical support for Oracle running on Unix systems. My boss sat me down and made me learn both Unix and vi. The main point of confusion is getting used to being in command mode or edit mode. In command mode, all the letters of the keyboard have a specific function. The letter "h" moves the cursor to the left, "j" moves the cursor down, "h" moves it up, and "l" moves it to the right. Once you get used to that, you never have to take your hands off the letters to use the cursor arrows to move the cursor. It does take a lot of practice though.

To get into edit mode, you just hit the "a" or "i" keys. The "a" stands for append which allows you to add more characters after the cursor where "i" is for insert and adds characters before the cursor. Then to get back to command mode, you just hit the "ESC" or escape key. Once you play around with vi for a bit, your left pinky finger can find the escape key fairly quickly.

There are some other quick shortcuts that make editing code very efficient. If you want to delete the current line, just double tap the "d" key. If you want to copy the line, double tap the "y" key, which is short for yank. Then to paste either what you have deleted or yanked, use the "p" key and vi pastes the line below your cursor. If you made a mistake with what you deleted or pasted, just hit the "u" key and it will undo your last command. Hit it a couple of times and it will undo the previous couple commands.

Up until now, nothing seems that groundbreaking. The real power comes from prefacing any command with a number. If you want to move the cursor down 5 lines, just type the "5" key followed by the "j". Want to copy or yank 10 lines, just type "1" followed by "0" followed by "y" and followed by "y" again. Then when you want to paste them, just go to the line above where you want the copied lines to be inserted and type "p".

Then there is my favorite vi command: the do-it-again key which is the period or "." If you copied something and want to paste it several places in your document, just copy the line and move the cursor to the line above where you want it to go and press "p". Then move to the line above the next place you want it to go and hit the period key. If you want 5 copies of the line, hit "5" followed by "." and you will see 5 identical lines below your cursor.

Now I recognize that vi would be horrible for creating a term paper for school or any document for that matter. So I don't recommend it for a general-purpose editor. It has evolved over time though to be a very efficient code editor and that is where it excels. It is available for Windows for free and comes installed on all Mac and Linux machines. Most people know it as Vim, which is just an enhanced version of vi with color coding and a few other goodies. If you need to create code, get familiar with it and you might learn to appreciate it. Just don't be turned off by the difficult learning curve.

Wednesday, October 18, 2023

Storing Electronic Files

Ever since my first computer I have had to work hard to best organize my electronic files. It started with school papers that I thought I would want to keep around forever. Then the problem seemed to explode as film cameras gave way to digital ones and I started storing photos on my computer. Throw in how to best keep e-mail messages and you can see how having a good filing system is required.

Back with my original computer I used floppy disks to store all my school papers. The documents never got very large and so I could use one disk for each class. Even if I still had those disks, I don't have a computer that can read them. Computer operating systems evolved to have a default storage directory or folder for your documents, appropriately named "Documents." There are also picture and movie folders. You can then nest folders inside of other folders to help you organize your electronic files even more.

In my pictures directory I have created a number of folders by subject. I have one folder that I use to store all the pictures of my grand kids and another for my trips to Disneyland. What happens when I take pictures of my grand kids at Disneyland? This is why it is important to have a good filing system. Unix and Linux allow you to easily create symbolic links. This means you can have one file with links to that single file appearing in multiple directories. Unfortunately that is a lot of work to set up when I am just dumping photos onto my computer. Instead I just know all photos related to a trip will appear in the trip folder even if they contain pictures of my grand kids. The "Grand Kids" folder is just the repository for all other pictures of my favorite little people.

What about multiple trips to Disneyland? That is easy. I have a top folder named, "Disneyland," with sub folders inside listing the dates of each trip. I should probably do something similar with my grand kids folder so I know which kid is who while I can still remember. Once kids get older they become much more difficult to differentiate between siblings. I'll save that for another day.

Finally there is the dreaded e-mail organization. I am one of those people that just keeps everything important in my "inbox" until it is no longer needed. Speaking from experience, that is a horrible way to store e-mail. Every week or so, I will go through and delete unneeded e-mails. I also have other mailboxes that relate to major categories. My work e-mail has a mailbox for each project I am working on. My personal e-mail has categories like skiing or Sony, should I receive a work e-mail in my personal account. About once a week, I take an hour and go through both e-mail accounts and move messages from my inboxes to appropriate mailboxes. A lot of the time I will delete messages as they become irrelevant. If I am really on top of things, I will move a message into the appropriate mailbox as soon as it comes in, which is ideal.

So what is your best electronic file storage tip? I'd be interested to know as it may help me. I'm sure others would be interested too. 

Wednesday, March 29, 2023

My Favorite Short Cut: ALT-TAB

You can tell how long someone has been using computers by paying attention to the short cuts he/she uses to navigate on the computer. I usually have quite a few application running on my computer and I need to be able to quickly switch between them. Back when dinosaurs roamed the earth and Windows 3.0 first came on the market, I used to only have one or two apps running and could easily just click between them. It didn't take long to quickly exhaust that functionality, especially with the small screens on laptops we had back then.

I remember leading a training session on a new database tool and I kept having to switch between a PowerPoint presentation and the tool. I would escape out of presentation mode, minimize PowerPoint, and show my demo. Once I completed the demo, I would expand PowerPoint and go back into presentation mode. Then someone showed me the power of ALT-TAB that allowed me to instantly switch between all running applications. I thought it was great and still use that key combination today even though there are more advanced ways of switching between running applications. For instance, you can see the running applications in Windows by just looking at the toolbar on the bottom of your screen. I still prefer ALT-TAB.

There are a number of other computer short cuts that will give away the length of time someone has been using computers. My work laptop run Microsoft Windows but I am an old Unix guy. When I open the Windows Power Shell it is a hard to remember not to use the "ls" command to get a directory listing of files instead of the preferred "dir" command familiar to those who learned "DOS." Fortunately Power Shell understands both commands and so I don't get the old "Syntax Error" I used to.

Another trick that really shows how old I am is from when I started using Oracle version 4. When you wanted to get a list of all the tables in the database, you would run the following command:

SELECT * FROM tab;

The result was a very simple listing of tables and some other basic information. Oracle later added more complete table definitions but I still use this simple command. Why? Because it is so simple and easy to remember. Are there better ways to find out what tables are in your database or schema? That depends upon how you define better. If you have to go to a manual and look it up, nope.

I used to work for a company that took PostgreSQL and made it look like Oracle for a lot less money. The first thing I tried when I sat down to play with the product was the command listed above. When it worked, I knew there were others at the company that appreciated quick and simple. I also knew they had people on the development staff that had used Oracle for a very long time, an important fact to me at the time.

Thursday, March 20, 2014

Running Something Other Than Windows 8

My wife is fed up with trying to run Windows on her new laptop. I spent some time last weekend playing with it and have to agree that Windows 8 is a giant step backwards in the evolution of operating systems. This week I have spent some time looking at alternatives. There are basically 3 choices when it comes to PC operating systems: Windows, MacOS X, and Linux/Unix. I will be working from home next week and will have more time to devote to my wife's computer and will see what I can get running.

There are a number of tutorials on how to get Windows 8 into a usable state. Just use your favorite search engine and use the following key words: Making Windows 8 Usable. With everyone so dissatisfied, I can't believe it is selling any product.

When you look at a Mac computer from Apple, it uses a lot of the same hardware as a normal Windows PC. Therefore you should be able to run the Mac OS on a carefully assembled PC. You can, and to prove it, there is the Hackintosh website to help you get started. Unfortunately this is more of an option if you are building a desktop as opposed to trying to get it to run on an existing laptop. There are some laptops that work better than others but my wife's model is not among them.

The next option is to throw away Windows and run Linux on the laptop. Unfortunately Linux can be much more difficult to use than Windows. Fortunately there is a UI that is easily installed on Ubuntu Linux that makes it look like a Mac and it is called Macbuntu. I will give that a shot and see if my wife likes that.

I should have an inexpensive laptop that my wife can use by the end of next week. I promise that I will give Windows a fair shake as there are a number of people who think it is awesome (I guarantee you that they are the same people that ate paste in kindergarten but are just grown up now). Look for an update next week.

Tuesday, December 1, 2009

Bad Typing

I have always considered myself a fast typist. I also think I am fairly accurate. This morning I learned that I am delusional. While I may be fast, I am not accurate at all.

One of my latest software projects is to convert a bunch of Unix cron jobs to Microsoft's SQL Server Integration Studio (SSIS). These are basically reports that query a MySQL database every day at the same time and spit the output into spreadsheets. It sounds easy enough but the queries are about half a page each and there six of them.

When I started creating the project, there was no easy way to copy and paste the database queries. Instead I had to retype them. I felt like I was accurate as I was typing but then came the true test where I ran them against the database. All six of them contained various errors. Some were as simple as using one double quote instead of two single quotes. Others were a bit more severe such as three missing lines.

I was less than pleased with my typing skills and gained a whole new appreciation for "cut and paste." When I couldn't find one particularly tricky problem, I figured out a way to make cut and paste work. Now I just wish I had gone through the effort much sooner. It would have saved me a lot of typing and debugging.

Tuesday, September 8, 2009

What is Linux?

I was at a friend's house last week who was giving me a bunch of photo's and movies from one of my many summer activities. He had some questions about Windows XP that I couldn't answer without a bit of research because I don't really use Windows on a day-to-day basis. Naturally he asked what I did use. While I have both Windows and Mac computers in my office, my primary computer runs Linux.

So what is Linux and why would anyone remove Windows from a new computer and install Linux instead? Actually I don't completely remove Windows from the computer when I install Linux. I set it up so that when I turn on the computer, it asks if I want to run Linux or Windows. This gives me a bit of flexibility with each of the machines in my office.

So back to the first question: What is Linux? Linux was created by Linus Torvalds in 1991 who wanted to create an operating system or computer user environment similar to Unix. Unix is an operating system created by Ken Thompson at Bell Laboratories in 1969. Linus was able to leverage other work done by Richard Stallman who created the GNU project in 1983 to create another Unix-like operating system. So what is Linux? Simply an open source or free version of Unix.

OK, so what is Unix and what makes it better than Windows or Macs for day-to-day use? Better is a relative term and so it is with extreme caution that I continue. When I worked at Oracle Corporation for the summer after my freshman year of college, they put me in the Unix technical support group and had me learn Unix. This was back before graphical user interfaces (GUIs) that most people are familiar with. Instead of being able to use a mouse to navigate thru directories and file folders, one had to know commands like "cd" and "ls". If you knew the name of file but didn't know where it was located, you could use the "find" command. Unix also has a bunch of tools and utilities that I learned such as the text editor called "vi" (pronounced like the letter V and the letter I). You could say that I got really used to using these two, three, and four-letter commands to help navigate the computer. Since my hands never have to leave the keyboard to touch the mouse, I feel I can do things faster and so I have continued to use Linux. Does that make Linux better than Windows? Probably not for most users, but it does for me and so I continue to use it.

Interestingly enough the operating system used by Macs is also Unix. Apple just did a spectacular job of hiding all those cryptic commands so that even the most novice user can enjoy the Mac experience. However if you know how to open a terminal window, you can revert back to those short-but-confusing Unix commands.

One of the benefits of Unix is how it protects itself from rogue programs like viruses and malware. When the PC first came out, it cam with the Microsoft DOS operating system and could only run one program at a time. Unix on the other hand was created to run multiple programs for different users at the same time. The creators of Unix knew that Jimmy might be running a program that could crash the computer and cause harm to Susan's program. Therefore they built in protection so that Jimmy's program cannot harm Susan's nor can it harm Unix itself. Therefore it is much more difficult to write a virus for Unix than it is for DOS which has now evolved into Windows.

This brings us back to my friend asking about Linux. It seems that my friend's son had contracted a nasty virus on his computer. In an attempt to cover his tracks, the son had reformatted the hard drive. He figured he could just reinstall Windows and nobody would know there had been a virus. Unfortunately the son didn't have a license key for Windows and couldn't reinstall it. Eventually the son had to confess about the virus. While my friend could easily get a license key from Microsoft to reinstall Windows, he has opted to put Linux on his son's computer. Maybe now his son won't get a computer virus.