Ivor O’Connor

August 8, 2013

Tutorial / Example VI Using SSH To Edit Remote Hosts

Filed under: howto, IDE, Linux, vi, vim — ioconnor @ 1:28 pm

Your local vi (gVim/Vim) with many of the settings you have painfully setup can be used to edit files on remote hosts. So you can keep your cool colors, most of your key bindings, and most of your tools. I say “most” because obviously your local copy does not have the same environment as the remote copy. If you try to hit the compile key it may not do what you expect. Same is true for your tags file.

:e scp://myusername@myserver/mydirectory/myfile

I have a list of files I routinely edit. So I edit that list of files putting that command on a line and then execute it from that line. Works wonderfully.  I do that by pressing ** over the line in question and having “:map ** “zyy:@z” that line in the equivalent of the ~.vimrc file.

 

It’s amazing how saving a few keystrokes makes things run so much more smoothly.

March 9, 2013

The Universal IDE: VI with SPF13

Filed under: bash, git, howto, html5, IDE, JavaScript, Linux, mint 14 xfce, php, sqlite, ubuntu, vi, vim, vundle — ioconnor @ 4:08 am

This is a youtube video I made on how to install SPF13 on Linux Mint 14 XFCE. I like learning from youtube videos whenever possible. If in the future I want to install SPF13 and something is not how I remember it I can return to this video. SPF13 makes VI into a universal IDE. It works on most if not all OSs including windows and the mac.  Sure if you are working with something like AIX, HP-UX, or some platform that does not have vim installed SPF13 will not be for you. I’d recommend you put in your base .vimrc commands directly into the .vimrc.local file and use them along with SPF13. Picking and choosing what you find works.

UPDATE 2013-03-08: Insert the following line into your .bashrc file before starting to get full colors: export TERM=”xterm-256color”

February 19, 2013

The Vim Vundle: spf13-vim

Filed under: bash, IDE, vi, vim, vundle — ioconnor @ 5:41 am

The SPF13 vundle is quite interesting. It is a pre-configured IDE for vim. It comes with quite a few of the best sub-vundles already built in. And works on Windows and Macintoshes just as it does on Linux. However it totally changes vim the look of vim and removes the modifications you may already be comfortable with. When finished installing your vim will look nothing like anything you may have been comfortable with before.

Take the install script and copy it into a file. Run that file. Be prepared to spend hours learning the new tools!

January 25, 2010

Navigating Log Files With VIM

Filed under: bash, cli, howto, Linux, tutorial, vi — ioconnor @ 6:40 pm

One of the main problems with VI, VIM and GVIM is the navigation between multiple files. The “:n” and “:rew” commands may get somebody through the basics. But seriously it’s not enough. The secret is writing little macros for the work being done. Often this is such a daunting task that most people haven’t got a clue. So hence this article. The idea presented here is to show how to make a navigation file so that once the cursor is over the file name in question, or information needed to identify the file, two keystrokes will take you to that file.

First there is the ~/.vimrc file or equivalent depending on the OS. Locate it. Add to this file the macros.

:map ** “zyy:@z<Enter>

This command maps the keystrokes “**”, the two asterisks hit in rapid succession, to the commands that follow it. The command that follows reads the line the cursor is on into the z buffer and then executes that buffer. This is very useful because now the commands you want to test can be written on a line and then executed by pressing “**”. It opens the world of macros to you.

So lets write a macro that will bounce the window between the current file and the last file you edited. Say you started gvim by typing gvim file1 file2 file3 and once in file1 you then typed :n and :n again. So you are now in file2. The goal of this macro is to let you move to file2 without doing a “:rew” and “:n”. So start a new line in file 3 and type in:

:map *; :e#<Enter>

Then press “**” over the line so this macro becomes available. Now if you press “*;” you’ll go to file2.

Now we are ready to write some macros to view log like files. I’ll start with the one I wrote yesterday. Websites require a file named “Sitemap.xml” which describes each file’s location, importance, and frequency they should be crawled by the search spiders. Each line has a format similar to this:

<url><loc>http://www.domain-name.com/index.html</loc><priority>0.50</priority><changefreq>weekly</changefreq></url&gt;

Three macros might be all that is needed to navigate a website via its Sitemap.xml. File navigation this way ensures the Sitemap.xml always stays relevant. So the first macro might be something like:

:map *S :e ~/path/Sitemap.xml<Enter>

This is simple enough to understand. When “*S” is typed it will take you to the Sitemap.xml file.

Now the following is all that is needed to edit the files listed in the Sitemap.xml by putting the cursor on the line containing the file and typing “*e”:

:map *e :w<Enter>$0fhf<mz0fml”ay`zo:e ~/path-to-website/<Esc>”ap<Esc>”zyyu:@z<Enter>

First the file is written out with this command “:w<Enter>”. Depending on your habits you may want to remove that.
Then the cursor is moved to the end of the line and then to the beginning with “$0”. There is history to this that is wasted on VIM/GVIM.
Then a search for the ‘h’ in “http” is done, to get to the start of the URL, with “fh” and then another search for the end of the URL with “f<”
Then that location is marked as location “z” with the command “mz”. Then we go back to the start of the file name which for my Sitemaps can be done for searching for the “m” in com. You may have to vary the search depending on your domain name. Then moving the cursor over to the “/”. So altogether it’s “0fml”.
Now yank the name of the file into the a buffer using the backwards tick “”ay`z”.
Now create a line with the command needed to edit the file just yanked “o:e ~/path-to-website/<Esc>”ap<Esc>”
Finally yank the newly created line into the z buffer, undo the line just created, and then execute the z buffer so the editor is now editing the file “”zyyu:@z<Enter>”

The next command sets the browser to the file in question:

:map *b :w<Enter>$0fhf<mz0fh”ay`zo:!gnome-open <Esc>”ap<Esc>”zyyu:@z<Enter>

These techniques can be used for almost anything to avoid typing names. In fact a list of all files you edit, in a centralized place, is all that’s needed to prevent ever repeatedly typing out a file name again. Once familiar with these techniques most of the files needing editing can be gotten to with two key strokes. This gives VI, VIM, and GVIM the ability to navigate files more easily than an IDE without losing all the power you surrender when using an IDE. And it’s all free!

March 17, 2009

Sorry state of “agrep”…

Filed under: awk/sed, bash, cli, Linux, ubuntu, vi — Tags: , , , , , , — ioconnor @ 7:27 pm

One very useful tool is “agrep”. Say a customers calls in and they live on “pentrose”. If you had a directory of customers you might be able to do a search for them by typing “agrep -i -2 pentrose *.*”. The “-2” says find any characters that are off by two or less from “pentrose”. So “Penrose”, “pentroose”, “Pentrous” and “Pentrouse” would all have matched. This is very helpful especially considering google maps and thomas brother maps often spell street names slightly different. Unfortunately the “-l” option telling agrep to list the file names does not work. See the following dump:

/customers$ agrep -V

This is agrep version 3.0, 1994.

/customers$ locate agrep
/usr/bin/agrep
/usr/share/doc/agrep
/usr/share/doc/agrep/README.gz
/usr/share/doc/agrep/agrep.algorithms
/usr/share/doc/agrep/agrep.ps.1.gz
/usr/share/doc/agrep/agrep.ps.2.gz
/usr/share/doc/agrep/changelog.Debian.gz
/usr/share/doc/agrep/changelog.gz
/usr/share/doc/agrep/contribution.list
/usr/share/doc/agrep/copyright
/usr/share/man/man1/agrep.1.gz
/var/cache/apt/archives/agrep_4.17-5_i386.deb
/var/lib/dpkg/info/agrep.list
/var/lib/dpkg/info/agrep.md5sums
/customers$ agrep -il -1  biider *.*html
customer_2401.shtml
*** glibc detected *** agrep: double free or corruption (top): 0x0821bf48 ***
======= Backtrace: =========
/lib/tls/i686/cmov/libc.so.6[0xb7e8aa85]
/lib/tls/i686/cmov/libc.so.6(cfree+0x90)[0xb7e8e4f0]
agrep[0x804b7df]
agrep[0x805751c]
agrep[0x8055dea]
agrep[0x80571bc]
agrep[0x8057228]
agrep[0x8066519]
/lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe0)[0xb7e35450]
agrep[0x8048b81]
======= Memory map: ========
08048000-08068000 r-xp 00000000 08:01 6931968    /usr/bin/agrep
08068000-08069000 rw-p 00020000 08:01 6931968    /usr/bin/agrep
08069000-0823a000 rw-p 08069000 00:00 0          [heap]
b7b00000-b7b21000 rw-p b7b00000 00:00 0
b7b21000-b7c00000 —p b7b21000 00:00 0
b7cdf000-b7ce9000 r-xp 00000000 08:01 5744705    /lib/libgcc_s.so.1
b7ce9000-b7cea000 rw-p 0000a000 08:01 5744705    /lib/libgcc_s.so.1
b7cfd000-b7cfe000 rw-p b7cfd000 00:00 0
b7cfe000-b7d3d000 r–p 00000000 08:01 6964092    /usr/lib/locale/en_US.utf8/LC_CTYPE
b7d3d000-b7e1e000 r–p 00000000 08:01 6964091    /usr/lib/locale/en_US.utf8/LC_COLLATE
b7e1e000-b7e1f000 rw-p b7e1e000 00:00 0
b7e1f000-b7f68000 r-xp 00000000 08:01 4252758    /lib/tls/i686/cmov/libc-2.7.so
b7f68000-b7f69000 r–p 00149000 08:01 4252758    /lib/tls/i686/cmov/libc-2.7.so
b7f69000-b7f6b000 rw-p 0014a000 08:01 4252758    /lib/tls/i686/cmov/libc-2.7.so
b7f6b000-b7f6e000 rw-p b7f6b000 00:00 0
b7f70000-b7f71000 r–p 00000000 08:01 6964097    /usr/lib/locale/en_US.utf8/LC_NUMERIC
b7f71000-b7f72000 r–p 00000000 08:01 6964100    /usr/lib/locale/en_US.utf8/LC_TIME
b7f72000-b7f73000 r–p 00000000 08:01 6964095    /usr/lib/locale/en_US.utf8/LC_MONETARY
b7f73000-b7f74000 r–p 00000000 08:01 6971396    /usr/lib/locale/en_US.utf8/LC_MESSAGES/SYS_LC_MESSAGES
b7f74000-b7f75000 r–p 00000000 08:01 6964098    /usr/lib/locale/en_US.utf8/LC_PAPER
b7f75000-b7f76000 r–p 00000000 08:01 6964096    /usr/lib/locale/en_US.utf8/LC_NAME
b7f76000-b7f77000 r–p 00000000 08:01 6964090    /usr/lib/locale/en_US.utf8/LC_ADDRESS
b7f77000-b7f78000 r–p 00000000 08:01 6964099    /usr/lib/locale/en_US.utf8/LC_TELEPHONE
b7f78000-b7f79000 r–p 00000000 08:01 6964094    /usr/lib/locale/en_US.utf8/LC_MEASUREMENT
b7f79000-b7f80000 r–s 00000000 08:01 6947512    /usr/lib/gconv/gconv-modules.cache
b7f80000-b7f81000 r–p 00000000 08:01 6964093    /usr/lib/locale/en_US.utf8/LC_IDENTIFICATION
b7f81000-b7f83000 rw-p b7f81000 00:00 0
b7f83000-b7f84000 r-xp b7f83000 00:00 0          [vdso]
b7f84000-b7f9e000 r-xp 00000000 08:01 5693498    /lib/ld-2.7.so
b7f9e000-b7fa0000 rw-p 00019000 08:01 5693498    /lib/ld-2.7.so
bf89e000-bf8c4000 rw-p bffda000 00:00 0          [stack]
Aborted
customers$

You’d think such a handy utility would be kept up-to-date. Yet it says it is version 3 from 12 years ago. This is not true. The actual version synaptic gives is 4.17-5.

To get around this goofiness and get the documented “-l” option without the core dump use:

/customers$ agrep -i -1 biider *.* | awk ‘{ print $1 }’ | sort | uniq
customer_2401.shtml:
customer_6986.php:
/customers$

I just put it into a script file called “f” so I can simply type “f biider” and view the files…

#!/bin/bash
if [ “1” = “$#” ]; then
time gvim +/$1 $(agrep -1 -i $1 /customers/*.*html /customers/*.php | awk -F”:” ‘{ print $1 }’ | sort -rn | uniq )
elif [ “2” = “$#” ]; then
time gvim +/$1 -n $(agrep -2 -i $2 $(agrep -1 -i $1 /customers/*.*html /customers/*.php | awk -F”:” ‘{ print $1 }’ | sort -rn | uniq) | awk -F”:” ‘{ print $1 }’ | sort -rn | uniq )
else
echo “wrong number of arguments”
fi

agrep is so very useful. Somebody really should fix it. Until then use my workaround.

March 1, 2009

gvim tips

Filed under: Linux, ubuntu, Uncategorized, vi — Tags: , , — ioconnor @ 4:53 pm

This post will be updated every time I have to do something in vim taking more than a few seconds to get right. Or when I find interesting reference information I don’t know or forget on a regular basis. The idea is to include everything I do and need with vim in this post.

I prefer using gvim for some reason. Don’t really have a reason I can remember. The useless toolbar on top can be turned off with:

set guioptions-=T

The most useful command in all of vi/vim land I have ever used is the following held within the .vimrc or whatever. It allows the line under the cursor to be executed. With clever useage vi becomes very useful. I’ve used it on all versions of vi for decades.

:map ** “zyy:@z

along with the following that needs to sometimes be modified heavily depending on the version of vi. This allows you to bounce between the two buffers with just a keystroke. (Removing the ability to use the standard ‘;’ for what it was intended for.)

:map ; :e#

When viewing a downloaded google spreadsheet in csv format of all deposits the following displays the summary of each deposit

:g/^\d\{1,3},”\p\{-}”,200

Removes the first 7 words of each line and replaces them with “rm “. Helpful when editing a file made with “ls > file”.

:%s/\(\S\+\s\+\)\{7\}\(.*\)/rm \2 /

Using vim’s internal sort. Start by marking the beginning and ending line with say ‘b’ and ‘e’. (Note the ‘}’ does not help in specifying range unfortunately.) Then type:

:’b,’esort

I’m always forgetting vim’s regexps metacharacters. The following I stole from this excellent guide http://www.geocities.com/volontir/

. any character except new line
\s whitespace character
\S non whitespace character
\d digit
\D non-digit
\x hex digit
\X non-hex digit
\o octal digit
\O non-octal digit
\h head of word character (a,b,c…z, A,B,C…Z and _)
\H non-head of word character
\p printable character
\P like \p, but excluding digits
\w word character
\W non-word character
\a alphabetic character
\A non-alphabetic character
\l lowercase character
\L non-lowercase character
\u uppercase character
\U non-uppercase character

Non-greedy quantifiers where n and m are positive integers (>0):

\{-} matches 0 or more of the preceding atom, as few as possible
\{-n,m} matches 1 to m of the preceding characters…
\{-n,} matches at least 1 or more of the preceding characters…
\{-,m} matches 1 or more of the preceding characters…

The greedy options have all the obvious non-greedy options plus the semi-obvious extras as follow

\{n} Matches exactly n characters. It is not greedy or non-greedy.
* Greedily matches 0 or more of the preceding whatever. “.*” matches everything including an empty line. Notice there is no backslash before the ‘*’
\+ Matches 1 or more of the preceding whatever.
\= Matches 0 or 1 of the preceding whatever.

There are valuable replacement options:

& The entire part matched by the pattern.
\1 When 1 is used it is the first pattern. A 0 is the same as the & above and probably makes more sense. I don’t know if two digit numbers can be used or if it stops at 9.
~ The previously substituted string.
\L \U \E \e Turns upper or lower case on until either of the end control characters are encountered.
\r Splits the line in two by adding a return character.
\l \u Makes the next character upper or lower case.

Create a free website or blog at WordPress.com.