Thread: VIM: The (un)official thread

  1. #1
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300

    Thumbs up VIM: The (un)official thread

    I noticed in this other thread a lot of people of people mentioned vim, and usage problems with vim. I'm not going to do a complete review, but I will say I really believe it is the greatest piece of software of all time.

    It's also the most poorly documented, so what I did want to do was start a new thread here in the "Product Review" board where people could ask stuff like "How can I see the value of a defined variable?" (the answer is here; "[i" works in escape mode)

    I'm sure there are some other programming software products around that could have an even more popular thread, but I'm not going to start any of them

    Here's the wikipedia entry for the curious:
    http://en.wikipedia.org/wiki/Vim_(text_editor)
    And the actual homepage is welcome home : vim online.

    I recommend against the use of the GUI. There is nothing wrong with it, but it is unnecessary and the functionality cannot be improved upon with tradition pull down menus and tabs (learn to use a keyboard, you are a computer programmer!). Plus, there is nothing like being able to use your absolute favourite tool even when you don't have a graphical desktop, and not have to remember how to get things done without a mouse to click and point things at. If you are new to the whole concept, two commands which work in escape mode that you might want to get to know are
    :split
    This splits the window in half horizontally, and if you include a filename loads it. You can also split the window unevenly (try ":help split"). To move back and forth, use ctrl-ww.
    :e #
    This will reload the last file, handy for flipping back and forth.

    Vim is standard on almost all unix derived operating systems including linux, and available for all of them (including MAC OS). There is even a windows and DOS version. However, the package included with your linux distro probably leaves a lot to be desired. It is easy to compile from source, however (choose the "huge" option to get the most functionality). A quick test of your installation is to try "vim --remote": you should be able to type:
    vim --remote mycode.c
    to send a file to the currently running (or first) instance of vim in any terminal anywhere on your desktop. This is very handy since you can add this command to your filebrowser, and just have one fullscreen, non-GUI vim in a terminal somewhere. If it doesn't work, your install doesn't have all the vim features and you should uninstall it and build from the source.

    Here's a simple ~/.vimrc that demonstrates some basic and essential configuration (the double quote " starts a comment):
    Code:
    " .vimrc
    " syntax highlighting, line numbering, etc on by default
    :syntax enable
    set number
    set nowrap
    set bs=indent,eol,start         " allow backspacing over everything in insert mode
    
    " you probably want all the syntax colors to work in your terminal
    " here's how to set them for contrast
    " numbers and color names are interchangable
    hi Statement ctermfg=7
    hi LineNr ctermbg=0 ctermfg=4
    hi Comment ctermbg=7 ctermfg=darkblue
    hi PreProc cterm=bold
    hi Type ctermfg=6
    hi Identifier ctermfg=2
    hi Number cterm=underline ctermfg=6
    hi Conditional cterm=bold ctermfg=3
    hi Repeat cterm=bold ctermfg=1
    hi Constant ctermfg=3
    hi StatusLine cterm=bold ctermfg=green ctermbg=darkmagenta
    hi StatusLineNC ctermfg=darkgray ctermbg=white
    
    " key bindings (these just print things in appropriate places to save typing)
    " cmap works in escape (aka command) mode, eg, for when loading a file
    :cmap <F2> /root/code/
    " imap works in insert (aka editing) mode
    :imap <F2> printf("\n"); fflush(stdout);
    :imap <F3> 12345678901234567890
    " that last one is for counting string lengths...
    If you have any questions about using vim, or some reason why it is not the greatest piece of software of all time, post those here
    Last edited by MK27; 04-24-2009 at 09:57 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    > I recommend against the use of the GUI.
    Going against what the authors recommend? Why? They suggest gvim because of the 256 native colour support, along with various other things (encoding, the terminal doesn't get in the way etc).

    I'd suggest using gvim and turning off the menu / toolbar. That way it's exactly the same if you SSH into another box and cannot pipe X (who would anyway?). And it allows you to enjoy 256 colours in the mean time.

    Otherwise, +1 for spreading vi/m. And down with Emacs!
    Last edited by zacs7; 04-24-2009 at 09:48 AM.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I wouldn't use it just for the colors, 16 seems enough for text (there's not that many syntax categories anyway), and you can make those 16 colors any combination of millions via the terminal (at least, most modern X terminals -- which also allow tabbing).

    Also, I really like a transparent background, which you can't get with the GUI. You know, no window borders, all see-thru: that classic linux power user look. Regarding "the terminal getting in the way", you may have to short circuit the esc key shortcut if there is one, but that's a fairly simple point and click task witht the terminal interface.

    I guess I should have added: Just don't use xterm!

    Emacs is definitely a creepy beast. But I never got into it.
    Last edited by MK27; 04-24-2009 at 10:49 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    1
    On large documents I've noticed a significant performance hit with gvim vs terminal vim when interacting with the mouse on blocks of text, and with scrolling. The extra colors can look really sharp, true, but as far as I can see the only true benefit to gvim is integration with system open/save dialogs -- and NERDTree is a great replacement for that even.

    Terminal Vim has tabs, splitscreen, and everything else. You can even turn on mouse control and be able to select dynamically, point/click to locate the cursor, resize the split windows, click an 'X' to close a tab, etc.

    It seems like every day I learn something new or a new way to do something in Vim. Programming auto-complete dropdowns, using ctags and taglist, etc.

    I don't know about best, but Vim is certainly in the upper echelon of best applications of all time.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by birnam View Post
    It seems like every day I learn something new or a new way to do something in Vim. Programming auto-complete dropdowns, using ctags and taglist, etc.

    I don't know about best, but Vim is certainly in the upper echelon of best applications of all time.
    I was in denial for a long time, then I realized it only has a few pieces of competition in my life: the web browser (any web browser, it's quite a feat), the kernel (under appreciated), gcc (also under appreciated), and the perl interpreter. I wouldn't include libraries and APIs, etc, and there are lots of super cool graphics things out there but I don't use them that much.

    Anyway, all the things birnam mentions are pretty cool in so far as I am familiar with them. The i-mode ctrl-x autocompletion/autoreplace stuff is almost psychotic (if you have an IQ of like 160, it must be amazingly handy), which is why I thought I'd mention something essential to newbies that I didn't think of in the OP:
    :u vim undo!
    :redo vim redo!
    because it ain't hard to do crazy things by accident (I think they have run out of ctrl-alt-meta-shift combinations).
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    169
    Being a Windows user, I was also raised with a silver mouse in my hand. Applications offering sleek GUI, animated tool-tips and decorated buttons are all around the place. These applications take eye-candy further and further, but forget one important thing: Efficiency.
    As Mad_Guy fanatically put it time and again, it all boils down to efficiency. VIM allows you just that, so I'll leave you to youtube VIM and see it in action rather than describe it in words.
    VIM is complete, yet minimalistic in nature. Gives you full control over your code, yet can automate all the boring chores. Takes time and effort to learn, yet pays off once you get to know it.

    Note: You should know proper touch-typing to actually be efficient.

  7. #7
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Speaking of which, a little "question"

    Use ESC, Ctrl + C, Ctrl + [ or rebind capslock for Insert -> Normal mode?

  8. #8
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Though I am no longer using VIM, I did use it for a long time and used capslock to go back to normal mode.

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by zacs7 View Post
    Speaking of which, a little "question"

    Use ESC, Ctrl + C, Ctrl + [ or rebind capslock for Insert -> Normal mode?
    Use ESC to leave "insert" mode and go into "command" (aka escape or normal) mode; use the Insert key to leave command mode and re-enter "insert" mode. Works for me anyway.

    If you are totally new to vim understanding those two modes (there are some other non-essential ones) is task #1.

    Here's my tips o' the day: You can read in (that is, have them processed, not read them into the text display) script files (like the sample .vimrc from my OP) with
    :source ~/vimscripts/somescript
    That includes ~/.vimrc itself, so you can reconfigure without restarting. In addition to just loading a set of imap/cmap macros, there is a whole language for vim scripts including loops, arrays, etc (and more internal functions than I want to count)*.

    You can set a "bookmark" to the current line while in command mode by pressing "m" and then another (upper or lower case) character, eg, "X". To return to that position, use ` (the backtick) and the same character. As long as the file remains in place, vim will remember your bookmarks between invocations; so for example if you have "mX" previously set in some other file, if you don't reset it, "`X" will take you to that line in that file. I have noticed the functionality with this is not perfect (eg, you can lose bookmarks if you mod the file enough) but it generally works. You can get a list of all your existing bookmarks with:
    :marks

    Here's a quite extensive and well written "Vim Reference" that is independent of the official documentation -- organized differently, etc, so it provides a good contrast (and is definately better indexed):
    VIM Reference Guide
    Remember, you can try consulting vim itself on some topic (eg, "recording") with:
    :help recording



    * this is actually how the syntax highlighting files are made. In addition to the hundred or so standard, included ones, there are also syntax files available for lots of C (etc) API's that can be added onto the top of the base language (eg, there's one for gtk and openGL). Those can be googled; one place to start is vim.org itself:
    http://www.vim.org/scripts/
    Last edited by MK27; 04-29-2009 at 11:40 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by zacs7
    Use ESC, Ctrl + C, Ctrl + [ or rebind capslock for Insert -> Normal mode?
    Interesting. I have only ever used ESC for that, but what advantage would the other methods have? Wouldn't rebinding capslock made it inconvenient for those rare cases where you actually want to use it to eh, shout?

    Quote Originally Posted by MK27
    use the Insert key to leave command mode and re-enter "insert" mode.
    I tend to use i for that, but then I am only a pedestrian vi user.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Capslock is closer in than ESC, I use several capslock keybindings and have the capslock function itself disabled. IF I NEED TO SHOUT I CAN JUST TYPE WITHOUT ONE OF MY PINKIES AND HOLD DOWN SHIFT.

  12. #12
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by laserlight View Post
    Interesting. I have only ever used ESC for that, but what advantage would the other methods have? Wouldn't rebinding capslock made it inconvenient for those rare cases where you actually want to use it to eh, shout?
    Sure, but caps lock is on the home row, and escape is certainly not. I usually use ctrl + c. But that's rather... emacs'y. If you need to shout, just bind escape to caps lock, and caps lock to escape.

    Oh and you forgot to mention, turn of vi compatibility mode, because even the bugs are mimicked.

    Here's my .vimrc if anyone cares.
    Code:
    " zac's vimrc
    " Wed Feb 11 07:28:14 UTC 2005
    
    " func
    set nocompatible              " non vi compatible
    set nobackup                  " no backup files (~)
    set history=64                " command line history
    
    " searching
    set incsearch                 " incremental searching
    set ignorecase                " case insenitive search
    set smartcase                 " if search has uppercase letters then search is case-sensitive
    
    " behaviour
       " backspace over everything in indent mode
    set backspace=start,indent,eol
    set mouse=""                  " no mouse support in X11
    set confirm                   " always :confirm on exit
       " complete options 
    set completeopt=menu,longest,preview
    
    " appearance
    set number                    " line numbers
    set ruler                     " ruler
    set showcmd                   " display incomplete commands
    set showmatch                 " show the matching bracket for the last )
    set nowrap                    " no wrapping
    syn on                        " syntax highlighting
    set showtabline=2             " tab bar at the top
    
    " indenting
    set tabstop=3                 " tab = 3 spaces
    set expandtab                 " space tabs
    set cindent
    set autoindent
    set smartindent
    
    " colour scheme
    colorscheme delek
    Last edited by zacs7; 04-29-2009 at 03:49 PM.

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The thing everyone wants to know when they run it for the first time:

    :q
    Quit

    :w
    Save

    :wq
    Save and quit.

    :q!
    Quit without saving.


    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    In addition to quzah's post, here are some other commands that everyone should know:

    i or a
    Enter insert mode. i will place the cursor before the currently selected character, and a will place the cursor after.

    ESC
    Go back to command mode. You may need to press esc twice if you are in some states (like the text search state).

    /
    Search for text

    *
    Search for occurences of the word that is currently under the cursor.

    n
    Go to the next search result.

    N
    Go to the previous search result.

    ==
    Correctly indent the current line.

    =G
    Indent the from the cursor location to the end of the file. This command is great for when you paste some non-indented (or poorly indented) code into a vim buffer.

  15. #15
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by bithub View Post
    ==
    Correctly indent the current line.

    =G
    Indent the from the cursor location to the end of the file. This command is great for when you paste some non-indented (or poorly indented) code into a vim buffer.
    >> will add a tab to the beginning of the current line while in escape mode. If you preface with a number, eg, "5>>" the next five lines will be indented.
    <<works the opposite way.

    Multiple use of either one will, surprise surprise, add or remove more tab stops. Ie, you can shift entire blocks right or left this way.
    Last edited by MK27; 05-02-2009 at 07:30 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 02-26-2009, 11:48 PM
  2. Terminating secondary thread from another thread
    By wssoh85 in forum C++ Programming
    Replies: 13
    Last Post: 12-19-2008, 05:14 AM
  3. CreateThread ?!
    By Devil Panther in forum Windows Programming
    Replies: 13
    Last Post: 11-15-2005, 10:55 AM
  4. pointer to main thread from worker thread?
    By draegon in forum C++ Programming
    Replies: 2
    Last Post: 10-27-2005, 06:35 AM
  5. Critical Sections, destroying
    By Hunter2 in forum Windows Programming
    Replies: 4
    Last Post: 09-02-2003, 10:36 PM

Tags for this Thread