|
It's been a really long time since we have had a vim thread, but seeing as how it is the greatest text editor in existence I think we need one. Feel free to make any suggestions and I will add them to the OP.vim.org posted:What Is Vim? How do I use vim?
In TYOOL 2013 why don't you just use an IDE IDE's are great and all, but there are times when some people have to SSH into a headless box and edit code. When that happens you will be thankful that you know vim instead of having to fight through using nano or something. Also, it may be hard to believe, but not having to ever touch your mouse may actually increase your productivity as a developer. A properly configured vim will provide you with almost all of the functionality of a modern IDE. Tagfiles give you the basic functionality to jump to function/object/class declarations just like an IDE does. Other plugins will let you do fancier things like add autocompletion, and auto updating of your tagfiles. Resources
Recommended Plugins Plugin Management
General
Language specific
Source Control
IDE integration
Goon .vimrc's astr0man fucked around with this message at 21:54 on Jun 20, 2013 |
# ? Jun 4, 2013 18:53 |
|
|
# ? May 8, 2024 03:35 |
|
Basic Tutorial This was shamelessly stolen from an archived vim thread Modes The aspect of vim that most shocks newcomers is that is modal - that is, the program has various different modes of operation, and you will find it extremely hard to avoid them if you want to use vim to its full potential. By default, vim will start in normal mode. In this mode, you simply navigate the text using the h, j, k and l keys - or the normal arrow keys if you want- and press other keys to perform operations on the text. You cannot insert text in this mode. It's often best to think of normal mode as a baseline state for the editor - it's easy to get back to. If you get mired, stuck, don't know what to do, just press the Escape key until vim starts beeping at you, and you're back in normal mode, ready to try again. Remember that - the escape key is your escape key By pressing the i or insert key, you can enter insert mode. In fact, there are a number of ways to do this starting from normal mode. To place the cursor before the current character, press i. To place it after, press I. To place it at the start of the current line, press A. To place it at the end of the line, press a. To create a new line above the cursor and move to the beginning, press O - below the cursor, press o. The text -- INSERT -- will appear at the bottom of the vim screen and you will be able to type away to your heart's content. The third major mode is visual mode. This is usually entered from normal mode in a variety of ways, although it is possible to enter from insert mode if you wish. However, we'll focus on entering from normal mode - it is this way that the greatest magic can be peformed. Visual mode, in essence, allows you to select regions of text to perform actions on. Absolute basics So let's try something out. Open up vim, and have a look. If you haven't already customised vim, you'll see something similar to the screenshot above, and be in normal mode. The first thing we're going to do is exit vim - one of the things that has caught out many a UNIX newbie on the command line. In the GUI age it's not quite as relevant, but always worth knowing. To do this, from normal mode, type :q<enter>(colon qthen press the enter key). This will exit vim. Simple! You can also type :q!<enter> to exit and abandon any changes you've made to the document without prompting. Now let's take a quick look at the help file. Again in normal mode type :he - short for :help which also works. This is something to note - any command you type starting with a colon is usually short for something else. If you want to know what it's short for, press Tab and vim will complete the command for you. Now , we should be looking at a split window- one a blank document, and the other showing some documentation. It's a good idea to read this. If you want help on a specific topic and know how to refer to it, type :he topicname and vim will take you right there if it knows the topic. To exit the help, type :q. You'll be back at your blank document again. Now let's type and edit something. Enter insert mode, and type out anything you like. Make it a few lines long by pressing <enter> a few times as you would in any other editor. Make a few typos. Next, choose a typo. Imagine, for a moment, you've tried to type the wort emacs, but ended up with rtmacs. This is, of course, wrong. But easy to correct. Return to normal mode, and navigate to the start of the word. In normal mode, the w and b move the cursor forward and backward by one word at a time respectively. Place the cursor on the start of the word rtmacs, and still in normal mode type x. Your delete and backspace keys will also still work, though backspace will only work in insert mode usually. x is the key to delete the text under the cursor, so you should be left with the word tmacs. Now we need to replace that errant t. Place the cursor on the t, then press r, then type E. The t will be replaced by a capital E, and all will be well. You can also repeat normal-mode commands easily. If you've just performed a command such as this and want to do it again, move to the place where you want to repeat it and press . (dot, full stop, period - whatever you call it). This is especially useful when you run a command consisting of many keystrokes. Another way to repeat command sequentially is to prefix it with a number - to delete 5 lines starting with the current one, type 5dd - 5 is the number of times to perform the command, the first d is the command (delete) and the second d tells the delete command to operate on whole lines. Ergo, 5 lines (if that many exist) will be deleted. You might want to delete 3 words including the current word - the command would be 3daw for completeness. You may have deciphered the format of vim commands of this type now - optional count, followed by the command, followed by the 'motion'. In this case, 'delete 3 entire words', or '3 times delete entire word'. To delete the two words after the current one, but leave half of the current word intact, place the cursor on earliest letter you wish to delete, and type 2dwp - see the difference? aw is the motion to represent entire words, regardless of the cursor position, and w represents from the cursor until the end of the word. Vim contains a multitude of commands such as these for all sorts of purposes. I'll not go into them all here because this is already getting long-winded. Visual mode and the ex line Now we'll take a quick look at visual mode. Return to normal mode, and we'll start. To enter visual mode in the simplest way, press the v key, and move the cursor about. Some text will be highlighted as you do so. This represents the range of text that any future commands will act upon until visual mode is cancelled. To exit visual mode without doing anything is simple - press Escape as usual. There are two more ways to enter visual mode - V (note the case) and ^V (ctrl-v). V will enter Visual Line mode (which selects entire lines as you move up and down), and ^V enters Visual Block mode, which allows you to select an arbitrary rectangle of text. To run a command on text selected this way, simply do as you already know how to from the Absolute Basics section, or type an ex-mode command. ex-mode? Ah, yes. Remember when we were learning to exit vim and look at the help? Those commands that start with a colon are ex-mode commands, typed into the ex line. If you have selected text in visual mode, then that selection is preserved and acted on. Perhaps the most useful ex-mode command you'll ever use is the :s command - where s is short for substitute - the search-and-replace command. The format of this will be familiar to anyone who uses regular expressions - because it runs a regular expression. The pitfall here is that vim regular expressions are similar to Perl and POSIX ones, but not quite the same. :he pattern and :he patterns are relevant commands at this point, if you ever get stuck or want to learn a little about vim's regular expressions. We'll just do a simple substitution here. Let's say we want to substitute the word cat for the word dog - just once - in our visual selection range. Once you've selected some text any old how, type :s/dog/cat - the first found instance of the word dog will be replaced with cat. To replaces ALL occurrences of dog, we use :s/dog/cat/g. What will happen is that vim will automatically realise that you want to work in the selection, and add some range indicators to the command as soon as you type that initial colon - so what you'll see before pressing Enter is, in fact, :'<,'>s/dog/cat/g. This only happens when there is a visual selection. To replace only on the current line, don't select anything, but type :s/dog/cat anyway - by default, single ex-mode commands concerned with text manipulation work only on the current line. To replace within the entire document, select nothing and type :%s/dog/cat/g Finding what you want Now we move on to finding text in a file. This is simple. In normal mode, press / (slash), and type the text you want to find, then press <enter>. To repeat the search, just type / on its own. You can move from one match to the next by pressing n. To search backwards, type ? then the text you want to find, or to repeat a search type ? on its own. Pressing n will then move backwards through the document. To make searches ignore the case of text, in normal mode type :set ignorecase. To make searches case-sensitive again, :set noignorecase. Conclusion Now, we've written a lovely text file, or program or something. It's time to go to dinner, and we need to save our work and shut the computer down. We do this by entering normal mode and using the :w command. If you've started totally from scratch, use :w /path/to/filename.ext, to save the file as /path/to/filename.ext. If you've been adding to an existing file, simply type :w to save it. This command can also be combined with the :q command easily - :wq to save the file and quit. :wq filename.whatever to save the file as filename.whatever and exit. And in all this, I've barely scratched the surface of what vim can do. I'm so very sorry. It is a fact that vim is guilty of virtually everything that is said about it. It is not imediately user-friendly, it is obtuse for only casual use, it's old, it's archaic, it's weird. It's also extremely powerful, fast, small, efficient, customisable, modern, up-to-date, and available just about everywhere. If you ever find yourself on a UNIX system, it's pretty much a foregone conclusion that at least one Vi derivative, perhaps vim itself will be present. There is a whole lot more that vim can do that I've not managed to cover in this whole introduction, and I hope to be able to cover more soon. And fear not - if this scares you, there is a script bundled with vim that allows you to change some of the behaviour to be more like MS Windows until you get more used to things. astr0man fucked around with this message at 01:35 on Jun 5, 2013 |
# ? Jun 4, 2013 18:53 |
|
You're like that grandpa engineer who keeps around a slide rule because, "it's so much quicker!"
|
# ? Jun 4, 2013 18:59 |
|
Vim thread, awesome! What are some vim plugins that completely revolutionized your productivity? I'd love to know what you swear by. For me, the big language-agnostic ones were:
In other news, tpope is God. I feel like I owe this guy a few thousands of hours of productivity. Btw, OP, you really need to add Practical Vim to mandatory readings for anybody serious about vim. It's a fantastic resource written by a guy who knows his stuff. It's an entry-to-intermediate level book that should cover 95% of use cases out there. DreadCthulhu fucked around with this message at 19:08 on Jun 4, 2013 |
# ? Jun 4, 2013 19:05 |
|
That viemu poo poo in the OP is wack as hell, use vsvim: http://visualstudiogallery.msdn.microsoft.com/59ca71b3-a4a3-46ca-8fe1-0e90e3f79329 It's free and very good.
|
# ? Jun 4, 2013 19:12 |
|
I'll add all that stuff to the OP when I get back to my desk. Does VSVim support versions older than 2010?
|
# ? Jun 4, 2013 19:15 |
|
DreadCthulhu posted:
Command-T is essentially the same but seems more robust in my experience, though you'll need to build vim with ruby support enabled.
|
# ? Jun 4, 2013 19:15 |
|
astr0man posted:I'll add all that stuff to the OP when I get back to my desk. Does VSVim support versions older than 2010? Doesn't appear to, though I think there was a version of it back in the 2005/2008 days, but it was iffy at the time. It's quite good now. And free!
|
# ? Jun 4, 2013 19:17 |
|
Dicky B posted:I switched from Command-T to ctrlp for a while because I read somewhere that it's apparently faster, but whenever I try to use it in a large project it doesn't work properly and some files just don't show up at all. I've had 0 problems with it in my experience, both as far as speed and functionality is concerned. I actually had the opposite experience with Command-T, it was super slow for me. Go figure. With regards to files disappearing, watch out for newly added files to the folder tree, they don't automatically get added to the cache. Press F5 from within the ctrlp buffer to refresh the cache.
|
# ? Jun 4, 2013 19:18 |
|
I'm stuck using 2005 . But hey, I got my work to expense the $100 for viemu.
|
# ? Jun 4, 2013 19:19 |
|
Dicky B posted:I switched from Command-T to ctrlp for a while because I read somewhere that it's apparently faster, but whenever I try to use it in a large project it doesn't work properly and some files just don't show up at all. What really sold me on ctrl-p is that basic insert-mode navigation commands (like ^W for deleting one word back) work on the search term input.
|
# ? Jun 4, 2013 19:22 |
|
DreadCthulhu posted:I've had 0 problems with it in my experience, both as far as speed and functionality is concerned. I actually had the opposite experience with Command-T, it was super slow for me. Go figure. More plugins! clang-complete - Intellisense for C, C++ and Objective-C! YouCompleteMe - An alternative to the above which I haven't tried.
|
# ? Jun 4, 2013 19:23 |
|
What do you guys use for something like renaming a ubiquitous variable across an entire project? I know there's a way of telling vim to do a substitute with confirmation across all the open buffers. Would you use that or perhaps the shell instead?
|
# ? Jun 4, 2013 19:29 |
|
DreadCthulhu posted:What do you guys use for something like renaming a ubiquitous variable across an entire project? I know there's a way of telling vim to do a substitute with confirmation across all the open buffers. Would you use that or perhaps the shell instead? I would just use sed.
|
# ? Jun 4, 2013 19:30 |
|
DreadCthulhu posted:I've had 0 problems with it in my experience, both as far as speed and functionality is concerned. I actually had the opposite experience with Command-T, it was super slow for me. Go figure. Turning off the cache is not as awful as it sounds. For me, a "large" project is around 100k LOC, maybe 500 source files or so, but even on a normal hard drive, the performance impact is barely noticable.
|
# ? Jun 4, 2013 19:32 |
|
DreadCthulhu posted:What do you guys use for something like renaming a ubiquitous variable across an entire project? I know there's a way of telling vim to do a substitute with confirmation across all the open buffers. Would you use that or perhaps the shell instead? Yeah, this is what sed is for. I added everything people have posted to the OP.
|
# ? Jun 4, 2013 19:41 |
|
Sailor_Spoon posted:Turning off the cache is not as awful as it sounds. For me, a "large" project is around 100k LOC, maybe 500 source files or so, but even on a normal hard drive, the performance impact is barely noticable.
|
# ? Jun 4, 2013 19:42 |
|
I made this thing a while back and someday I will actually finish it and add all the features I want to add just as soon as I have some free time oh god why am I so busy... http://www.vimtax.com/ It helps make colorschemes.
|
# ? Jun 4, 2013 19:50 |
|
You guys should post your vimrc's because mine sucks.
|
# ? Jun 4, 2013 19:51 |
|
I mean mine sucks too but that's because I don't use vim as an IDE (this is for Windows) code:
|
# ? Jun 4, 2013 19:55 |
|
This is a wonderful read and helped me really understand writing stuff in my vimrc: http://learnvimscriptthehardway.stevelosh.com/
|
# ? Jun 4, 2013 20:10 |
|
Oh yeah, if any of you feel like owning a really keyboard with vim commands instead of letters on it you can get the same one as me from wasd: http://www.wasdkeyboards.com/index.php/wasd-v1-custom-keyboard.html?kt=1&k=1&s=7cdc41a18e757810db84d26f5875626f
|
# ? Jun 4, 2013 20:15 |
|
astr0man posted:Oh yeah, if any of you feel like owning a really keyboard with vim commands instead of letters on it you can get the same one as me from wasd: http://www.wasdkeyboards.com/index.php/wasd-v1-custom-keyboard.html?kt=1&k=1&s=7cdc41a18e757810db84d26f5875626f No Topre? I'm outta here :P
|
# ? Jun 4, 2013 20:17 |
|
I like the syntastic syntax checking plugin. Just make to bind a key to turn it off. I had to edit a multiple-GB xml file once (don't ask) and it took forever.
|
# ? Jun 4, 2013 20:17 |
|
vimrc? Here's mine, shamelessly stolen from someone else. I've been using VIM for coding for a while now, my only complaint over using an IDE is that I don't have a nice visual debugger available to use. Does anything like the MSVC debugger exist that might run in this environment? I tend to use C/C++. Aw crap, meant to put that image in as a thumbnail :-( code:
Keebler fucked around with this message at 20:34 on Jun 4, 2013 |
# ? Jun 4, 2013 20:29 |
|
Keebler posted:I've been using VIM for coding for a while now, my only complaint over using an IDE is that I don't have a nice visual debugger available to use. Does anything like the MSVC debugger exist that might run in this environment? I tend to use C/C++. If you're comfortable using GDB pyclewn works pretty well. But I probably do end up just using the VS debugger more often.
|
# ? Jun 4, 2013 20:36 |
|
Keebler posted:I've been using VIM for coding for a while now, my only complaint over using an IDE is that I don't have a nice visual debugger available to use. Does anything like the MSVC debugger exist that might run in this environment? I tend to use C/C++.
|
# ? Jun 4, 2013 20:37 |
|
https://github.com/natw/dotfiles - Here's my dotfile repo, with my vimrc and all that other good stuff. I use submodules and pathogen for plugin management. I can't say it's all in active use, but I've been tending to it for a while.
|
# ? Jun 4, 2013 20:42 |
|
Do you ever get to edit a file, delete everything, then start writing new stuff, only to find out, 2 hours later, that you needed something that was in the old version and that you lost it all? Don't start typing from scratch just yet! Vim allows time-based undoing! code:
How can this be done? Vim stores a tree of changes in memory instead of just a regular line. That means you can also find forks in an open file's history, but rather than using the commands to do that, use Gundo: code:
|
# ? Jun 4, 2013 20:49 |
|
Sailor_Spoon posted:https://github.com/natw/dotfiles - Here's my dotfile repo, with my vimrc and all that other good stuff. I use submodules and pathogen for plugin management. I can't say it's all in active use, but I've been tending to it for a while. Wow, I don't know how it never occurred to me to just version my entire .vim directory using git submodules for the plugin stuff.
|
# ? Jun 4, 2013 20:49 |
|
About time for Vim thread. Here's my config. https://bitbucket.org/MalucoMarinero/vimsettings Some bits don't work as intended, but I try not to spend too much time gardening and just fix things as they become problems, or when I have a little time to burn.
|
# ? Jun 4, 2013 23:38 |
|
What are people's thoughts on spf-13? I've been using it for a while now with some minor tweaks (mostly to remove some plugins I don't use, and to add in ragtag) and it seems to work out OK. It's definitely a bit bloated, though.
|
# ? Jun 4, 2013 23:56 |
|
astr0man posted:Absolute basics I think that should refer to normal mode. All these nice .vimrc files are great but I interact with about 10 servers daily and 8 of them only have vi installed (with no possibility of installing vim). Does anyone have a nice example of a .vimrc/.exrc combo? For me it's just: code:
|
# ? Jun 5, 2013 01:09 |
|
I'm really excited for this thread. I just started a RA position at my school and we are working in Python almost exclusively. I've seen a colleague using Vim and he accomplished fixing my files in a fraction of the time that it would have taken me using IDLE in windows. Anyways! I'd like to learn Vim this summer. I briefly read over your OP and was wondering if you, or any other users have any tips for beginners. We will be working on large data sets to populate 3D scenes in OpenSceneGraph. Does anyone have any experience working in Vim in this specific arena? Do you have any advice or time saving tricks to use with data or 3D scenes? I should also mention that I'm pretty nubile when it comes to Unix.
|
# ? Jun 5, 2013 01:30 |
|
While some may not agree with me, fully loaded distributions of Vim with every plugin under the sun should be avoided until you have a good grip of the basics. There are some good books out there that'll help you tackle the early stages. I haven't worked all the way through it but it seems pretty well written: http://pragprog.com/book/dnvim/practical-vim Vim is a very deep editor, especially in the level of customisation it has received. If something goes wrong with all that customisation you'll be lost, so it's much better if you can find stable footing with the core features.
|
# ? Jun 5, 2013 01:36 |
|
Ephphatha posted:I think that should refer to normal mode. Thanks, fixed it.
|
# ? Jun 5, 2013 01:36 |
|
Scat Cat posted:Anyways! I'd like to learn Vim this summer. I briefly read over your OP and was wondering if you, or any other users have any tips for beginners. Remap H to ^ (beginning of line) and L to $ (end of line). Stumbled on it in someone's .vimrc a while back and now I can't live without it. ; to : is good too, saves hitting shift every time you want to use a command.
|
# ? Jun 5, 2013 01:43 |
|
Something nice about the visual block mode (^V). Let's take the following document:code:
Press <Esc> twice, or once followed by any movement key and the change should be repeated on each line: That's right, visual block mode allows the usual selection tricks: Select text with v, press c and you can replace the text. We could use that to uppercase our list, for example, by selecting it, then pressing c and changing things: That's a bad example. Let's press u to undo once, and use ~ on the selected text instead. That one toggles case: Hooray, text successfully edited! All other regular modificators (d, r, c, I, :s for regexes, > and < to indent, etc.) all work there and allow to edit multiple lines at once. This is especially nice when you're working over well-structured text and need to make changes to the structure. It took me a while to discover that one and I'm using it more frequently than I'd have expected.
|
# ? Jun 5, 2013 01:59 |
|
My .vimrc/.exrc combo is slightly more complex now that I had some motivation to do it. One thing I'm wondering though is is there any danger to using map! in this way?code:
|
# ? Jun 5, 2013 02:32 |
|
|
# ? May 8, 2024 03:35 |
|
Is there a reason to do that when you have expandtab set?
|
# ? Jun 5, 2013 02:40 |