Peter's Blog

Redefining the Impossible

Items filed under vim


I found the best tip ever.

Scenario: hop into /etc and start editing any file using vim of course. Go to write the file and find it's read only: bugger forgot to launch vim with sudo vim or sudoedit. This happens to me around 50 times a day.

Solution:

:w !sudo tee %

I had to go through various man pages to understand this one:

:w ! execute a command, sending it the contents of the current file

sudo run the command as root. The contents of the file being edited get passed through sudo to the command that sudo launches

tee the tee command is normally used to allow one to view text that is being pumped to stdout while also saving it to a file. Here it is being used primarily to write from stdin to a named file. cat > % does not work because the '> %' part is run as the pleb user, not the superuser.

% vim replaces this with the name of the current file

It has the downside that the output of tee that is sent to stdout is fed back into vim so vim will wave it at you unless you type in

:w !sudo tee % > /dev/null

which is far too much typing.

vim will also prompt you that the file has changed and do you want to reload it.

HOWEVER the good thing about this tip is that it is short enough to remember and doesn't need any plugins installed or .vimrc files editing and hence can be used immediately on half a dozen different accounts.


Filed under: sudo vim

Add a comment

I've been vimming a lot recently yet I haven't given any vim tips for an eternity.

With this in .vimrc/_vimrc:

set numberwidth=5
set numbers

vim shows line numbers on each line. I've held out against this for years but now I'm using >= 22" widescreen monitors I am converted.

The only problem is when I am vimming over an ssh tunnel (which I do a lot to maintain PetersBlogger) and I want to copy and paste: the pasted stuff includes the line numbers. When this crops up I do

:nonumber

to turn it off (kinda like doing a :noautoindent before pasting indented code).

UPDATE: this is even better if the background of the line numbers is changed so you can clearly see the gutter they are in:

:color murphy
:hi LineNr guifg=white guibg=darkgreen
:hi LineNr ctermfg=white ctermbg=darkgreen
images/VimLineColor.gif

Filed under: vim

2 Comments

Finally given up with the silly dark-blue on black background text that linux distro's seem so hot on. Life is too short to spend squinting.

To fix dark blue on black directories from ls:

  • edit/create a file called ~/.dir_colors (or copy from /etc/DIR_COLORS or even just edit that).
  • change line
    DIR 01;34       # directory
    
    to
    DIR 01;33       # directory
    
    to make directories yellow. Vim is cool and when you change the numbers it will automatically display them in the corresponding colour.

To fix dark blue on black comments in vim:

  • edit/create a file called ~/.vimrc
  • add line
    highlight comment ctermfg=darkgreen
    highlight comment guifg=darkgreen
    

Filed under: linux ubuntu vim

Add a comment

WingIde 2.1 beta is out and... it has vi keystrokes! python ide, vi keystrokes, it just doesn't get any better.

Since I have been using Wing IDE I have not looked back. Annoyances:

  • Even on a >1Ghz processor it feels sluggish
  • It has an indentation analysis thing that examines the indentation in a file. You copy some dubiously indented code from somewhere and paste it into your beautifully crafted code and it decides to reindent your code with tabstop of 8 or something daft.
  • ugly gtk user interface
  • when editing anything other than python the tab key does nothing and I have to press ctrl-shift-dot to indent. Still, with vi keystrokes >> seems to work.

But for python development, even django and turbogears I wouldn't be without it.


Filed under: python vim wingide

Add a comment

After a couple of weeks of neglect I ran an update on the packages on my debian server. It gave me a few surprises:

  • it upgraded mysql from 4.1 to version 5
  • it uninstalled the webmin mysql module
  • it uninstalled my vim-python version 6.3 and installed plain vim version 6.4

The new version of mysql seems to be running smoothly (or you wouldn't be reading this). Must see if it supports cascade delete, my all-time favourite sybase sqlanywhere feature.

I manually installed vim-python using dselect and was happy. Installer glitch. I haven't addressed webmin-mysql yet, it wasn't obvious from what dselect showed me whether what it had would work with mysql 5. Maybe it doesn't say because it doesn't matter? I find webmin-mysql can do all the mysql database administration I need (create database, add users, set permissions, view table etc) with a much simpler interface than phpmyadmin, which is slightly ott.

I think vim 6.4 is a bug fix release. Upgrading on windows involves recompiling it with python support, not a big deal but it takes time that could be spent doing other things. Sometimes I contemplate hosting vim-python for windows downloads as a public service. I'll contemplate it some more.


Filed under: debian mysql vim

2 Comments

I'm doing some legacy work using Visual Studio 5 (cannot upgrade as the project uses a library that won't build with anything later: that's third party tools for you). I need a real editor so I've set it up to launch Vim on the current file when I bang F8. In the tool settings I put:

Command:

C:\Bin\Vim\vim63\gvim.exe

Arguments:

--remote-silent +$(CurLine) "$(FilePath)"

The Vim incantation tells it to use an existing vim process if it can find it and tells it to go to the same line in the file as I was at in Visual Studio.

I could install the vimole thing and have an ole interface but from my recollection it doesn't add much more than the above, which is simple and robust and works with my custom Vim build that includes python.


Filed under: vim

3 Comments

Kinda exasperated because right-clicking in Windows file manager or Salamander does not give me the option to open any file in VIM. For me this has to include .exe's, .dll's, anything that I might want to open: after all, VIM can view binary files in hex mode.

The VIM setup offers to create something like this but it does not seem to work for me so I've figured out how to hack the registry to do it. A picture is work a 1000 words so:

images/EditWithVim.gif

This gives a context menu entry saying 'Open with VIM' that opens the selected file in VIM.


Filed under: salamander vim windows

1 Comment

In old dos days I used to use the fc command to do a binary compare on two files. I tried it today at a debian bash shell and something very odd happened. It opened up vim showing me the last command line I had typed. When I exited vim it then executed that command.

I had stubled upon something that might be useful, if I can remember it is there next time I want to edit a long command line.


Filed under: debian vim

5 Comments

I am writing this using my Microblog setup in VIM. This is the first test post. If you can read this then that is a good thing.

If you can read this then I can edit as well as post and that is even better.

I have set things up so that I can edit one of three blogs (Work, peters, personal) by typing (for example):

:e blog/Work

in vim.

This will read in all the postings for today where I can edit them or add new ones. I then type:

:w

to update or publish the posts. This is much easier and quicker than using the drupal web interface to do anything.

I can type:

:e blog/Work/-1

to edit yesterdays posts.

Hopefully we are seeing the dawn of a new era of more than one post a day.


Filed under: microblog vim

Add a comment

This is so cool, I have to tell the world how easy it is. Suppose you want to create a new type of virtual file to edit in vim. You can set vim up to automatically call your handlers to read and write files of your magic file type. Here's how: put this in vimrc or $VIM/plugins/mynewfiletype.vim:

   1  python << EOF
   2  
   3  def WriteFile( strFile):
   4      """
   5      Write File
   6      """
   7      #
   8      # Do whatever, send file wherever you like
   9      #
  10      pass
  11  
  12  def ReadFile( strFile):
  13      """
  14      Read file
  15      """
  16      #
  17      # Do whatever, read file from wherever you like
  18      #
  19      pass
  20  EOF
  21  
  22  :au BufWriteCmd blog/* py WriteFile( vim.eval( "expand('<afile>')"))
  23  :au BufReadCmd blog/* py ReadFile( vim.eval( "expand('<afile>')"))
  24  

In the example above I have said that any file name that starts with 'blog/' is to be handled by my read and write functions instead of being read/written to disk.

Once this is done:

:e blog/blah

will cause vim to call the ReadFile function in order to edit the file 'blog/blah' and

:w

will cause vim to call the WriteFile function to write it.

ReadFile and WriteFile can do whatever you like: go over networks, send email, climb mountains, send morse code, whatever you can code in python.

One little word of warning: under windows, the file names get automatically converted to have backslashes, i.e. strFile above will hold blog\\\\blah instead of blog/blah even if you type in

:e blog/blah

to open the file.


Filed under: python vim

2 Comments