Vim Tips Wiki
(archive)
(→‎Previously featured tips: archive current feature)
 
(18 intermediate revisions by 4 users not shown)
Line 5: Line 5:
   
 
==Now THAT's a Good Tip!==
 
==Now THAT's a Good Tip!==
While most of the tips on this wiki are very helpful, every now and then, you'll stumble on a tip that shines above all the rest. Such a tip is a tip that changes the way you do a basic task, a tip that you try immediately and then use all the time, a tip that makes Vim integrate SO much better with your setup, or a tip that you just ''wish'' somebody gave to you when you started using Vim. We here on the Vim Tips Wiki recognize this, and therefore will–about once a month–feature a new tip that we feel is among the best of the best. So keep an eye on this page for life-altering (or at least mildly interesting) tips to add to your ''repertoire''.
+
While most of the tips on this wiki are very helpful, every now and then, you'll stumble on a tip that shines above all the rest. Such a tip is a tip that changes the way you do a basic task, a tip that you try immediately and then use all the time, a tip that makes Vim integrate SO much better with your setup, or a tip that you just ''wish'' somebody gave to you when you started using Vim. We here on the Vim Tips Wiki recognize this, and therefore will on occasion feature a new tip that we feel is among the best of the best. So keep an eye on this page for life-altering (or at least mildly interesting) tips to add to your ''repertoire''.
   
 
==Previously featured tips==
 
==Previously featured tips==
This is an archive of the ''Featured tip'' section on the [[Vim Tips Wiki|home page]]. We won't feature these tips again – however, they remain great tips!
+
This is an archive of the ''Featured tip'' section on the [[Vim Tips Wiki|home page]]. We may not feature these tips again, however they remain great tips!
   
===[[VimTip27|November 2009]]===
+
===[[VimTip630|June 2012]]===
  +
When writing a program it can be convenient if closing brackets are automatically inserted. For example, after typing <code>'''{'''</code> it can be handy if the closing <code>'''}'''</code> is added automatically.
Programmers often need to convert numbers from hexadecimal to decimal, or vice versa. You can simply enter a command like <tt>:echo&nbsp;0x1234</tt> to display the decimal equivalent of hex 1234, or enter <tt>:echo&nbsp;printf('%x',1234)</tt> to display the hex equivalent of decimal 1234.
 
   
  +
Our [[VimTip630|featured tip]] discusses solutions for this problem: some simple mappings are provided and their shortcomings outlined. More advanced solutions require a plugin, and a list of scripts for the automatic insertion of matching brackets is provided.
For more serious converting, our user-defined command <tt>:Dec2hex</tt> converts all decimal numbers in a selected area to hex, and <tt>:Hex2dec</tt> does the reverse.
 
   
 
===[[VimTip1662|June 2011]]===
Our [[VimTip27|featured tip]] has the details, as well as links to tips showing how to display or edit binary files in hex. A method to convert the current line to or from percent-encoded hex is also provided.
 
  +
Vim can work with all the major [[File format|line-ending formats]] but sometimes does not load a file in the format you want. Usually this is because a different text editor saved a file with mixed line-ending styles. It can be very annoying to load a file in Vim, which reads in with a Unix file format, when only a few lines lack this carriage return. It is worse when you cannot modify it because of strict revision control.
   
  +
Our [[VimTip1662|featured tip]] automatically reloads such files using DOS format if any DOS-style line endings are found. This will automatically "correct" the file if you save it, so an enhanced version will also tell you how many line endings are in each style so you can decide for yourself which format to use on a save.
===[[VimTip2|October 2009]]===
 
Perhaps you are editing several files in different directories, and now you want to open another file in the same directory as your current file. Problem: the file is deep in the directory tree, and navigating to find the file would be frustrating.
 
   
 
===[[VimTip246|April 2011]]===
Try our command line abbreviation to invoke <tt>expand('%:p:h')</tt> so you can easily insert the full path of the directory of the current file into the command line. Then you can press Enter to browse the directory, or use Tab for file name completion.
 
  +
For a long time now, Vim has had the ability to work with files in a very large set of [[wikipedia:Character encoding|character encodings]]. Using a character encoding with a very large range of characters, such as the Unicode encodings, can provide many benefits to any Vim user. Whether you need to work with multiple languages, or just want to use fancy box-drawing characters and arrows in your 'listchars' option or a plugin, using a Unicode encoding can make editing in Vim even more pleasant than it already is.
   
  +
There are a lot of options that impact encoding in Vim, and it can be intimidating to someone not familiar with them. (And who really is?) Our [[VimTip246|featured tip]] discusses the various options you will need to start using Unicode successfully. Use the included example as a starting point or a drop-in addition to your [[vimrc]] to start editing in Unicode today!
Our [[VimTip2|featured tip]] has the details, as well as an alternative to [[VimTip64|set the working directory to the current file]].
 
   
===[[VimTip1183|September 2009]]===
+
===[[VimTip1369|February 2011]]===
  +
Vim is made to work well with other tools, which means that whenever Vim saves a file, it correctly adds a line ending of the [[File Format|correct format for your system]] to the end of every line. When the file already existed, this is usually done whether the original file had a line ending on the final line or not. Nevertheless, Vim does keep track of whether the file originally had a line ending on the last line in the {{help|prefix=no|'eol'}} option, so that when writing binary files Vim does not introduce any new bytes.
Have you ever used Caps Lock in insert mode, then switched back to normal mode to issue a few commands, completely forgetting that Caps Lock is still on? If entering <tt><Esc>JJJJUUU</tt> nonsense commands sounds familiar, you need to try our [[VimTip1183|featured tip]].
 
   
  +
Our [[VimTip1369|featured tip]] takes advantage of this 'eol' option, to preserve the missing end-of-line on the last line of files which are read in without one.
By implementing language maps (or by using a keymap), you can take advantage of Vim's ability to translate characters after pressing Ctrl-<tt>^</tt> while in insert mode. The "Caps Lock" provided by this technique applies only to entered text, not to normal-mode commands.
 
   
  +
This can be useful if you work on a project with very strict policies against needless changes to files, or which specifically require no EOL on the final line.
===[[VimTip42|August 2009]]===
 
A '''mark''' (aka bookmark) can be used to record your current position, so you can return to it later. You probably know that <tt>ma</tt> sets mark '''a''', and that later you can type <tt>`a</tt> to jump to the '''a''' location. But what about file marks, and what about special marks?
 
   
 
===[[VimTip1643|January 2011]]===
There are lots of questions to ask about marks, and our [[VimTip42|featured tip]] has all the answers.
 
  +
The ability to undo mistakes and redo them if you change your mind is a basic feature of any advanced editing program. Vim expands on this, keeping track of ''every'' change made in one or more "undo branches", so that you can return to any state your buffer was in even after undoing those changes and making different changes. Vim 7.3 adds the "persistent undo" feature, which saves this undo tree between editing sessions to make it even more powerful.
   
  +
Our [[VimTip1643|featured tip]] will introduce you to Vim's undo tree, and even show you some plugins that make it simple to use.
===[[VimTip857|July 2009]]===
 
Syntax highlighting helps show the structure of a program, and usually all you need is <tt>:syntax&nbsp;on</tt> – the rest is automatic.
 
 
However, what if you have some C code in a text file, or some SQL code in a C file? With a little work, you can apply different syntax highlighting rules to different regions of a file, so your C code looks like C, your text looks like text, and your SQL code looks like SQL.
 
 
Our [[VimTip857|featured tip]] has the details. Use it to create syntax regions in plaintext, tweak existing syntax rules, or even as a guide when developing a new syntax plugin!
 
 
===[[VimTip1300|June 2009]]===
 
After another editing frenzy, you might wonder exactly where you made changes. Of course (if you have an old copy of the file) you can use <tt>vimdiff</tt> to see exactly what changes have occurred. Even with no original file, you can see ''where'' each change occurred with the <tt>:changes</tt> command.
 
 
Even more useful is the fact that you can return to locations where changes occurred, even those from previous editing sessions.
 
 
===[[VimTip1611|May 2009]]===
 
It's time to go ''back to basics'' and check how line ranges work. You may know that <tt>:s/old/new/g</tt> substitutes in the current line, and <tt>:%s/old/new/g</tt> substitutes in the whole file, but it's worth learning more!
 
 
Ranges like <tt>.+1,$</tt> (all lines after current line) and <tt>'<,'></tt> (last visually selected lines) are very useful.
 
 
===[[VimTip478|April 2009]]===
 
What can you do with all those search hits? You can simply list them with <tt>:g/pattern/</tt>, or you can make a clickable list with <tt>:vimgrep&nbsp;/pattern/&nbsp;%</tt>.
 
 
Our [[VimTip478|featured tip]] shows these, and has some techniques to copy the search results to the clipboard.
 
 
===[[VimTip171|March 2009]]===
 
You can search by typing <tt>/</tt> followed by the search pattern, or by pressing <tt>*</tt> to [[VimTip1|search for the current word]].
 
 
With [[VimTip171|our featured tip]], you can also select some text, then press <tt>*</tt> to search for the next occurrence of the text. The selection can be a few characters, or a few lines. Whitespace in the selection matches any whitespace when searching (searches will find words separated by any combination of spaces, tabs and line breaks).
 
 
===[[VimTip1577|February 2009]]===
 
Do you ever do some edits in one place, knowing that you're going to have to repeat those edits in several other places? Naturally Vim allows complex repeats by recording keys into a register. {{help|complex-repeat}}
 
 
Now you can try the magic of a [[VimTip1577|recursive repeat]] to automatically apply the commands you have recorded to many lines at a time. By following our technique for recording to a register, and finishing by invoking the register, you can apply a macro to every line in a file with no counts necessary!
 
 
===[[VimTip1572|January 2009]]===
 
Sometimes it's useful to highlight several patterns in different colors. For example, you may be studying how three variables are used, and you would like to see each displayed with a different background color.
 
 
It's easy to [[VimTip1572|highlight multiple words]] using our amazing highlighting script. You can press a key to highlight visually-selected text, or the current word, or you can enter a command to highlight any pattern. Searching for the next highlight is also easy.
 
 
You can have different sets of highlighting in different windows, or can copy highlighting from one window to another. If you really like your current highlight patterns, you can save them for use in the future.
 
   
 
==Previous years==
 
==Previous years==
  +
*[[/2010|2010 ''Featured tips'']]
  +
*[[/2009|2009 ''Featured tips'']]
 
*[[/2008|2008 ''Featured tips'']]
 
*[[/2008|2008 ''Featured tips'']]
   

Latest revision as of 03:49, 7 November 2013

  • Please discuss proposals for new Featured Tips on the talk page.
  • The home page highlights the current featured tip.
  • Add comments on the current featured tip to the comments section on the current tip page.

Now THAT's a Good Tip![]

While most of the tips on this wiki are very helpful, every now and then, you'll stumble on a tip that shines above all the rest. Such a tip is a tip that changes the way you do a basic task, a tip that you try immediately and then use all the time, a tip that makes Vim integrate SO much better with your setup, or a tip that you just wish somebody gave to you when you started using Vim. We here on the Vim Tips Wiki recognize this, and therefore will on occasion feature a new tip that we feel is among the best of the best. So keep an eye on this page for life-altering (or at least mildly interesting) tips to add to your repertoire.

Previously featured tips[]

This is an archive of the Featured tip section on the home page. We may not feature these tips again, however they remain great tips!

June 2012[]

When writing a program it can be convenient if closing brackets are automatically inserted. For example, after typing { it can be handy if the closing } is added automatically.

Our featured tip discusses solutions for this problem: some simple mappings are provided and their shortcomings outlined. More advanced solutions require a plugin, and a list of scripts for the automatic insertion of matching brackets is provided.

June 2011[]

Vim can work with all the major line-ending formats but sometimes does not load a file in the format you want. Usually this is because a different text editor saved a file with mixed line-ending styles. It can be very annoying to load a file in Vim, which reads in with a Unix file format, when only a few lines lack this carriage return. It is worse when you cannot modify it because of strict revision control.

Our featured tip automatically reloads such files using DOS format if any DOS-style line endings are found. This will automatically "correct" the file if you save it, so an enhanced version will also tell you how many line endings are in each style so you can decide for yourself which format to use on a save.

April 2011[]

For a long time now, Vim has had the ability to work with files in a very large set of character encodings. Using a character encoding with a very large range of characters, such as the Unicode encodings, can provide many benefits to any Vim user. Whether you need to work with multiple languages, or just want to use fancy box-drawing characters and arrows in your 'listchars' option or a plugin, using a Unicode encoding can make editing in Vim even more pleasant than it already is.

There are a lot of options that impact encoding in Vim, and it can be intimidating to someone not familiar with them. (And who really is?) Our featured tip discusses the various options you will need to start using Unicode successfully. Use the included example as a starting point or a drop-in addition to your vimrc to start editing in Unicode today!

February 2011[]

Vim is made to work well with other tools, which means that whenever Vim saves a file, it correctly adds a line ending of the correct format for your system to the end of every line. When the file already existed, this is usually done whether the original file had a line ending on the final line or not. Nevertheless, Vim does keep track of whether the file originally had a line ending on the last line in the 'eol' option, so that when writing binary files Vim does not introduce any new bytes.

Our featured tip takes advantage of this 'eol' option, to preserve the missing end-of-line on the last line of files which are read in without one.

This can be useful if you work on a project with very strict policies against needless changes to files, or which specifically require no EOL on the final line.

January 2011[]

The ability to undo mistakes and redo them if you change your mind is a basic feature of any advanced editing program. Vim expands on this, keeping track of every change made in one or more "undo branches", so that you can return to any state your buffer was in even after undoing those changes and making different changes. Vim 7.3 adds the "persistent undo" feature, which saves this undo tree between editing sessions to make it even more powerful.

Our featured tip will introduce you to Vim's undo tree, and even show you some plugins that make it simple to use.

Previous years[]

Comments[]