Archive for the 'vi' Category

VI: Indent lines

Wednesday, March 12th, 2014

The other day I was editing some code and as usual I was in Vi. It was python code and I had refactored some code and needed to indent a section of code. I did not relish the idea of trying to make sure that all the indents were done on several dozen lines of code so I looked into how to indent the code in VI.

Here is the sequence that let me indent blocks easily.

I used <shift>-V to select the entire line.

Then I selected multiple lines by using the arrow keys or j or k depending on which direction you need to select.

Then you use the ‘>’ key to indent to the right and the ‘<‘ key to decrease the indent to the left.

VI: Reload Current file

Wednesday, October 26th, 2011

When I am editing and having a “clumsy fingers” day, I find that I have to reload the file that I am editing from disk repeatedly. I usually just do a quick “:q!” and re-run the vi command that opened the file. But there is a better way!

Use the :edit command:

:edit

Or you can shorten it to

:e

Now if you have typed in the window you are editing and want to reload from disk anyway without saving, remember to add the exclamation point(!) to the command.

:edit!

or

:e!

Tags: , ,

VI: Paste code without formatting

Thursday, September 1st, 2011

I happened to be creating a script and found myself trying to copy and past some functions between them. Well my functions
had some comments and this caused the paste operation into vi to add comments to every line after the first comment.

So this:

sub somefunc {
  # input args
  my $var1 = shift;
  my $var2 = shift;

  if ($var1 < $var2)
  {
    return $var1;
  }
  else
  {
    return $var2;
  }
}

Became this:

sub somefunc {
  # input args
  #my $var1 = shift;
  #my $var2 = shift;
  #
  #if ($var1 < $var2)
  #{
  #  return $var1;
  #}
  #else
  #{
  #  return $var2;
  #}
  #}

This made all my formatting go awry as well, and there were about 4 different functions I was about to cut and past. So I looked into how to make vi accept the text as is without adding formatting. The answer is the paste and nopaste mode.

To enter the mode to allow pasting without formatting:

:set paste

To return to regular mode:

:set nopaste

There are also ways to bind a key to more quickly enter and exit this mode if you are pasting on a regular basis.
That will be in another article.

Tags: ,

Vi: toggle syntax highlighting

Saturday, August 20th, 2011

If you have ever used vi to edit source code, there are occasions where you get on a system that has a very difficult to read color mode. At these times you want the colors to just go away. You can use the following in command mode in vi:

This will toggle off the syntax highlighting mode.

:syntax off

This will turn it back on.

:syntax on

Tags: , ,