Skip to main content

Posts

Showing posts with the label regex

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...

using vim and command line to backup and remove a lot of directories

Was just working on some server cleanup for a client and thought that this might be a handy tip for anyone out there in a similar situation... Here's the problem, I have a directory on a server that has lots (i.e. thousands) of subdirectories. I need to copy those up to an S3 backup bucket - organized by year and month - and then delete them. The tricky bit is they're not organized by month/year right now. It's just a giant mess of subdirectories without much of a coherent naming strategy. So, here's what I've come up with so far. First, I generate a text file listing all the directories, filtering through grep for the year I'm interested in, i.e. ls -ltr | grep 2019 > list.txt This gives me a list of all directory entries that have '2019' in them, which should get them all since we're using 'ls -l' Next, I open that file in vim, and copy all the files for the month I'm backing up, and use a regex to remove the leading bi...

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...