+ Reply to Thread
Results 1 to 2 of 2

Thread: Vim hacks

  1. #1
    DarkLordoftheMonkeys's Avatar
    DarkLordoftheMonkeys is offline Programming Professional
    Join Date
    Oct 2009
    Location
    Massachussets
    Posts
    255
    Blog Entries
    56
    Rep Power
    11

    Vim hacks

    Hack #1: The Vim startup file

    The Vim startup file is a script, written in Vimscript, that you put in your home directory (~). The script is executed every time Vim starts. It is usually used to customize Vim by specifying the default settings. The startup file must have the filename _vimrc. Here is the startup script that I use:

    Code:
    " My Vim setup script
    set number
    set autoindent
    set nowrap
    set ruler
    set noignorecase
    set encoding=utf-8
    syntax on
    " Use :TOhtml
    let html_use_css = 1

    Hack #2: Incrementing and decrementing numbers

    There are two keystrokes for changing the value of the number under the cursor. Typing Ctrl+A increments the number. Typing Ctrl+X decrements the number. When you do this, the entire number is changed, not just the digit under the cursor.


    Hack #3: Bookmarking parts of a document

    Bookmarking helps you to move around a document more quickly by marking a place to return to later. Marks are given letters that you can type to return to them. Typing m followed by a letter marks the character under the cursor. Here are the keystrokes:

    mx - mark the character under the cursor with mark x
    'x - return to the line marked by x
    `x - return to the character marked by x
    '' - return to the line the cursor was on before the last jump
    `` - return to the character that was under the cursor before the last jump
    <start bracket>' - go back one mark
    <end bracket>' - go forward one mark


    Hack #4: Creating macros

    Macros are sequences of keystrokes that can be mapped to one key. When that key is pressed, all of the keystrokes mapped to that key are executed. It is generally best to use keys that are not used elsewhere for mapping, such as k, K, and =.

    :map = sequence - map a sequence to =
    :unmap = - remove mapping for =
    :map! = sequence or :imap = sequence - map a sequence to = for insert mode
    :unmap! = or :iunmap = - remove mapping for = in insert mode
    :map - show mappings


    Hack #5: Command history

    Vim has a command history that you can search to avoid having to type long ex commands multiple times. It even saves commands from previous sessions. The command history can be accessed using the keystroke q: (q followed by a colon). This will bring up a list of commands at the bottom of the screen. You can scroll through and edit commands in the command history just like you would in the document. When you find the command you want, simply hit enter to enter the command.


    Hack #6: Syntax highlighting

    Vim color codes, or highlights code, making it easier to spot mistakes. Syntax highlighting can be turned on by typing :syntax on, and turned off by typing :syntax off. Vim provides syntax highlighting for several languages, including:

    C
    C++
    HTML
    CSS
    Javascript
    PHP
    SQL
    BASIC
    DOS Batch
    Shell scripts
    Perl
    Ruby
    Python
    Java
    Fortran
    Pascal
    Ada
    Lisp
    Vimscript
    sed
    AWK
    Assembly language
    Objective-C
    XML
    DTD
    XSLT

    And probably many more.


    Hack #7: Save syntax highlighting to HTML

    The ex command :TOhtml creates an HTML file that contains the highlighted text of a document. The default is to use font tags to color code the text, but you can set it to use CSS instead by typing :let html_use_css = 1 first.


    Hack #8: Get the ASCII or Unicode value of a character

    There are two ways to get the code value of the character under the cursor. To get the ASCII value, type :ascii at the ex prompt. There is a keystroke you can use to get the Unicode value. When the character is under the cursor, type ga. Make sure that encoding is set to utf-8 first.


    Hack #9: Tools for multiwindow editing

    The keystroke Ctrl+W is always used for multiwindow editing commands. There are many commands you can use, but here are a few of them:

    ^Ws - split the window horizontally
    ^Wv - split the window vertically
    ^W^W - go to the next window
    ^W+ - increase the current window's height by the preceding number of lines
    ^W- - decrease the current window's height by the preceding number of lines
    ^W> - increase the current window's width by the preceding number of columns
    ^W< - decrease the current window's width by the preceding number of columns
    ^W_ - set the current window's height to the preceding number of lines
    ^W| - set the current window's width to the preceding number of columns
    ^Wr - rotate windows


    Hack #10: Toggle the case of a character

    To toggle the case of a character, type ~ when the cursor is over that character. Vim will change the case and then move to the next character. This also allows you to change the case of a group of characters very quickly by holding down the tilde key.


    Hack #11: Show marks and buffers

    :display - show all character buffers and their contents.

    :marks - show all markers and a section of the text surrounding the characters they mark


    Hack #12: Explore, Sexplore, and Vexplore

    Explore, Sexplore, and Vexplore (standing for Split-explore and Vertical-explore, respectively), are part of a plugin that allows you to explore and manipulate the filesystem from within Vim. Typing :Explore shows the contents of the current directory in the current window. Vexplore and Sexplore split the window and show the filesystem in the new window. From this new window you can navigate through the filesystem and also create and delete files and directories.
    Life's too short to be cool. Be a nerd.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Vim hacks

    Nice tidbits. Currently, gVim is the only editor that I consider replacing jEdit with. I guess I just haven't forced myself to use it enough, yet. +rep
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 1
    Last Post: 09-20-2010, 08:46 PM
  2. bash hacks
    By DarkLordoftheMonkeys in forum Linux Tutorials, Guides and Tips
    Replies: 3
    Last Post: 12-03-2009, 01:37 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts