What are the dark corners of Vim your mom never told you about?
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.
Admin
Updated on September 15, 2020Comments
-
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 about 15 yearsWhat does it do exactly?
-
chaos about 15 yearsSwitches a pair of tab-separated columns (separator arbitrary, it's all regex) with each other.
-
ojrac about 15 yearsThis is my favorite bit of vi voodo, hands down.
-
Admin about 15 yearsNever had to use this sort of a thing, but we'll certainly add to my arsenal of tricks...
-
Adriano Varoli Piazza about 15 yearsgreat tip, thanks. For bonus points, add a list of common valid encodings.
-
chaos about 15 yearsThe ^I is meant to be a tab, incidentally.
-
Admin about 15 yearsI knew the first one :)
-
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 about 15 yearsThere's also Ctrl-W =, which makes the windows equal width.
-
Admin about 15 yearsAmazing....I really needed it
-
aks about 15 yearsGreat command, I have lost count of the number of times this has happened to me ever since I moved to Ubuntu from Slackware.
-
dbr about 15 yearsjholloway7: 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 about 15 yearsHow to force it works after :edit file again?
-
sjh about 15 yearsdab "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 about 15 yearscounts, counts as i haven't seen this one :)
-
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 about 15 yearsNice one. Never heard of it... Might be useful :P
-
Don Reba about 15 years@Masi: yi(va(p deletes only the brackets
-
rlbond about 15 yearsThis is just a regex; plenty of IDEs have regex search-and-replace.
-
thomasrutter about 15 yearsThis 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 about 15 yearscan you summarize? If that external page goes away, this answer is useless. :(
-
Jeffrey Knight about 15 yearsOne 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 about 15 yearsIt will exit edit mode when you press "jj"
-
saffsd about 15 yearsThis 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 about 15 yearswhat if i only want perldo to run on a specified line? or a selected few lines?
-
Ben Richardson about 15 yearsYou can give it a range like any other command. For example :1,5perldo will only operate on lines 1-5.
-
Leonard about 15 yearsWhat does this do? And does it work with autchdir?
-
rampion about 15 yearsI 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 about 15 yearsHow does that make Vim more like an IDE ??
-
mpe about 15 yearsI 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 about 15 yearsAfter all, an IDE = editor + compiler + debugger + building tools + ...
-
darjab about 15 yearsAlso, 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 about 15 yearsSame goes for cursorline ... and a lot others.
-
hasen almost 15 yearshow will you type jj then? :P
-
Neeraj Singh almost 15 yearsI added # to comment out a block of code in ruby. How do I undo it.
-
Grant Limberg almost 15 yearswell, 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 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 almost 15 yearsCould you do $_ += '\nNEWLINE!!!' to get a newline after the current one?
-
Ben Richardson almost 15 yearsSadly 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 almost 15 yearsThis doesn't seem to fly on my system. Can you show an example only using the ls command?
-
ojblass almost 15 yearsHow often to you type jj? In English at least?
-
ojblass almost 15 yearsYou can type it slowly too and it works. Outstanding tip.
-
Christian almost 15 yearsAnd how do you revert it back?
-
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 almost 15 years:!xxd -r //To revert back from HEX
-
Ivan Vučica almost 15 yearsI 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 almost 15 yearswould :set encoding=cp1250 have the same effect?
-
Etienne PIERRE almost 15 yearsAlso 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 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 almost 15 yearsYou can just hit ctrl+v again, mark the //'s and hit x to "uncomment"
-
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 almost 15 yearsthis cursor line feature makes VIM ugly slow, actually unusable
-
Arafangion over 14 yearsArguably, that's even better than running vim as root! Upvoted!
-
MaddTheSane over 14 yearsI hit
q:
by accident all the time... -
alexey_p over 14 years<C-V> is the more generic command that works in both the text-mode and gui
-
cmcginty over 14 yearsso I guess this won't work if you use 'set expandtab' to force all tabs to spaces.
-
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 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 over 14 yearsYou can also browse files within Vim itself, using :Explore
-
Alex over 14 yearsI 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 over 14 yearsAlternatively, from the ex editor (:), you can do CTRL-f to pop up the command history window.
-
KKovacs over 14 yearsHi Roman, this is exactly what this mapping does, but assigns it to a "hot key". :)
-
Artem Russakovskii over 14 yearsExcellent tip - exactly what I was looking for today.
-
Andreas Grech over 14 yearsI actually think it's
:%!xxd -r
to revert it back -
Claudiu over 14 yearso my god this is the coolest thing
-
David Winslow over 14 yearsOn linuxes (where xmllint is pretty commonly installed) I usually just do :%! xmllint - for this.
-
Scotty Allen over 14 yearsTry it:) It basically highlights a given word, without moving the cursor to the next occurrance (like * would).
-
jamessan over 14 yearsEven 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 over 14 yearsIt's only <C-q> if you're using the awful mswin.vim (or you mapped it yourself).
-
jamessan over 14 yearsYou can do the same with "nnoremap m* :let @/ = '\<' . expand('<cword>') . '\>'<cr>"
-
jamessan over 14 yearsWhy 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 over 14 yearsAn 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 over 14 yearssimilarly,
'.
will go to the last edited line, And`.
will go to the last edited position -
AndyL about 14 yearsFor a noob, what exactly does tee do? Would someone mind parseing out this command for me?
-
ZyX about 14 yearsI use NERDCommenter for this.
-
bradlis7 about 14 years@jleedev me too. I almost hate this command, just because I use it accidentally way too much.
-
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 about 14 yearsCtrl-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 about 14 yearsYou can list the change list by doing :changes
-
Kimball Robinson about 14 yearsThe * 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 about 14 yearsI 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 about 14 yearsI think that's intentional, as a nested and recursive macro.
-
Armstrongest about 14 yearstee 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 almost 14 years`:e +b %' is similarly useful for reopening in binary mode (no munging of newlines)
-
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 almost 14 years@Alex: definitely, capslock is death. "wait, wtf? oh, that was ZZ?....crap."
-
romandas almost 14 years@intuited - Of course. But I've never found that to be all I wanted. :)
-
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 almost 14 yearsThis is stupid. Defeats the productivity gains from not using the mouse.
-
jabirali almost 14 years@sundar: Why pass a line to
sed
, when you can use the similar built-ined
/ex
commands? Try running:.s/old/new/g
;-) -
matpie over 13 yearsA much simpler solution would be to use bcvi: sshmenu.sourceforge.net/articles/bcvi
-
Ivan Vučica over 13 yearsFor me it didn't open the source; instead it apparently used elinks to dump rendered page into a buffer, and then opened that.
-
Admin over 13 years@thomasrutter: Why not dW/cW instead of dt<space>?
-
Admin over 13 years@Masi: With the surround plugin: ds(.
-
Vitalii Fedorenko over 13 yearsTo map x!! to sudo write and quit use:
cmap x!! w !sudo tee %<CR><CR>:q!<CR>
-
Rafid over 13 yearsExcellent tip man! How do you get to know these geeky things?
-
Nathan Fellman over 13 yearsor better yet, instead of typing
a
, just remove thec
.c
means confirm replacement -
bpw1621 over 13 yearsq/ and q? can be used to do a similar thing for your search patterns.
-
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 over 13 yearsyeah... 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 over 13 yearsHitting <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 about 13 yearsHow can I get the result/output to a different buffer than the current?
-
Ehtesh Choudhury about 13 yearsHm, I didn't know this about the indentation.
-
clt60 about 13 yearscmap w!! w !sudo tee %
-
jbleners almost 13 years@hasen easy, from insert mode jhjjrja
-
Gerardo Marset almost 13 yearsYou should never run
sudo vim
. Instead you should exportEDITOR
as vim and runsudoedit
. -
Gerardo Marset almost 13 years
qqqqqifuu<Esc>h@qq@q
-
Nathan Long over 12 yearsAnother 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 over 12 yearsI don't see what this is good for (besides looking like a joke answer). Can anybody else enlighten me?
-
Ehtesh Choudhury over 12 yearsYou 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 over 12 yearsWow, so now I can just do
:later 8h
and I'm done for today? :P -
Raja over 12 yearsDon'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 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 about 12 yearsWith
^X^F
my pet peeve is that filenames include=
signs, making it do rotten things in many occasions (ini files, makefiles etc). I usese isfname-==
to end that nuisance -
sehe about 12 yearsI don't know what you are writing... But surely, my boss would be more curious when he saw me write ROT13 jumble :)
-
sehe about 12 yearsNo need, usually, to be exactly on the braces. Thought frequently I'd just
=}
orvaBaB=
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 about 12 yearsI'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 about 12 yearsYou 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 almost 12 yearsthe tab one is really useful :D
-
Dzung Nguyen almost 12 yearsthere's error when executed this command
-
mk12 almost 12 yearsThis is actually a pretty simple regex, it's only escaping the group parentheses that makes it look complicated.
-
sjas almost 12 yearsI find
G=gg
easier to type. -
brianmearns over 11 years@ojblass: Not sure how many people ever right matlab code in Vim, but
ii
andjj
are commonly used for counter variables, becausei
andj
are reserved for complex numbers. -
Luc M over 11 yearsA 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 over 11 yearsYou can also
:so session.vim
inside vim. -
codygman over 11 yearsopen vim and then do ":for i in range(1,255) | .put='10.0.0.'.i | endfor"
-
Wayne Werner over 11 yearsHot dang that's useful. I use
<C-o>
/<C-i>
for this all the time - or marking my place. -
Keyur Padalia about 11 yearsWorks better with a slash at the end. Neat trick!
-
aqn about 11 yearsAnd 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 about 11 years
=%
should do it too. -
dotancohen about 11 yearsI found this post googling recursive VIM macros. I could find no way to stop the macro other than killing the VIM process.
-
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 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. almost 11 yearspretty sure this works in command mode too.
-
Justin L. almost 11 yearsi type <kbd>ctrl</kbd>+<kbd>p</kbd> way too much by accident while trying to hit <kbd>ctrl</kbd>+<kbd>[</kbd> ><
-
00dani almost 11 yearsMacros 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 ツ almost 11 yearsCan anybody please explain % after tee?
-
migu over 10 years@maximus: vim replaces % by the name of the current buffer/file.
-
attomos over 10 yearsI used
control-c
to exit insert mode, the only constrain is I cannot usecontrol-c
after I inserted text in Visual block mode. I need to useesc
for this case. -
Ruslan over 10 years@RyanEdwards filling /etc/hosts maybe
-
Taurus Olson over 10 yearsI 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 over 10 yearsSometimes 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 over 10 years
gg=G
is quite neat when pasting in something. -
alexzzp over 10 yearsThe best one is: inoremap <c-i> <ESC><ESC>
-
Marcin Łoś over 10 yearsWhile
<esc>
is really bad, I findctrl-[
comfortable enough not to look for replacements. Felt a little bit weird at first, but now it's a second nature. -
freeo over 10 years
jk
- once you go jk, you don't use anything else. I haveShift+Space
to toggle insert mode, system-wide caps to esc,jj
and built-inC-[
. Butjk
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 over 10 years@tsukimi wow, thanks for the link to commandlinefu.com ; What an awesome site!
-
Derecho about 10 yearsSimilarly,
pydo
andpy3do
work for python if you have the required support compiled in. -
laughing_man about 10 yearsTo 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 about 10 years@NeerajSingh: you can undo that by pressing CTR+V, selecting the column of #'s and press x to remove them.
-
d33tah about 10 yearsWhy does your sysadmin even give you root? :D
-
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 about 10 yearsJust in case you didn't already know, ctrl+c already works like escape.
-
romeovs almost 10 yearsor to spoof your friends:
nmap i ggg?G``
. Or the diabolical:nmap i ggg?G``i
! -
Brady Trainor almost 10 yearsI had no idea what was bringing up that window. Finally I know!
-
Brady Trainor almost 10 yearsMaybe fgm has head gestures mapped to mouse clicks.
-
Raja almost 10 years
:verbose
can also be used beforenmap l
orhighlight Normal
to find out where thel
keymap or theNormal
highlight were last defined. Very useful for debugging! -
Raja almost 10 years+1 the built-in autocomplete is just sitting there waiting to be discovered.
-
Laurence Gonsalves almost 10 years@joeytwiddle Thanks! I added that info to the answer.
-
Déjà vu almost 10 yearsYour command assumes one will spend at least 15 minutes in vim!
-
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 almost 10 yearsNERDCommentor 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 over 9 yearsI 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 over 9 years@LucM If you tried it why didn't you provide an explanation?
-
zmo over 9 yearsfor 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 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 over 9 years@dotancohen: If you don't want to close/reopen the file you can do :set binary
-
zmo over 9 yearsand why not use <C-w>* commands?
-
ken over 9 yearstoo 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 over 9 years
<c-w><left>
or<c-w><right>
… it's not that awful. -
ken over 9 yearsAwesome! not aware about it
-
dotancohen over 9 yearsThis 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 over 9 yearsRelated: How to save a file for which I have no write permissions? at Vim SE
-
glerYbo over 9 yearsRelated: How can I safely undo changes that I may or may not have made? at Vim SE
-
glerYbo over 9 yearsRelated: How to dump output from external command into editor? at Vim SE
-
glerYbo over 9 yearsRelated: How to edit binary files with Vim? at Vim SE
-
glerYbo over 9 yearsRelated: How to convert a source code file into HTML? at Vim SE
-
glerYbo over 9 yearsRelated: Re-indenting badly indented code at Vim SE
-
glerYbo over 9 yearsRelated: How to dump output from external command into editor? at Vim SE
-
vHalaharvi about 9 yearsyou 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 about 9 yearsI agree with @freeo that
jk
is better. I often find myself just doingjkjkjkjk
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 mappedjk
andkj
to escape. To me,jj
is inferior because the cursor is then 2 lines down. -
adam_0 almost 9 yearsAdditionally
luado
for Lua if you have that support compiled in -
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 aftertextwidth
. See the documentation for more information. -
shalomb almost 9 yearsI 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. over 8 yearsCheck explanation here!
-
Braden Best over 8 yearsDid 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 invim -u none
-
Braden Best over 8 yearsI 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 aNUL
byte appearing somewhere, and strings being null-terminated. This happens even if I set binary and noeol from within vim. So, thanks @JoshLee for thevim -b
tip, but I still don't trust:%!xxd -r
. -
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 thatU
does. Try to move right and you go to the bottom of the page. And it messes with you until you try to insert withi
, 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 over 8 years@kyrias Oh, I've been doing it like
ggVG=
. -
Braden Best over 8 yearsCommented 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 over 8 yearsI believe it's
<C-w> c
to close a window, actually.:h ctrl-w
-
Braden Best over 8 yearsNote 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 over 8 yearsYou can also do
:g/match/normal "Ayy
-- thenormal
keyword lets you tell it to run normal-mode commands (which you are probably more familiar with). -
haridsv over 8 yearsI 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 almost 8 years@MykolaGolubyev
:set undofile
-
dreftymac almost 8 yearssee
:help cmdwin
-
Amit Gold almost 8 years@romeovs 2nd one is infinite loop, use nnoremap
-
Admin almost 8 yearsruby ruby dooooo...ruby ruby doooo. Sing it like Megan Draper
https://www.youtube.com/watch?v=DKol_ThaMWc
-
BlackCap over 6 yearsWithout ex-mode:
i10.0.0.1<Esc>Y254p$<C-v>}g<C-a>
-
SidOfc over 6 yearsWhen 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 almost 6 years@BlackCap the last
g
is supposed to beG
(go to last line)?<C-a>
will only increment by one, so you'll end up with10.0.0.1
on the first line, and10.0.0.2
on the next 254 lines. -
BlackCap almost 6 years@StefanvandenAkker
}
goes to the last character on the last line, as opposed toG
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 almost 6 years
cmder
is much easier and simpler, it also comes with its own ssh client -
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 over 5 yearsMacros 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 over 5 years
-
Pie almost 5 years@BradenBest why do you say that? what are those better uses for awesome block insert
-
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 almost 5 yearsI'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 over 4 yearsnot working for me
-
JDG over 3 yearsThis is awesome. Btw the
%
is unnecessary as the default parameter is the current buffer.:help TOhtml
-
Diti about 2 yearsNowadays it only seems to work if the terminal does not require interaction (
sudo: no tty present and no askpass program specified
).