Vim

From 43FoldersWiki

(Difference between revisions)
Jump to: navigation, search
Revision as of 17:07, 19 April 2005 (edit)
Gochess (Talk | contribs)
(Links)
← Previous diff
Revision as of 22:07, 19 April 2005 (edit) (undo)
ThePolack (Talk | contribs)
(added tips for dealing with files that use the wrong text end-of-line format (Mac format or Windows format))
Next diff →
Line 27: Line 27:
responds to queries. --[[User:Gochess|gochess]] 13:07, 19 Apr 2005 (EDT) responds to queries. --[[User:Gochess|gochess]] 13:07, 19 Apr 2005 (EDT)
 +=== Editing files that use non-Unix end-of-line characters ===
 +Sometimes, you open a text file and the line formatting is all wrong and there are loads of annoying '^M' characters spread all throughout it. If you're using vim on a Mac, you are likely to encounter this problem a lot because it is caused when a Mac-formatted text file is opened in vim because vim prefers text files that are Unix-formatted. You could also run into similar problems with Windows-formatted text files. Luckily, this is an easy thing to deal with in vim.
 +
 +Solution #1: Type <tt>:set fileformat unix</tt> and hit enter while in edit (not insert) mode. This should reformat the file for you and solve the problem.
 +
 +Solution #2: Sometimes solution #1 doesn't work. So I don't personally use it. Instead I use global search and replace to fix my files. While in edit mode, type <tt>:%s/\r/\r/g</tt> and hit enter. This will replace all Mac-formatted carriage returns with unix-formatted newlines. This seems counter-intuitive since you're telling the editor to replace carriage returns with carriage returns, but it works because of how vim interprets a carriage return command. Using the same fix to change a Windows file to a Unix file would require a different pattern, like this: <tt>:%s/^M//g</tt> (be sure to use ctrl-M when typing the ^M in the pattern, don't type a '^' character followed by a 'M' character).
 +
 +If typing that pattern in every time you need it seems a bit much, just map it to a key command. For example, put this in your .vimrc file in your home directory: <tt>nnoremap M :%s/\r/\r/g <CR></tt>
 +
 +Now the command to convert Mac text files into Unix text files is mapped to the upper-case 'M' in edit mode, so you can just type that command the instant you load the file and see that it's broken.
[[Category: Cross-Platform Software]] [[Category:Text Editors]] [[Category: Open Source]] [[Category: Cross-Platform Software]] [[Category:Text Editors]] [[Category: Open Source]]

Revision as of 22:07, 19 April 2005

Vim, or "vi improved" is a text editor by Bram Moolenaar. It is an enhanced version of "the unix text editor," vi, which is in turn based on ed and ex.

Links


Tips for mastering vim

Vim is a free cross-platform (*nix, Mac, Windows) text editor with amazing depth, adaptable to the way you work. In a nutshell, one can swiftly edit CONTENT (as opposed to the CONTAINER, cf. M$ Word). The Linux Journal magazine in its annual Readers' Choice Award selected vim as favorite text editor -- four years in a row!

Some of the best TIPS are found at sourceforge.net. The vim community is very friendly and helpful. Their archived forum is publicly accessible via Yahoo group for Q&A and RSS access. (Before posting a message you must subscribe to the list by sending a message to vim-subscribe@vim.org .) Bram Moolenaar, the chief programmer par excellence, often responds to queries. --gochess 13:07, 19 Apr 2005 (EDT)

Editing files that use non-Unix end-of-line characters

Sometimes, you open a text file and the line formatting is all wrong and there are loads of annoying '^M' characters spread all throughout it. If you're using vim on a Mac, you are likely to encounter this problem a lot because it is caused when a Mac-formatted text file is opened in vim because vim prefers text files that are Unix-formatted. You could also run into similar problems with Windows-formatted text files. Luckily, this is an easy thing to deal with in vim.

Solution #1: Type :set fileformat unix and hit enter while in edit (not insert) mode. This should reformat the file for you and solve the problem.

Solution #2: Sometimes solution #1 doesn't work. So I don't personally use it. Instead I use global search and replace to fix my files. While in edit mode, type :%s/\r/\r/g and hit enter. This will replace all Mac-formatted carriage returns with unix-formatted newlines. This seems counter-intuitive since you're telling the editor to replace carriage returns with carriage returns, but it works because of how vim interprets a carriage return command. Using the same fix to change a Windows file to a Unix file would require a different pattern, like this: :%s/^M//g (be sure to use ctrl-M when typing the ^M in the pattern, don't type a '^' character followed by a 'M' character).

If typing that pattern in every time you need it seems a bit much, just map it to a key command. For example, put this in your .vimrc file in your home directory: nnoremap M :%s/\r/\r/g <CR>

Now the command to convert Mac text files into Unix text files is mapped to the upper-case 'M' in edit mode, so you can just type that command the instant you load the file and see that it's broken.

Personal tools