Archive for the 'Editors' 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.

My Favorite Vi(m) commands

Thursday, September 20th, 2012

I have found myself using Vim as my Linux editor of choice.  It is not dependent on X11 being available and I can edit pretty much anything fairly quickly.  I have been using a Redhat flavored linux(Redhat,Fedora, Centos) for quite sometime and because I couldn’t guarantee that Emacs would be installed I just got used to using Vi.

When I first attempted to use Vi after using Emacs from my college days, I was initially very frustrated with modes and key sequences.  But after a very short time I found I could edit files very rapidly and make changes quite easily.

Quick key list:

Esc The escape key is the way to get out of editor mode and clear
Ctrl-c will exit the editor mode
:q! Exits without saving
u Undo
cw changes the following word
dd deletes the line
D deletes the line to the end
J removes the end of the line
:e or :edit reload the file from disk

1) The thing that I do quite often is to replace all Ctrl-M (Windows Returns) within a file.  (Yes, I could do this with a call to dos2unix, but this is a Vi list).   I found the following key sequence works quite well.

:%s/Ctrl-v<enter>//g

breaking this sequence down

2) Another sequence I use quite often is to reformat source code.

:1 <enter> <shift-v> <shift>-g =

breaking the sequence down

:1<enter>   – states go to the first line of the file

<shift>-v    – Turns on visual line mode which selects the entire current line

<shift>-g    -Makes the cursor go to the last line in the file and with the last command

selects the entire file

=                   – Tells the editor to reformat the selection area

This page will be updated as I figure out more useful commands. This is only a small amount of the power of VI!

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: , ,

Notepad++: Changing shortcut keys

Monday, October 3rd, 2011

I was tracking a log file the other day and had to reload the file from disk regularly so I wanted to find out how to make a shortcut key to the Reload from disk menu entry on the File menu. Using the Settings->Shortcut Mapper to update the shortcuts for a given menu entry, I chose the Ctrl-R sequence but you are welcome to find an unmapped key sequence that works for you.

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: , ,

VI: Display tabs and end of lines

Thursday, August 18th, 2011

I was working on a system the other day and needed to see where the end of lines were and where there were tabs instead of spaces. And I found the list option to vi.

This will turn on the display of “$” to indicate the end of line and “^I” to indicate a tab.

:set list

This will turn off the option to display the end of line and tabs.

:set nolist

Tags: ,

Toggle wrap in VI

Sunday, October 31st, 2010

This turns off word wrap in the vi window

:set nowrap

to enable it again:

:set wrap

Tags: ,