Blog of Leonid Mamchenkov

You just stepped in a pile of posts.

Vim for Perl developers

Posted in All on May 10th, 2004 · 34 Comments


Basic Configuration


Text wrapping

Text wrappingAs you code, sometimes text flows further than the width of the editor window. Some people like long lines to be wrapped (in other words, continued on the other line), while others prefer them unwrapped. You can configure Vim to behave according to your liking by adding “set wrap” or “set nowrap” to your .vimrc configuration file.


Flexible tabs

Tabulation is one of the hottest topics in programmers discussions when it comes to style. A very nice explanation of what is the problem with tabulations and spaces can be read at http://www.jwz.org/doc/tabs-vs-spaces.html. I must say though, that I don’t agree with author’s position on the subject.

The article does provide you with several options for vi/Vim configuration to control the behavior of the tabs. I will just add/repeat the ones I consider important.

Add “set tabstop=4” to your .vimrc to display all occurrences of Tab character (ASCII #9) as 4 spaces. This option will not change the text, it will only change the displaying of the text.

Add “set autoindent” to your .vimrc for Vim to automatically position the cursor with appropriate indentation level when you press Enter and want to write the next line of code. This is very useful when you are indenting your code in structures like loops and procedures.

Vim allows you to select a block of code and change it’s indentation level (either increase or decrease the level). Control the width of the indentation level with “set shiftwidth=4” in your .vimrc configuration file.

While you are in the command mode, Vim can complete the names of files and directories in case you want to open or save a file. In order to complete a file or a directory name you should start typing it’s name on the command prompt and then press the completion key. You can change the completion key to be almost anything you want with “set wildchar=” line in your .vimrc file.

If you need more information on this topic, read file /usr/share/doc/vim-common-6.1/docs/indent.txt.


Line numbering

Line numberingWhile Vim always displays your current position in the status line, you might also want to see all lines numbered. To do that, you will need to add “set number” to your .vimrc.

If you need more information on this topic, read file /usr/share/doc/vim-common-6.1/docs/usr_03.txt.


Syntax highlighting

Highlighting for light backgroundThe first thing any developer will notice in Vim is excellent syntax highlighting. All popular languages are supported. Syntax files for less popular are available from Vim web site. Perl is supported out of the box.

Highlighting for dark backgroundVim identifies keywords, comments, variables, strings, in-line POD and other standard parts of the program and highlights them. Vim supports two color modes: one for terminals with dark background color and another one for terminals with light background color. You can control these modes by specifying command “set bg=dark” and “set bg=light” in your .vimrc.

If you still don’t like the appearance of the code, stay tuned until “Color schemes” discussion.

If you need more information on this topic, read file /usr/share/doc/vim-common-6.1/docs/syntax.txt.


Text completion

One of Vim’s features I cannot live without is text completion. When editing, press Ctrl-n / Ctrl-p to cycle through the current word completion suggestions. Vim generates suggest list based on the words in the current file. If you need key completion to make suggestions from other files, then Vim by default understand the ctags file. Simply run “ctags *.p?” to generate ctags for all Perl files and modules in the current directory (assuming you are using “.pl” extension for Perl scripts and “.pm” extension for Perl modules). This Vim’s feature not only saves a tonne of time on typing, but greatly decreases the error rate, especially with long variable and procedure names in the code.

If you need more information on this topic, read file /usr/share/doc/vim-common-6.1/docs/usr_24.txt.


Window splitting

Split windowsSometimes I need to see two or more files simultaneously. While I can always start another Vim session in a separate terminal, it is not as comfortable at times as Vim’s window splitting feature.

Vim supports both vertical and horizontal window splitting. To split current window horizontally execute “:split” while in the normal mode. To split current window vertically, use “:vsplit” instead. If you don’t provide any filename as argument to split/vsplit then current filename is used. You further split resulting windows as much as you like. Use Ctrl-w w to cycle through the windows. “:close” will close the current windows. “:only” will close all windows except the current one.

If you need more information on this topic, read file /usr/share/doc/vim-common-6.1/docs/usr_08.txt.


Diff mode

Diff modeVim provides excellent facilities for checking and editing differences between two or more files. Execute “vimdiff main.pl main2.pl” (or “vim -d main.pl main2.pl“) from the command line to see difference made to file main.pl in file main2.pl.

Vim will show both files in vertical split window mode. When you will scroll in one window, Vim will automatically scroll another window for you. You can patch the individual differences using do and dp commands. do will get the patch from the neighbor window and apply it to the current buffer, while dp will apply the difference from the current buffer to the neighbor window.

If you need more information on this topic, read file /usr/share/doc/vim-common-6.1/docs/diff.txt.


Text folding

Before foldingAfter foldingYet another popular feature found in popular editors and IDEs is text folding. I think that folding is easier shown then explained.

Vim supports six folding methods: by indent, by expression, by syntax, by changes in the text, by marker, and manual folding. It is up to you which method to use. For the sake of the example, I will show how to configure folding by marker.

In order to tell Vim to use folding by marker add the following lines to your .vimrc:

" Folding configuration
set foldmethod=marker
" Edit and uncomment next line if you want non-default marker
"set foldmarker="{{{,}}}"

If you need more information on this topic, read files /usr/share/doc/vim-common-6.1/docs/usr_28.txt and /usr/share/doc/vim-common-6.1/docs/fold.txt.


Marks

One of Vim’s features that speeds up navigation is - marks. You can set a mark anywhere in the text and then quickly jump back to it. You can set several marks. You can set marks in several files and then quickly switch between them. I find myself set mark on the place I am currently working at with mc (m is for “mark”, c I use for “current”) and then quickly returning to the same place with 'c.

If you need more information on this topic, read file /usr/share/doc/vim-common-6.1/docs/motion.txt.


Vertical Indent Display

Vertical indentFor some people indenting text horizontally is not enough. They need some kind of vertical indentation to help them see corresponding parts of the code. While I am not one of those people, I’ve seen this feature around so often that I decided to add it codemonkey’s .vimrc.

All you need to do to have vertical indent display with pipe (”|”) character is to add the following two lines to your .vimrc.

" This is for vertical indenting
set list
set listchars=tab:|
" NOTE the space char after last backslash.

:set list” forces Vim to show hidden characters like tabulations, ends of lines, and ends of files. “:set listchars=tab:| ” asks Vim to show only tab characters and use a pipe (”|”) with a space (” “) to do so, instead of usual “^I” thing that Vim likes to show.

Pages: 1 2 3 4

Tags:

34 responses so far ↓

  • 1 oleg // May 12, 2004 at 6:26 pm

    great article. thanks!

  • 2 SR // May 13, 2004 at 10:14 pm

    Really Great Article.
    Easy to understand and with complete information and links. Thanks 8)

  • 3 Axioplase // May 14, 2004 at 12:24 am

    Yes!
    thanks you!
    Though i wouldn’t spit on a more detailed index, neither on some more blank lines, to have the reading of this article easier.

  • 4 noahpb // Jun 9, 2004 at 11:57 pm

    Very informative. Thanks for taking the time to write this up.

    It’s great to see explanations of Vim features I wasn’t aware of.

  • 5 fo0 // Jun 12, 2004 at 12:04 am

    great thanks :lol:

  • 6 HoRai // Jun 26, 2004 at 10:43 pm

    Great!
    This turns me into a vim killer! :twisted:

    I only disagree on the fact that it’s easy to browse on the vim website.
    I’ve been searching for this block commenting tip and had to read you article to succeed.

  • 7 Alex // Aug 1, 2004 at 11:00 am

    Your article was the sole reason I had the courage to start using VIM. I have never looked back. Thanks!

    Now if I could only stop hitting escape on the command line out of habit… ;)

  • 8 nothingmuch // Aug 18, 2004 at 12:32 am

    Alex - try bash’s Vi editing mode.

  • 9 jbWare // Sep 16, 2004 at 4:00 am

    Great article (++)

  • 10 Venkatraman.D // Oct 21, 2004 at 12:50 pm

    it is superb article… Whenever i thought of reading VIM , usually it will end up in mishap… but this tutorial is ultimate….

  • 11 Nicola Worthington // Oct 28, 2004 at 6:29 pm

    Can I marry you? You’re wonderful.

    **licks you for wonderful vim and perl niceness**

  • 12 Ben Prew // Jan 6, 2005 at 7:42 am

    What about adding support for automatically recognizing .t files as perl test files?

    1. create a directory called ftdetect in .vim
    2. create a file with a .vim extension (I named mine perl_test.vim)
    3. Put this line in it:
    au BufRead,BufNewFile *.t setfiletype=perl

    Now, whenever I enter a .t file, Vim assumes it a perl file.

  • 13 chocolateboy // Mar 5, 2005 at 8:52 pm

    What Nicola Worthington said + a lazier version of the above:

    au BufRead,BufNewFile *.t set syntax=perl

  • 14 Bert // Mar 8, 2005 at 6:55 pm

    How do i switch between the taglist pane and the main editor without using a mouse, is there a keyboard shortcut so i can togle between them?

  • 15 Leonid Mamchenkov // Mar 9, 2005 at 5:44 pm

    [14] The shortcut for switching between windows is "Ctrl+W W". You might want to read ":help split" and ":help CTRL-W".
  • 16 adong // Mar 19, 2005 at 2:27 pm

    ‘za’ and ‘za’ again to folding and unfolding.

  • 17 Blog of Leonid Mamchenkov » Blog Archive » Fixed ‘Vim for perl developers’ for Konqueror // Apr 12, 2005 at 10:15 pm

    [...] » Fixed ‘Vim for perl developers’ for Konqueror Vim for perl developers was terrible broken when viewed with Konqueror. I gu [...]

  • 18 Anno Siegel // May 12, 2005 at 8:39 pm

    Concerning “Perl syntax compiler”, there is an alternative that takes things from the Perl side.

    If you use Vi::QuickFix (from CPAN) somewhere, Perl will write a file errors.err for Vim. The commands “:cf”, “:cn”, etc. are immediately available.

    Anno (Author of Vi::QuickFix, in case you wonder why I care)

  • 19 모꿈ì?˜ 쿵딱쿵딱 장난ê°? 공작실 » Blog of Leonid Mamchenkov » Blog Archive » Vim for Perl developers // Jun 17, 2005 at 3:34 am

    [...] 6ì›” 17, 2005 at 9:34 am · Filed under development, perl Blog of Leonid Mamchenkov » Blog Archive » Vim for Perl developers Leonid M [...]

  • 20 Edward WIJAYA // Nov 6, 2005 at 10:37 am

    Have you considered “perl-support” by Fritz Mehner? As a more complete version from C.Keith’s.

    http://www.vim.org/script.../s.....ipt_id=556

  • 21 Leonid Mamchenkov // Nov 7, 2005 at 11:51 am

    Thanks for the link Edward. I missed that one. ;)
  • 22 骑马扇风 » vi 颜色搭配 // Dec 6, 2005 at 11:44 am

    [...] vim for perl developers http://mamchenkov.net/wor.../v.....evelopers/   [...]

  • 23 Jagadeesh // Jan 21, 2006 at 1:48 pm

    Greate help for perl developpers……..

  • 24 marc // Feb 1, 2006 at 12:24 am

    Hi, your links don’t work anymore. I’d love to try. Can you fix them?

    Marc

  • 25 Leonid Mamchenkov // Feb 1, 2006 at 12:55 am

    Hi Marc,

    Thanks for letting me know. I’ll fix them shortly. In the mean time, please use page navigation to go around. You can find page links just after the article and above the comments - they look like this “Pages: 1 2 3 4″.

  • 26 ivan // Aug 13, 2006 at 8:17 pm

    Pretty good! I just now realized the beauty of VIM. Another pico fan converted *sigh*

  • 27 naboj // Aug 29, 2006 at 1:18 am

    Very good article. This was the final reason to convert me to vim

  • 28 Luca // Oct 18, 2006 at 11:40 am

    Very useful. Thank you.

  • 29 miguel // Nov 14, 2006 at 8:32 pm

    great article.

    here are a few features that I can’t live without,

    :let &backspace=2 # when you go into insert mode you can backspace over anything even if you move the cursor around to other lines

    :au BufReadPost * if line(”‘\”") > 0 && line(”‘\”")

  • 30 Anonymous // Nov 22, 2006 at 9:00 am

    Great article, you\’ve really done a lot of hard work. Thanks for sharing.

  • 31 prozz’s blog » Blog Archive » Vim Zen // Feb 4, 2008 at 2:42 pm

    [...] Here you can find great article about Vim editing in practice. Recommended for nonbelievers. [...]

  • 32 Jagadeesh // Feb 21, 2008 at 8:02 am

    Can we have something like, on saving file [ on using :w] it should sople source code.

  • 33 Jagadeesh // Feb 21, 2008 at 8:02 am

    Can we have something like, on saving file [ on using :w] it should compile source code.

  • 34 Jagadeesh // Feb 21, 2008 at 8:14 am

    Hey,

    I got something like this. just append following line to .vimrc file

    autocmd BufWritePost *.pl !perl -c %

    when you save your perl file, syntax will be verified :)

Leave a Comment

Note: This post is over 4 years old. You may want to check later in this blog to see if there is new information relevant to your comment.

Advertisement

  • Linux Weekly News
  • 50 ways to help the planet
  • Advertise at mamchenkov.net