What are the dark corners of Vim your mom never told you about?

128,556

Solution 1

Might not be one that 99% of Vim users don't know about, but it's something I use daily and that any Linux+Vim poweruser must know.

Basic command, yet extremely useful.

:w !sudo tee %

I often forget to sudo before editing a file I don't have write permissions on. When I come to save that file and get a permission error, I just issue that vim command in order to save the file without the need to save it to a temp file and then copy it back again.

You obviously have to be on a system with sudo installed and have sudo rights.

Solution 2

Something I just discovered recently that I thought was very cool:

:earlier 15m

Reverts the document back to how it was 15 minutes ago. Can take various arguments for the amount of time you want to roll back, and is dependent on undolevels. Can be reversed with the opposite command :later

Solution 3

:! [command] executes an external command while you're in Vim.

But add a dot after the colon, :.! [command], and it'll dump the output of the command into your current window. That's : . !

For example:

:.! ls

I use this a lot for things like adding the current date into a document I'm typing:

:.! date

Solution 4

Not exactly obscure, but there are several "delete in" commands which are extremely useful, like..

  • diw to delete the current word
  • di( to delete within the current parens
  • di" to delete the text between the quotes

Others can be found on :help text-objects

Solution 5

One that I rarely find in most Vim tutorials, but it's INCREDIBLY useful (at least to me), is the

g; and g,

to move (forward, backward) through the changelist.

Let me show how I use it. Sometimes I need to copy and paste a piece of code or string, say a hex color code in a CSS file, so I search, jump (not caring where the match is), copy it and then jump back (g;) to where I was editing the code to finally paste it. No need to create marks. Simpler.

Just my 2cents.

Share:
128,556
Admin
Author by

Admin

Updated on September 15, 2020

Comments

  • Admin
    Admin over 3 years

    There are a plethora of questions where people talk about common tricks, notably "Vim+ctags tips and tricks".

    However, I don't refer to commonly used shortcuts that someone new to Vim would find cool. I am talking about a seasoned Unix user (be they a developer, administrator, both, etc.), who thinks they know something 99% of us never heard or dreamed about. Something that not only makes their work easier, but also is COOL and hackish. After all, Vim resides in the most dark-corner-rich OS in the world, thus it should have intricacies that only a few privileged know about and want to share with us.

  • Admin
    Admin about 15 years
    What does it do exactly?
  • chaos
    chaos about 15 years
    Switches a pair of tab-separated columns (separator arbitrary, it's all regex) with each other.
  • ojrac
    ojrac about 15 years
    This is my favorite bit of vi voodo, hands down.
  • Admin
    Admin about 15 years
    Never had to use this sort of a thing, but we'll certainly add to my arsenal of tricks...
  • Adriano Varoli Piazza
    Adriano Varoli Piazza about 15 years
    great tip, thanks. For bonus points, add a list of common valid encodings.
  • chaos
    chaos about 15 years
    The ^I is meant to be a tab, incidentally.
  • Admin
    Admin about 15 years
    I knew the first one :)
  • Michal aka Miki
    Michal aka Miki about 15 years
    @dbr: di( deletes the content inside the brackets. How can you delete only the brackets? I remember that it is possible too.
  • Bill Lynch
    Bill Lynch about 15 years
    There's also Ctrl-W =, which makes the windows equal width.
  • Admin
    Admin about 15 years
    Amazing....I really needed it
  • aks
    aks about 15 years
    Great command, I have lost count of the number of times this has happened to me ever since I moved to Ubuntu from Slackware.
  • dbr
    dbr about 15 years
    jholloway7: Thanks, mentioned that help page and linked to the text-objects vim docs. Masi: Not sure, daw will delete the brackets+contents, the closest command I can think of is xf)x (delete bracket, jump to next ) and delete again)
  • Mykola Golubyev
    Mykola Golubyev about 15 years
    How to force it works after :edit file again?
  • sjh
    sjh about 15 years
    dab "delete arounb brackets", daB for around curly brackets, t for xml type tags, combinations with normal commands are as expected cib/yaB/dit/vat etc
  • Admin
    Admin about 15 years
    counts, counts as i haven't seen this one :)
  • ephemient
    ephemient about 15 years
    @skinp: If you undo and then make further changes from the undone state, you lose that redo history. This lets you go back to a state which is no longer in the undo stack.
  • skinp
    skinp about 15 years
    Nice one. Never heard of it... Might be useful :P
  • Don Reba
    Don Reba about 15 years
    @Masi: yi(va(p deletes only the brackets
  • rlbond
    rlbond about 15 years
    This is just a regex; plenty of IDEs have regex search-and-replace.
  • thomasrutter
    thomasrutter about 15 years
    This is possibly the biggest reason for me staying with Vim. That and its equivalent "change" commands: ciw , ci( , ci" , as well as dt<space> and ct<space>
  • TREE
    TREE about 15 years
    can you summarize? If that external page goes away, this answer is useless. :(
  • Jeffrey Knight
    Jeffrey Knight about 15 years
    One other command -- I don't want to open a separate reply: If you have a split window (:sp [filename] or :vert split [filename) you can swap window panes with ^w r
  • Sir Rippov the Maple
    Sir Rippov the Maple about 15 years
    It will exit edit mode when you press "jj"
  • saffsd
    saffsd about 15 years
    This is quite similar to :r! The only difference as far as I can tell is that :r! opens a new line, :.! overwrites the current line.
  • Haseeb
    Haseeb about 15 years
    what if i only want perldo to run on a specified line? or a selected few lines?
  • Ben Richardson
    Ben Richardson about 15 years
    You can give it a range like any other command. For example :1,5perldo will only operate on lines 1-5.
  • Leonard
    Leonard about 15 years
    What does this do? And does it work with autchdir?
  • rampion
    rampion about 15 years
    I suppose it would override autochdir temporarily (until you switched buffers again). Basically, it changes directory to the root directory of the current file. It gives me a bit more manual control than autochdir does.
  • darjab
    darjab about 15 years
    How does that make Vim more like an IDE ??
  • mpe
    mpe about 15 years
    I did say "a little" :) But it is something many IDEs do, and some people like it, eg: eclipse.org/screenshots/images/JavaPerspective-WinXP.png
  • darjab
    darjab about 15 years
    After all, an IDE = editor + compiler + debugger + building tools + ...
  • darjab
    darjab about 15 years
    Also, just FYI, vim has an option to set invnumber. That way you don't have to "set nu" and "set nonu", i.e. remember two functions - you can just toggle.
  • darjab
    darjab about 15 years
    Same goes for cursorline ... and a lot others.
  • hasen
    hasen almost 15 years
    how will you type jj then? :P
  • Neeraj Singh
    Neeraj Singh almost 15 years
    I added # to comment out a block of code in ruby. How do I undo it.
  • Grant Limberg
    Grant Limberg almost 15 years
    well, if you haven't done anything else to the file, you can simply type u for undo. Otherwise, I haven't figured that out yet.
  • romandas
    romandas almost 15 years
    @rlbond - It comes down to how good is the regex engine in the IDE. Vim's regexes are pretty powerful; others.. not so much sometimes.
  • Bert Coerver
    Bert Coerver almost 15 years
    Could you do $_ += '\nNEWLINE!!!' to get a newline after the current one?
  • Ben Richardson
    Ben Richardson almost 15 years
    Sadly not, it just adds a funky control character to the end of the line. You could then use a Vim search/replace to change all those control characters to real newlines though.
  • ojblass
    ojblass almost 15 years
    This doesn't seem to fly on my system. Can you show an example only using the ls command?
  • ojblass
    ojblass almost 15 years
    How often to you type jj? In English at least?
  • ojblass
    ojblass almost 15 years
    You can type it slowly too and it works. Outstanding tip.
  • Christian
    Christian almost 15 years
    And how do you revert it back?
  • Naga Kiran
    Naga Kiran almost 15 years
    :set autochdir //this also serves the same functionality and it changes the current directory to that of file in buffer
  • Naga Kiran
    Naga Kiran almost 15 years
    :!xxd -r //To revert back from HEX
  • Ivan Vučica
    Ivan Vučica almost 15 years
    I have used this today, but I think I didn't need to specify "%:p"; just opening the file and :e ++enc=cp1250 was enough. I
  • laz
    laz almost 15 years
    would :set encoding=cp1250 have the same effect?
  • Etienne PIERRE
    Etienne PIERRE almost 15 years
    Also very usefull is g+ and g- to go backward and forward in time. This is so much more powerfull than an undo/redo stack since you don't loose the history when you do something after an undo.
  • Paul
    Paul almost 15 years
    @laz: no, 'encoding' option defines the encoding that Vim uses to store all of its internal data like text in the buffers, registers, etc. 'fileencoding' defines the encoding of the current buffer. But if you set 'fenc' after opening a file, Vim will convert file from current encoding to the new one. So, the only way to open file using the specific encoding is to use ++enc option.
  • nos
    nos almost 15 years
    You can just hit ctrl+v again, mark the //'s and hit x to "uncomment"
  • Maxim Sloyko
    Maxim Sloyko almost 15 years
    @Casey, this can only help you if you already have a sudo, i.e. did something with it in the current session.
  • robson3.14
    robson3.14 almost 15 years
    this cursor line feature makes VIM ugly slow, actually unusable
  • Arafangion
    Arafangion over 14 years
    Arguably, that's even better than running vim as root! Upvoted!
  • MaddTheSane
    MaddTheSane over 14 years
    I hit q: by accident all the time...
  • alexey_p
    alexey_p over 14 years
    <C-V> is the more generic command that works in both the text-mode and gui
  • cmcginty
    cmcginty over 14 years
    so I guess this won't work if you use 'set expandtab' to force all tabs to spaces.
  • Martin York
    Martin York over 14 years
    @Casey: The first two lines will not apply. The last line will make sure you can just cut and paste into SO.
  • Sundar R
    Sundar R over 14 years
    @Jeffrey: Thanks... @saffsd: That single difference is a great thing: Now I can pass lines to sed or awk and have it replaced with the processed output...
  • Roman Plášil
    Roman Plášil over 14 years
    You can also browse files within Vim itself, using :Explore
  • Alex
    Alex over 14 years
    I remapped capslock to esc instead, as it's an otherwise useless key. My mapping was OS wide though, so it has the added benefit of never having to worry about accidentally hitting it. The only drawback IS ITS HARDER TO YELL AT PEOPLE. :)
  • Smith.Patel
    Smith.Patel over 14 years
    Alternatively, from the ex editor (:), you can do CTRL-f to pop up the command history window.
  • KKovacs
    KKovacs over 14 years
    Hi Roman, this is exactly what this mapping does, but assigns it to a "hot key". :)
  • Artem Russakovskii
    Artem Russakovskii over 14 years
    Excellent tip - exactly what I was looking for today.
  • Andreas Grech
    Andreas Grech over 14 years
    I actually think it's :%!xxd -r to revert it back
  • Claudiu
    Claudiu over 14 years
    o my god this is the coolest thing
  • David Winslow
    David Winslow over 14 years
    On linuxes (where xmllint is pretty commonly installed) I usually just do :%! xmllint - for this.
  • Scotty Allen
    Scotty Allen over 14 years
    Try it:) It basically highlights a given word, without moving the cursor to the next occurrance (like * would).
  • jamessan
    jamessan over 14 years
    Even better is using sudoedit in the first place. Otherwise, I'd use ":w !sudo dd of=%" since that doesn't bombard you with the file you're saving being cat to stdout.
  • jamessan
    jamessan over 14 years
    It's only <C-q> if you're using the awful mswin.vim (or you mapped it yourself).
  • jamessan
    jamessan over 14 years
    You can do the same with "nnoremap m* :let @/ = '\<' . expand('<cword>') . '\>'<cr>"
  • jamessan
    jamessan over 14 years
    Why wouldn't you just set makeprg to the proper tool you use for your build (if it isn't set already) and then use :make? :copen will show you the output of the build as well as allowing you to jump to any warnings/errors.
  • hlovdal
    hlovdal over 14 years
    An alternative to :.!date is to write "date" on a line and then run !$sh (alternatively having the command followed by a blank line and run !jsh). This will pipe the line to the "sh" shell and substitute with the output from the command.
  • Asela Liyanage
    Asela Liyanage over 14 years
    similarly, '. will go to the last edited line, And `. will go to the last edited position
  • AndyL
    AndyL about 14 years
    For a noob, what exactly does tee do? Would someone mind parseing out this command for me?
  • ZyX
    ZyX about 14 years
    I use NERDCommenter for this.
  • bradlis7
    bradlis7 about 14 years
    @jleedev me too. I almost hate this command, just because I use it accidentally way too much.
  • Nefrubyr
    Nefrubyr about 14 years
    :.! is actually a special case of :{range}!, which filters a range of lines (the current line when the range is .) through a command and replaces those lines with the output. I find :%! useful for filtering whole buffers.
  • Kimball Robinson
    Kimball Robinson about 14 years
    Ctrl-O and Ctrl-I (tab) will work similarly, but not the same. They move backward and forward in the "jump list", which you can view by doing :jumps or :ju For more information do a :help jumplist
  • Kimball Robinson
    Kimball Robinson about 14 years
    You can list the change list by doing :changes
  • Kimball Robinson
    Kimball Robinson about 14 years
    The * will be greedy, so this regex assumes you have just two columns. If you want it to be nongreedy use {-} instead of * (see :help non-greedy for more information on the {} multiplier)
  • Kimball Robinson
    Kimball Robinson about 14 years
    I didn't know macros could repeat themselves. Cool. Note: qx starts recording into register x (he uses qq for register q). 0 moves to the start of the line. dw delets a word. j moves down a line. @q will run the macro again (defining a loop). But you forgot to end the recording with a final "q", then actually run the macro by typing @q.
  • Yktula
    Yktula about 14 years
    I think that's intentional, as a nested and recursive macro.
  • Armstrongest
    Armstrongest about 14 years
    tee creates a tee junction in your output. You can output to the screen and a file, for example. Best to search SO or google for a more detailed answer. Or ask a question!
  • intuited
    intuited almost 14 years
    `:e +b %' is similarly useful for reopening in binary mode (no munging of newlines)
  • intuited
    intuited almost 14 years
    @romandas: If all you want to do is column transposition, then you don't need a powerful regex engine. Just sayin.
  • intuited
    intuited almost 14 years
    @Alex: definitely, capslock is death. "wait, wtf? oh, that was ZZ?....crap."
  • romandas
    romandas almost 14 years
    @intuited - Of course. But I've never found that to be all I wanted. :)
  • Sudhanshu
    Sudhanshu almost 14 years
    ^R=3+5+8 in insert mode will let you insert the value of the expression (3+5+8) in text with fewer keystrokes.
  • Andreas Grech
    Andreas Grech almost 14 years
    This is stupid. Defeats the productivity gains from not using the mouse.
  • jabirali
    jabirali almost 14 years
    @sundar: Why pass a line to sed, when you can use the similar built-in ed/ex commands? Try running :.s/old/new/g ;-)
  • matpie
    matpie over 13 years
    A much simpler solution would be to use bcvi: sshmenu.sourceforge.net/articles/bcvi
  • Ivan Vučica
    Ivan Vučica over 13 years
    For me it didn't open the source; instead it apparently used elinks to dump rendered page into a buffer, and then opened that.
  • Admin
    Admin over 13 years
    @thomasrutter: Why not dW/cW instead of dt<space>?
  • Admin
    Admin over 13 years
    @Masi: With the surround plugin: ds(.
  • Vitalii Fedorenko
    Vitalii Fedorenko over 13 years
    To map x!! to sudo write and quit use: cmap x!! w !sudo tee %<CR><CR>:q!<CR>
  • Rafid
    Rafid over 13 years
    Excellent tip man! How do you get to know these geeky things?
  • Nathan Fellman
    Nathan Fellman over 13 years
    or better yet, instead of typing a, just remove the c. c means confirm replacement
  • bpw1621
    bpw1621 over 13 years
    q/ and q? can be used to do a similar thing for your search patterns.
  • bpw1621
    bpw1621 over 13 years
    ":e ." does the same thing for your current working directory which will be the same as your current file's directory if you set autochdir
  • We Are All Monica
    We Are All Monica over 13 years
    yeah... I'd hate you too ;) you should only need a root shell VERY RARELY, unless you're already in the habit of running too many commands as root which means your permissions are all screwed up.
  • idbrii
    idbrii over 13 years
    Hitting <C-f> after : or / (or any time you're in command mode) will bring up the same history menu. So you can remap q: if you hit it accidentally a lot and still access this awesome mode.
  • dcn
    dcn about 13 years
    How can I get the result/output to a different buffer than the current?
  • Ehtesh Choudhury
    Ehtesh Choudhury about 13 years
    Hm, I didn't know this about the indentation.
  • clt60
    clt60 about 13 years
    cmap w!! w !sudo tee %
  • jbleners
    jbleners almost 13 years
    @hasen easy, from insert mode jhjjrja
  • Gerardo Marset
    Gerardo Marset almost 13 years
    You should never run sudo vim. Instead you should export EDITOR as vim and run sudoedit.
  • Gerardo Marset
    Gerardo Marset almost 13 years
    qqqqqifuu<Esc>h@qq@q
  • Nathan Long
    Nathan Long over 12 years
    Another way of accomplishing this is to record a macro in register a that does some transformation to a single line, then linewise highlight a bunch of lines with V and type :normal! @a to applyyour macro to every line in your selection.
  • Ryan Edwards
    Ryan Edwards over 12 years
    I don't see what this is good for (besides looking like a joke answer). Can anybody else enlighten me?
  • Ehtesh Choudhury
    Ehtesh Choudhury over 12 years
    You don't lose the redo history if you make a change after an undo. It's just not easily accessed. There are plugins to help you visualize this, like Gundo.vim
  • Igor Popov
    Igor Popov over 12 years
    Wow, so now I can just do :later 8h and I'm done for today? :P
  • Raja
    Raja over 12 years
    Don't forget you can prepend numbers to perform an action multiple times in Vim. So to expand the current window height by 8 lines: 8<C-W>+
  • sehe
    sehe about 12 years
    @intuited that isn't really a problem, just vim "+'0" (use undofile and viminfo for added bonus). Now, ZQ would be an accident but not as likely to happen by chance
  • sehe
    sehe about 12 years
    With ^X^F my pet peeve is that filenames include = signs, making it do rotten things in many occasions (ini files, makefiles etc). I use se isfname-== to end that nuisance
  • sehe
    sehe about 12 years
    I don't know what you are writing... But surely, my boss would be more curious when he saw me write ROT13 jumble :)
  • sehe
    sehe about 12 years
    No need, usually, to be exactly on the braces. Thought frequently I'd just =} or vaBaB= because it is less dependent. Also, v}}:!astyle -bj matches my code style better, but I can get it back into your style with a simple %!astyle -aj
  • tsukimi
    tsukimi about 12 years
    I'm a beginner vim user and even i know this command, I think its a common command.Its a top rated command on commandlinefu
  • tsukimi
    tsukimi about 12 years
    You can use :g! to find lines that don't match a pattern e.x. :g!/set/normal dd (delete all lines that don't contain set)
  • Dzung Nguyen
    Dzung Nguyen almost 12 years
    the tab one is really useful :D
  • Dzung Nguyen
    Dzung Nguyen almost 12 years
    there's error when executed this command
  • mk12
    mk12 almost 12 years
    This is actually a pretty simple regex, it's only escaping the group parentheses that makes it look complicated.
  • sjas
    sjas almost 12 years
    I find G=gg easier to type.
  • brianmearns
    brianmearns over 11 years
    @ojblass: Not sure how many people ever right matlab code in Vim, but ii and jj are commonly used for counter variables, because i and j are reserved for complex numbers.
  • Luc M
    Luc M over 11 years
    A short explanation would be appreciated... I tried it and could be very usefull! You can even do something like set colorcolumn=+1,+10,+20 :-)
  • TankorSmash
    TankorSmash over 11 years
    You can also :so session.vim inside vim.
  • codygman
    codygman over 11 years
    open vim and then do ":for i in range(1,255) | .put='10.0.0.'.i | endfor"
  • Wayne Werner
    Wayne Werner over 11 years
    Hot dang that's useful. I use <C-o>/<C-i> for this all the time - or marking my place.
  • Keyur Padalia
    Keyur Padalia about 11 years
    Works better with a slash at the end. Neat trick!
  • aqn
    aqn about 11 years
    And also note that '!' is like 'y', 'd', 'c' etc. i.e. you can do: !!, number!!, !motion (e.g. !Gshell_command<cr> replace from current line to end of file ('G') with output of shell_command).
  • Sri Kadimisetty
    Sri Kadimisetty about 11 years
    =% should do it too.
  • dotancohen
    dotancohen about 11 years
    I found this post googling recursive VIM macros. I could find no way to stop the macro other than killing the VIM process.
  • Isaac Nequittepas
    Isaac Nequittepas almost 11 years
    @Vdt: It'd be useful if you posted your error. If it's this one: "error (netrw) neither the wget nor the fetch command is available" you obviously need to make one of those tools available from your PATH environment variable.
  • dotancohen
    dotancohen almost 11 years
    @JoshLee: If one is careful not to traverse newlines, is it safe to not use the -b option? I ask because sometimes I want to make a hex change, but I don't want to close and reopen the file to do so.
  • Justin L.
    Justin L. almost 11 years
    pretty sure this works in command mode too.
  • Justin L.
    Justin L. almost 11 years
    i type <kbd>ctrl</kbd>+<kbd>p</kbd> way too much by accident while trying to hit <kbd>ctrl</kbd>+<kbd>[</kbd> ><
  • 00dani
    00dani almost 11 years
    Macros are also allowed to be recursive and work in pretty much the same fashion when they are, so it's not particularly necessary to use a mapping for this.
  • maximus ツ
    maximus ツ almost 11 years
    Can anybody please explain % after tee?
  • migu
    migu over 10 years
    @maximus: vim replaces % by the name of the current buffer/file.
  • attomos
    attomos over 10 years
    I used control-c to exit insert mode, the only constrain is I cannot use control-c after I inserted text in Visual block mode. I need to use esc for this case.
  • Ruslan
    Ruslan over 10 years
    @RyanEdwards filling /etc/hosts maybe
  • Taurus Olson
    Taurus Olson over 10 years
    I have almost the same mapping: imap kj <esc> I find it faster than jj and more natural than jk probably because I have more strength in my middle finger. The pattern middle finger then index finger is more simple. This simple mapping is one of my favourite so far.
  • Pandu
    Pandu over 10 years
    Sometimes it's better to do what tsukimi said and just filter out lines that don't match your pattern. An abbreviated version of that command though: :v/PATTERN/d Explanation: :v is an abbreviation for :g!, and the :g command applies any ex command to lines. :y[ank] works and so does :normal, but here the most natural thing to do is just :d[elete].
  • remmy
    remmy over 10 years
    gg=G is quite neat when pasting in something.
  • alexzzp
    alexzzp over 10 years
    The best one is: inoremap <c-i> <ESC><ESC>
  • Marcin Łoś
    Marcin Łoś over 10 years
    While <esc> is really bad, I find ctrl-[ comfortable enough not to look for replacements. Felt a little bit weird at first, but now it's a second nature.
  • freeo
    freeo over 10 years
    jk - once you go jk, you don't use anything else. I have Shift+Space to toggle insert mode, system-wide caps to esc, jj and built-in C-[. But jk beats them all. I've read it in the vim wikia, but ignored it, because I wouldn't be able to write jk! It's been a while since then and never ever needed to write jk literally. It should be a vim default :) imap jk <ESC> vmap jk <ESC> (exit visual mode!)
  • nedR
    nedR over 10 years
    @tsukimi wow, thanks for the link to commandlinefu.com ; What an awesome site!
  • Derecho
    Derecho about 10 years
    Similarly, pydo and py3do work for python if you have the required support compiled in.
  • laughing_man
    laughing_man about 10 years
    To test if your version of vim is compiled with the clipboard, do vim --version | grep clipboard and you should have the following arguments: +clipboard and +xterm_clipboard.
  • d33tah
    d33tah about 10 years
    @NeerajSingh: you can undo that by pressing CTR+V, selecting the column of #'s and press x to remove them.
  • d33tah
    d33tah about 10 years
    Why does your sysadmin even give you root? :D
  • Sebastian Ärleryd
    Sebastian Ärleryd about 10 years
    +1 for diw, never occured to me to use i/a on words, used it only for (), <>, {} until now. ^^
  • Kyle Challis
    Kyle Challis about 10 years
    Just in case you didn't already know, ctrl+c already works like escape.
  • romeovs
    romeovs almost 10 years
    or to spoof your friends: nmap i ggg?G`` . Or the diabolical: nmap i ggg?G``i!
  • Brady Trainor
    Brady Trainor almost 10 years
    I had no idea what was bringing up that window. Finally I know!
  • Brady Trainor
    Brady Trainor almost 10 years
    Maybe fgm has head gestures mapped to mouse clicks.
  • Raja
    Raja almost 10 years
    :verbose can also be used before nmap l or highlight Normal to find out where the l keymap or the Normal highlight were last defined. Very useful for debugging!
  • Raja
    Raja almost 10 years
    +1 the built-in autocomplete is just sitting there waiting to be discovered.
  • Laurence Gonsalves
    Laurence Gonsalves almost 10 years
    @joeytwiddle Thanks! I added that info to the answer.
  • Déjà vu
    Déjà vu almost 10 years
    Your command assumes one will spend at least 15 minutes in vim!
  • Andrew Ferrier
    Andrew Ferrier almost 10 years
    autocmd FileType mail set tw=76 fo=tcrq in your ~/.vimrc will also work, if you can't edit the external editor command.
  • cartbeforehorse
    cartbeforehorse almost 10 years
    NERDCommentor is the way forward for commenting. \cc - comment line. 5\cc comment 5 lines. 5\ci - invert comment of next 5 lines (commented lines will be uncommented, and vice-versa). 5\cs - block comment the next 5 lines of code. etc. etc.
  • Dettorer
    Dettorer over 9 years
    I find this one particularly useful when people send links to a paste service and forgot to select a syntax highlighting, I generally just have to open the link in vim after appending "&raw".
  • DBedrenko
    DBedrenko over 9 years
    @LucM If you tried it why didn't you provide an explanation?
  • zmo
    zmo over 9 years
    for the second tip, there is <C-^> that switches between the two last buffers, so all you've got is to remap that key on <tab>. And if you're editing a C/C++ file, you'll switch between header. For more info, :help alternate
  • 2c2c
    2c2c over 9 years
    @dotancohen recursive macros end when they read end of file. ctrl D is a shortcut to inject this is most programs.
  • Bambu
    Bambu over 9 years
    @dotancohen: If you don't want to close/reopen the file you can do :set binary
  • zmo
    zmo over 9 years
    and why not use <C-w>* commands?
  • ken
    ken over 9 years
    too many keys to be pressed... lol. anyway i rarely use this anymore, have another script which help to me move to numbered window by just pressing the <leader>number.
  • zmo
    zmo over 9 years
    <c-w><left> or <c-w><right>… it's not that awful.
  • ken
    ken over 9 years
    Awesome! not aware about it
  • dotancohen
    dotancohen over 9 years
    This is a terrific answer. Not the bit about creating the IP addresses, but the bit that implies that VIM can use for loops in commands.
  • glerYbo
    glerYbo over 9 years
  • glerYbo
    glerYbo over 9 years
  • glerYbo
    glerYbo over 9 years
  • glerYbo
    glerYbo over 9 years
  • glerYbo
    glerYbo over 9 years
  • glerYbo
    glerYbo over 9 years
  • glerYbo
    glerYbo over 9 years
  • vHalaharvi
    vHalaharvi about 9 years
    you don't have to type jj. Ctrl-c will also take you to the normal mode.. In-fact this is such faster way to toggle mode and yet so few do it.
  • dansch
    dansch about 9 years
    I agree with @freeo that jk is better. I often find myself just doing jkjkjkjk over and over now, while thinking. It doesn't screw up cursor position, and it lets me know what window/pane I'm on. I mapped jk and kj to escape. To me, jj is inferior because the cursor is then 2 lines down.
  • adam_0
    adam_0 almost 9 years
    Additionally luado for Lua if you have that support compiled in
  • mjturner
    mjturner almost 9 years
    colorcolumn allows you to specify columns that are highlighted (it's ideal for making sure your lines aren't too long). In the original answer, set cc=+1 highlights the column after textwidth. See the documentation for more information.
  • shalomb
    shalomb almost 9 years
    I prefer never to run vim as root/under sudo - and would just run the command from vim e.g. :!sudo tee %, :!sudo mv % /etc or even launch a login shell :!sudo -i
  • SD.
    SD. over 8 years
    Check explanation here!
  • Braden Best
    Braden Best over 8 years
    Did you know that you can double-bang (!!) to automatically fill out the :.!? Pretty convenient when you don't feel like typing all that out. It even works in vim -u none
  • Braden Best
    Braden Best over 8 years
    I can never get xxd -r to revert properly. Every time I do, it corrupts the binary and 99% of the file goes missing. I'm guessing it's due to a NUL byte appearing somewhere, and strings being null-terminated. This happens even if I set binary and noeol from within vim. So, thanks @JoshLee for the vim -b tip, but I still don't trust :%!xxd -r.
  • Braden Best
    Braden Best over 8 years
    @Alex "I remapped capslock to esc instead, as it's an otherwise useless key." It's also a bastard of a key. How many times have you tried to use vim without knowing caps lock was on? Try to hit u and it does that weird shit that U does. Try to move right and you go to the bottom of the page. And it messes with you until you try to insert with i, which takes you to the beginning of the line rather than where you were, and then YOUR TYPING COMES OUT LIKE THIS. There's another reason to re- or un-map caps lock.
  • Braden Best
    Braden Best over 8 years
    @kyrias Oh, I've been doing it like ggVG=.
  • Braden Best
    Braden Best over 8 years
    Commented out code is probably one of the worst types of comment you could possibly put in your code. There are better uses for the awesome block insert.
  • Braden Best
    Braden Best over 8 years
    I believe it's <C-w> c to close a window, actually. :h ctrl-w
  • Braden Best
    Braden Best over 8 years
    Note that you can achieve the same thing with cat <file> | awk '{print " " $line}'. So try :w ! awk '{print " " $line}' | xclip -i. That's supposed to be four spaces between the ""
  • Kimball Robinson
    Kimball Robinson over 8 years
    You can also do :g/match/normal "Ayy -- the normal keyword lets you tell it to run normal-mode commands (which you are probably more familiar with).
  • haridsv
    haridsv over 8 years
    I have used macros for over 20 years and never knew this time saving festure. I wonder if it is supported in the original SysV version itself or an extension of Vim.
  • Giumo
    Giumo almost 8 years
    @MykolaGolubyev :set undofile
  • dreftymac
    dreftymac almost 8 years
    see :help cmdwin
  • Amit Gold
    Amit Gold almost 8 years
    @romeovs 2nd one is infinite loop, use nnoremap
  • Admin
    Admin almost 8 years
    ruby ruby dooooo...ruby ruby doooo. Sing it like Megan Draper https://www.youtube.com/watch?v=DKol_ThaMWc
  • BlackCap
    BlackCap over 6 years
    Without ex-mode: i10.0.0.1<Esc>Y254p$<C-v>}g<C-a>
  • SidOfc
    SidOfc over 6 years
    When you get into creating custom mappings, this will save your ass so many times, probably one of the most useful ones here (IMO)!
  • Stefan van den Akker
    Stefan van den Akker almost 6 years
    @BlackCap the last g is supposed to be G (go to last line)? <C-a> will only increment by one, so you'll end up with 10.0.0.1 on the first line, and 10.0.0.2 on the next 254 lines.
  • BlackCap
    BlackCap almost 6 years
    @StefanvandenAkker } goes to the last character on the last line, as opposed to G which goes to the first character on the last line. g<C-a> is range increment- it increments the first line by 1, the next by 2 and so on..
  • Uri Goren
    Uri Goren almost 6 years
    cmder is much easier and simpler, it also comes with its own ssh client
  • Stefan van den Akker
    Stefan van den Akker almost 6 years
    @BlackCap ah, I missed the }, sorry. :help v_g_CTRL-A shows the help for incrementing addition. Thanks for explaining!
  • Stun Brick
    Stun Brick over 5 years
    Macros being used recursively is definitely a design choice, and they will keep doing so until they hit the end of the buffer. If your macro is recursive and stuck in an infinite loop, press ctrl+c to stop it rather than killing your entire vim process.
  • sudo bangbang
    sudo bangbang over 5 years
  • Pie
    Pie almost 5 years
    @BradenBest why do you say that? what are those better uses for awesome block insert
  • Braden Best
    Braden Best almost 5 years
    @Pie because commented out code is dead code. Dead code serves no useful purpose and creates clutter. You think you're preserving code you might want to bring back later, but there's always a better way. You can use your undo history, you can dupe the file, or you can use git, which has this functionality built in (git checkout). As for better uses for block insert, there aren't any set-in-stone. You just use it whenever you have multiple lines where you want to insert the same sequence of characters at the same place.
  • Braden Best
    Braden Best almost 5 years
    I'd be lying if I said I never comment out code. I'm guilty of that occasionally, too. But I recognize that it's bad practice, and I make a serious point to either delete it or bring it back when I'm done with whatever experiment I'm doing at the time.
  • Anthony Awuley
    Anthony Awuley over 4 years
    not working for me
  • JDG
    JDG over 3 years
    This is awesome. Btw the % is unnecessary as the default parameter is the current buffer. :help TOhtml
  • Diti
    Diti about 2 years
    Nowadays it only seems to work if the terminal does not require interaction (sudo: no tty present and no askpass program specified).