Wikia

Vim Tips Wiki

Changes: Vim Tips Wiki:Featured Tip/2009

Edit

Back to page

(2009 archive)
 
(Change <tt> to <code>, perhaps also minor tweak.)
 
Line 2: Line 2:
   
 
===[[VimTip1553|December 2009]]===
 
===[[VimTip1553|December 2009]]===
One of Vim's great features is the ability to press <tt>J</tt> to join lines. But joining lines of comments in a program is still a hassle because you need to manually remove comment leaders.
+
One of Vim's great features is the ability to press <code>J</code> to join lines. But joining lines of comments in a program is still a hassle because you need to manually remove comment leaders.
   
Our [[VimTip1553|featured tip]] shows how to map <tt>J</tt> to join comment lines. More importantly for Vimmers interested in learning scripting techniques, the tip has many explanations showing how the result is achieved while handling normal mode and visual mode.
+
Our [[VimTip1553|featured tip]] shows how to map <code>J</code> to join comment lines. More importantly for Vimmers interested in learning scripting techniques, the tip has many explanations showing how the result is achieved while handling normal mode and visual mode.
   
 
===[[VimTip27|November 2009]]===
 
===[[VimTip27|November 2009]]===
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.
+
Programmers often need to convert numbers from hexadecimal to decimal, or vice versa. You can simply enter a command like <code>:echo 0x1234</code> to display the decimal equivalent of hex 1234, or enter <code>:echo printf('%x',1234)</code> to display the hex equivalent of decimal 1234.
   
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.
+
For more serious converting, our user-defined command <code>:Dec2hex</code> converts all decimal numbers in a selected area to hex, and <code>:Hex2dec</code> does the reverse.
   
 
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.
 
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.
Line 16: Line 16:
 
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.
 
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.
   
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.
+
Try our command line abbreviation to invoke <code>expand('%:p:h')</code> 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.
   
 
Our [[VimTip2|featured tip]] has the details, as well as an alternative to [[VimTip64|set the working directory to the current file]].
 
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]]===
 
===[[VimTip1183|September 2009]]===
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]].
+
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 <code><Esc>JJJJUUU</code> nonsense commands sounds familiar, you need to try our [[VimTip1183|featured tip]].
   
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.
+
By implementing language maps (or by using a keymap), you can take advantage of Vim's ability to translate characters after pressing Ctrl-<code>^</code> while in insert mode. The "Caps Lock" provided by this technique applies only to entered text, not to normal-mode commands.
   
 
===[[VimTip42|August 2009]]===
 
===[[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?
+
A '''mark''' (aka bookmark) can be used to record your current position, so you can return to it later. You probably know that <code>ma</code> sets mark '''a''', and that later you can type <code>`a</code> to jump to the '''a''' location. But what about file marks, and what about special marks?
   
 
There are lots of questions to ask about marks, and our [[VimTip42|featured tip]] has all the answers.
 
There are lots of questions to ask about marks, and our [[VimTip42|featured tip]] has all the answers.
   
 
===[[VimTip857|July 2009]]===
 
===[[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.
+
Syntax highlighting helps show the structure of a program, and usually all you need is <code>:syntax on</code> – 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.
 
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.
Line 38: Line 38:
   
 
===[[VimTip1300|June 2009]]===
 
===[[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.
+
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 <code>vimdiff</code> to see exactly what changes have occurred. Even with no original file, you can see ''where'' each change occurred with the <code>:changes</code> command.
   
 
Even more useful is the fact that you can return to locations where changes occurred, even those from previous editing sessions.
 
Even more useful is the fact that you can return to locations where changes occurred, even those from previous editing sessions.
   
 
===[[VimTip1611|May 2009]]===
 
===[[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!
+
It's time to go ''back to basics'' and check how line ranges work. You may know that <code>:s/old/new/g</code> substitutes in the current line, and <code>:%s/old/new/g</code> 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.
+
Ranges like <code>.+1,$</code> (all lines after current line) and <code>'<,'></code> (last visually selected lines) are very useful.
   
 
===[[VimTip478|April 2009]]===
 
===[[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>.
+
What can you do with all those search hits? You can simply list them with <code>:g/pattern/</code>, or you can make a clickable list with <code>:vimgrep /pattern/ %</code>.
   
 
Our [[VimTip478|featured tip]] shows these, and has some techniques to copy the search results to the clipboard.
 
Our [[VimTip478|featured tip]] shows these, and has some techniques to copy the search results to the clipboard.
   
 
===[[VimTip171|March 2009]]===
 
===[[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]].
+
You can search by typing <code>/</code> followed by the search pattern, or by pressing <code>*</code> 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).
+
With [[VimTip171|our featured tip]], you can also select some text, then press <code>*</code> 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]]===
 
===[[VimTip1577|February 2009]]===

Latest revision as of 08:33, July 15, 2012

This is an archive of the Featured tip section on the Main Page for 2009.

December 2009Edit

One of Vim's great features is the ability to press J to join lines. But joining lines of comments in a program is still a hassle because you need to manually remove comment leaders.

Our featured tip shows how to map J to join comment lines. More importantly for Vimmers interested in learning scripting techniques, the tip has many explanations showing how the result is achieved while handling normal mode and visual mode.

November 2009Edit

Programmers often need to convert numbers from hexadecimal to decimal, or vice versa. You can simply enter a command like :echo 0x1234 to display the decimal equivalent of hex 1234, or enter :echo printf('%x',1234) to display the hex equivalent of decimal 1234.

For more serious converting, our user-defined command :Dec2hex converts all decimal numbers in a selected area to hex, and :Hex2dec does the reverse.

Our 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.

October 2009Edit

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.

Try our command line abbreviation to invoke expand('%:p:h') 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.

Our featured tip has the details, as well as an alternative to set the working directory to the current file.

September 2009Edit

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 <Esc>JJJJUUU nonsense commands sounds familiar, you need to try our featured tip.

By implementing language maps (or by using a keymap), you can take advantage of Vim's ability to translate characters after pressing Ctrl-^ while in insert mode. The "Caps Lock" provided by this technique applies only to entered text, not to normal-mode commands.

August 2009Edit

A mark (aka bookmark) can be used to record your current position, so you can return to it later. You probably know that ma sets mark a, and that later you can type `a to jump to the a location. But what about file marks, and what about special marks?

There are lots of questions to ask about marks, and our featured tip has all the answers.

July 2009Edit

Syntax highlighting helps show the structure of a program, and usually all you need is :syntax on – 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 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!

June 2009Edit

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 vimdiff to see exactly what changes have occurred. Even with no original file, you can see where each change occurred with the :changes command.

Even more useful is the fact that you can return to locations where changes occurred, even those from previous editing sessions.

May 2009Edit

It's time to go back to basics and check how line ranges work. You may know that :s/old/new/g substitutes in the current line, and :%s/old/new/g substitutes in the whole file, but it's worth learning more!

Ranges like .+1,$ (all lines after current line) and '<,'> (last visually selected lines) are very useful.

April 2009Edit

What can you do with all those search hits? You can simply list them with :g/pattern/, or you can make a clickable list with :vimgrep /pattern/ %.

Our featured tip shows these, and has some techniques to copy the search results to the clipboard.

March 2009Edit

You can search by typing / followed by the search pattern, or by pressing * to search for the current word.

With our featured tip, you can also select some text, then press * 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).

February 2009Edit

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 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!

January 2009Edit

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 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.

CommentsEdit

Around Wikia's network

Random Wiki