Monday, August 28, 2017

VI editor for windows admins and its benefits for preformatting data

As someone with some linux background, I ended up using VI/VIM as my go to editor for any linux text editing.  While its a powerful, and some may say complicated, piece of software, if you learn a few of the basic commands and tricks it will be a very beneficial tool.  For windows, there are versions available, such as gvim.  I often find myself going back to it as a nice text editing tool when I receive text that needs some transformation work prior to using it in a script or some other data tool.

Let me give a few examples.

Example #1, you need a csv list of entries, however you have received a list with one item per line.


  1. Copy the text from the original source
  2. open gvim
  3. hit the insert key, to enter insert mode
  4. paste from the clip board
  5. hit esc, to enter command mode
  6. type (including the colon):     :1,$s/\n/\,/
  7. go to the very end of that line and remove the extra comma, ensure its highlighted, then type dl
  8. go to the edit menu, select all and copy
  9. paste it into whatever app you needed it in csv format

Let me explain a bit from the example above.  For most people when they think of text editors you are always in a mode where you are editing the text, and any special command will be a menu item or a keyboard shortcut of a combination of a special key (ctrl, alt, etc) and a letter or number.  VI uses different modes of operations, the two highlighted here are Command (this is what you start it when vi opens), and Insert (this is one of the text editing modes).  While in command mode, there are keyboard shortcuts for moving and editing text based on the cursor position, like in step number 7.  The d, followed by another modifier (lower case L in this example) says delete, in the direction specified (L) which deletes the highlighted character and moves left.  These same direction keys used in the delete command can be used on their own to move the cursor around.  Numbers can be put in front of them to make the move farther.  Another useful operation combined with the delete option is using the W key, this specifies word.  So when combined with the delete (typing:  dw) it deletes from the position of the cursor to the end of the current word.  You can again combine that with numbers, such as d3w, to delete from the current position 3 words to the right.

Now let me explain step #6 and the strange code in there.  When in command mode not all commands are executed by directly typing letters and numbers, sometimes the command needs to be entered from a special prompt, which is brought up by typing the colon.  So in the beginning of that text, the colon brings up this special prompt, and the next 2 parts of the command are a range.  1,$ means from line 1 to the end of the document.  The s/// command is substitution with regex support.  s/ starts the substitute command, the first area between the forward slashes is a regex of what you want to subtitute, and the next area between the forward slashes is the text you want to change it with.  Since its regex, you may need to use lots of backslashes for escaping text.  In this example  /\n/\,/ means match a newline (\n) and replace it with a comma.  Hit enter aft er that and it will execute.  If you are doing multiple matches on a single line, putting a g at the end, such as s///g switches to global match mode.  This command combined with regex is very powerful, and can be used to rearrange text by subset matching.  For cleaning up text, substitute is one of the main go to functions, so doing some research on that and regex's will make it incredibly powerful for you.

One more thing I want to highlight, in case you are using vim without a gui menu, saving a file is :w in command mode, and to exit vim :q.  As with other commands, these can be combined as :wq save and quit.

Insert mode is much more like your standard text editing experience where you move around and edit text.  When working in linux/unix shells, sometimes the arrow keys or backspace key may not work exactly as expected and instead throws some wierd codes into your text.  So if you end up in that situation, learn a bit about the movement, delete and replace commands in command mode.

In the future I may add some additional examples as I come across them.