Skip to main content

Posts

Showing posts with the label vi

Using a vim macro to fix 'mysql_' calls with regex

 I'm working on a largish project, converting it to work from early PHP 5 to PHP 7.3 and I found myself spending a lot of time converting all those various mysql_* to their equivalent procedural mysqli_* function calls. Mostly pretty tedious. Anytime you encounter something tedious that you'll have to do on a ton of files, it's a chance for automation. In this case, I decided come up with a vim macro that would automagically convert the most commonly occurring function calls. Here's the macro, which uses 4 regular expressions to search and replace the four most common ones - then followed by a search for "mysql_" to catch any outliers. :% s/mysql_query(\(.\{-}\),\(.\{-}\))/mysqli_query(\1 , \2)/eg :% s/mysql_fetch/mysqli_fetch/eg :% s/mysql_num/mysqli_num/eg :% s/mysql_close/mysqli_close/ge :/mysql_ The easiest way to use this is to paste into a new document, then select it all and yank it into a buffer. Then just run the buffer as macro. So, I used 'm...

Quickly create a gallery of images from a list in vim

This is something I run into pretty often, so I thought I'd share how I handle it, in case it's helpful. Sometimes you have a list of image files, and you need to create the html formatting to make them into a gallery of some sort. Imagine each file has two versions, a full size and a thumbnail image. Pretty common scenario. The URL for the files will look like /images/gallery/full/[filename] and /images/gallery/thumb/[filename] There's lots of ways to do this, here's what works well for me. First I grab a list of the files from the command line, ending up with something like this ls -l -rwxrwxrwx 1 user group 388044 May 25 11:16 IMG_20161205_120541.jpg -rwxrwxrwx 1 user group 451788 May 25 11:16 IMG_20161215_114201.jpg -rwxrwxrwx 1 user group 298125 May 25 11:16 IMG_20161216_121618.jpg -rwxrwxrwx 1 user group 369981 May 25 11:16 IMG_20161216_125940.jpg -rwxrwxrwx 1 user group 479464 May 25 11:16 IMG_20161216_131010.jpg -rwxrwxrwx 1 user group 664528 May 25 11:16 IMG_20...

Handy vim - easily generate properly formated bullet lists

This is a problem I run into fairly often, because I do a bit of hand-coding HTML from word files or other content that clients send me. When you cut and paste the items for a bullet list into an html document, they often come in one per line with no formatting (or sometimes they have a "*" at the start of each line. Obviously you can create a macro to automate the process of putting <li> tags around each item, but there's an even faster way to do this using vi's powerful regular expression tools. Of course, this will only be useful if you can use vi, a command-line text-editor. If you're on shared hosting or something, you may be out of luck. There is a windows version of vi, but I haven't really done more than mess with it a bit. If you're on OSX, you're probably in luck - you can use the console, and I think that it has vim installed, although it's been like eight years since I used OSX regularly. First, figure out the lines that you w...

Another VI tip - using buffers

VI lets you use buffers to quickly copy and save chunks of text. You can think of them as being sort of like the clipboard that you copy to when you highlight some text in Word or other such programs - except that you have many, many more clipboards at your disposal. Here's how it works. In VI, highlight some text that you want to copy - hit v and then move the cursor around to highlight the text. Then decide what buffer you want to copy it to. The buffers are identified by a letter - so let's say you wanted to copy some text to the "h" buffer - you would type the following (with the text highlighted) "hy which "yanks" that text into the buffer. You could then paste the contents of that buffer with "hp The contents of the buffers persist until something replaces them - which makes them a great place to store those little snippets of text you use a lot. And in reference to my earlier post about using macros , you can even use buffers within a mac...

Another VI tip - using macros, an example

God I love VI. Well, actually, vim but whatever. Here's another reason why. Suppose you need to perform some repetitive task over and over, such as updating the copyright date in the footer of a static website. (Yes, yes I know you could do a javascript thing or whatever, just bear with me.) Of course you could just search and replace in some text editor, changing "2007" to "2008" (if you're stupid) - and you'll end up with a bunch of incorrect dates being changed, most likely. What you need to do is only change that date at the bottom. And suppose that because of the formatting, you can't use the "Copy" part of the string in a search replace - perhaps some of the pages use "©", some spell out "Copyright" etc. This is where vi macros come in handy. A macro in vi is exactly what you expect, it records your actions and allows you to play them back. To start recording, press q followed by a character to use to "stor...

Six Reasons Why Once You Go CLI You Never Go Back!

(ok, fine I know it doesn't rhyme. I'm a coder, not a poet!) If you're still screwing around uploading and downloading your code to your web server using FTP, it's time you gave the command line a try! Not only is it more efficient to edit your code directly on the server, you'll also find the that the command line tools on your average linux install are quite powerful when it comes to web development! 1. You're on the server - Think how much time you spend dragging/dropping files in FTP, connecting and reconnecting when the FTP connection dies, etc. With a command line you're more saving time already just by avoiding all of that! 2. vi/emacs - These are the two biggies, but there's many, many other text editors available to use on the command line. I'm a vi guy, and it's definitely more powerful than the various GUI-based text editors I've used in the past. The built-in regular expressions alone are great! Sure, there's more than a li...

Handy vi tip - opening a list of files from a grep command

My primary text editor of choice is VI (actually, vim ) - a console-based text editor that I use via SSH on the various linux servers that I work with. There's also a windows version available that I use occasionally, but I'm more likely to use textpad for that work. Anyways, vim when used via a console on a linux server is really quite powerful. Here's a handy tip - suppose you're working with a large number of files nested within multiple subdirectories - and you need to edit all the files that contain a certain string. Perhaps you need to open and edit every file that uses a certain javascript file (let's call it 'example.js'). First, you can find all the files that have this string in them by using grep to search recursively for it: grep -r 'example.js' * This will return a long list of files with the text where that string was found. But what you really need is just the file names that have the string. This can be done by adding the "-l...