TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Ask HN: What is one Vim trick most people don't know?

45 pointsby wawhalalmost 7 years ago

40 comments

aequitasalmost 7 years ago
If you forgot to sudo vi, you&#x27;ve already edited the file, saving the file as root without first saving it as your own and then having to move it:<p>&lt;ESC&gt;:w !sudo tee %<p>Pipes the content of the file to a tee process as root and lets tee save it under the current file name. Vim will tell you the file has changed on disk and ask if it should reload it afterwards.
评论 #17428589 未加载
thedjinnalmost 7 years ago
Not really a trick of Vim itself, but when using Vim from the terminal I find ctrl-z very useful to background Vim, type in a few shell commands for git or whatever and then use &quot;fg&quot; to hop back into Vim.
评论 #17423142 未加载
评论 #17426239 未加载
Xophmeisteralmost 7 years ago
Many of the things here, as of writing, are quite elementary Vim tips. A lesser known trick that I learnt quite recently and have found very useful is `gn`, which visually selects the next search hit (similarly `gN` for the previous hit). That&#x27;s not so useful in itself, but it can be combined with an operation, such as `c` or `y`, etc., which can then be repeated using the `.`.
k4ch0walmost 7 years ago
Macros, Macros, Macros. These are a game changer when editing large files. Use the `q` key and followed by any letter you like, I default to `a`. Perform some action, like deleting a line, appending to the start of a line, or searching for a word then deleting it. Press `esc`, then type in the number of times you want to repeat the action, followed by `@` and the letter you chose. I.E `10@a` and it will perform that action 10 times.
评论 #17423542 未加载
评论 #17423265 未加载
评论 #17423711 未加载
robotvertalmost 7 years ago
`Ctrl + v` to visually select one character at a time (instead of say `Shift + v` that selects a whole line) and then use the movement keys to select a block of characters horizontally as well as vertically. Imagine several well indented HTML &lt;li class=&quot;something&quot;&gt; elements, you could for instance delete&#x2F;modify all the class attributes on all lines visually in one go (to insert text use `Shift + i`, type, then Esc).
评论 #17423654 未加载
vikin9almost 7 years ago
:norm(al) command Allows you to do some of the vim magic also on remote systems where you don&#x27;t have all your plugins.<p>Example:<p>(1) Select block with `C-v`<p>(2) Hit colon `:`<p>(3) Write norm and enter any sequence as you would be in normal mode: `norm I# ` (prepend selection with a comment sign)<p>(4) Admire the result.
评论 #17423634 未加载
valbacaalmost 7 years ago
Quick renaming of variables: what to type&quot;explanation<p><pre><code> gd&quot; go to definition of variable :%s&#x2F;&quot; beginning of a replace... Ctrl-r &#x2F;&quot; inserts the last word searched into the command, so what &quot;gd&quot; found &#x2F;newname&#x2F;gc&quot; finish the replace command, g=global replace, c=confirm each replace </code></pre> Save a character going to a line: instead of using :42&lt;enter&gt; to go to line 42, can use 42G<p>Auto-indent: =<p>Go to last file: Ctrl-^<p>Abbreviations: put this in your ~&#x2F;.vimrc<p><pre><code> iab cmain int main() { \&lt;CR&gt; return 0; \&lt;CR&gt;} </code></pre> Insert the contents of a file: :r &lt;filename&gt;<p>I like to have a ~&#x2F;gitmessage.txt which has a template I like, then when I do git commit all I have to do is:<p><pre><code> :r ~&#x2F;gitmessage.txt </code></pre> Vim + Ctags:<p>Ctags link usages and definitions so you can navigate through code like in an IDE without the bloat of an IDE. Works best with C, but works pretty well with any language with C-like syntax (and more).<p>Goto tag (definition) of what&#x27;s under cursor: Ctrl-]<p>Go back: Ctrl-t<p>Open tag in a preview: Ctrl-w<p>:tag &lt;tag&gt; &quot; go to tag, like:<p><pre><code> :tag someFunction :tag SOME_CONSTANT</code></pre>
评论 #17428514 未加载
lgeorgetalmost 7 years ago
`Shift+v` to select a block of text and do a single operation to the same portion of text on several lines (regexp, aligning, etc.)<p>There&#x27;s also `vt` + some character to select text from the cursor to the next occurrence of that character.
评论 #17422924 未加载
amaterasualmost 7 years ago
A couple of nice ones: `gq` for re-wrapping a visually selected block of text (&lt;shift&gt;-v is simplest) to your text width.<p>`=` for re-indenting a selection to your current shift-width settings etc. (or `gg=G` to just do the whole file)
评论 #17423325 未加载
wawhalalmost 7 years ago
You can use `Ctrl + C` instead of `ESC`<p>I figured this out today. And I submitted this Ask HN to find out more such things.
评论 #17423819 未加载
评论 #17423086 未加载
评论 #17423005 未加载
eb0laalmost 7 years ago
I use a lit this ed (<a href="http:&#x2F;&#x2F;man.openbsd.org&#x2F;4.4BSD-Lite2&#x2F;ed.1" rel="nofollow">http:&#x2F;&#x2F;man.openbsd.org&#x2F;4.4BSD-Lite2&#x2F;ed.1</a>) commands from vi (not just vim).<p>For instance:<p>Delele first 20 lines:<p>&lt;ESC&gt;:0,20d<p>Delete lines from current location to end of file:<p>&lt;ESC&gt;:,$d<p>Search and replace from regrex:<p>&lt;ESC&gt;:0,$s&#x2F;regex&#x2F;replacement&#x2F;g
评论 #17425184 未加载
评论 #17423042 未加载
评论 #17423240 未加载
评论 #17423036 未加载
crooalmost 7 years ago
I have two favourites : alt+* (search for the word I currently standing on) and ciw (delete the word you standing on and enter insert mode).
评论 #17422780 未加载
评论 #17423183 未加载
评论 #17423361 未加载
smilesndalmost 7 years ago
&#x27;Esc :q&#x27; to quit vim seems to be the one trick most people don&#x27;t know.
评论 #17424466 未加载
flukusalmost 7 years ago
Mine is playing with errorformat (personal blogspam: <a href="http:&#x2F;&#x2F;flukus.github.io&#x2F;vim-errorformat-demystified.html" rel="nofollow">http:&#x2F;&#x2F;flukus.github.io&#x2F;vim-errorformat-demystified.html</a>) and combining this with :cf (read errors from file) or :cb (read errors from buffer) to parse a file&#x2F;buffer into the quicklist. Great for dealing with log files.
vram22almost 7 years ago
Not sure if most people don&#x27;t know it, but this is useful (in Windows vim):<p>In Unix vi(m), you use Ctrl-V to quote a special character to actually insert it into the text (or into a last line mode command; see example below).<p>So in Unix if you want to insert a carriage return (Ctrl-M) into a file or a command, you do Ctrl-V Ctrl-M. That does not work in Windows with gVim because Ctrl-V means paste from clipboard. So you use Ctrl-Q instead of Ctrl-V, like in this example, where you want to use vim to remove all the carriage returns (CRs) from the file (say if you were going to use the file on Unix later [1]):<p><pre><code> :%s&#x2F;Ctrl-QEnter&#x2F;&#x2F;g </code></pre> where instead of typing the letters Enter, you hit the Enter key.<p>The %s means do the command for all lines in the file (% for all lines, s for substitute), the command is search-and-replace (old pattern with new pattern, here Ctrl-M with nothing), and the g(lobal) at the end means do it for all occurrences in the line (otherwise it does it only for the first occurrence on the line).<p>This will show on your screen as:<p><pre><code> :%s&#x2F;^M&#x2F;&#x2F;g (the Ctrl-Q does not show on screen) </code></pre> where the ^M represents the CR or Enter.<p>The same command on Unix would be:<p><pre><code> :%s&#x2F;Ctrl-VEnter&#x2F;&#x2F;g </code></pre> This will show on your screen as:<p><pre><code> :%s&#x2F;^M&#x2F;&#x2F;g (the Ctrl-V does not show on screen) </code></pre> [1] Of course there are other ways to remove the CRs from the file, like a) using a dos2unix utility, a) many modern editors will do it for you (via a setting or a menu option), c) push it to Git from Windows and then pull it from Unix (with appropriate Git settings for line-ending handling), etc.
评论 #17425401 未加载
评论 #17425677 未加载
apraoalmost 7 years ago
A slightly longer trick but..<p>:tabe newfile to open a new file.<p>Then, add the following to your ~&#x2F;.vimrc (on MacOS)<p>map &lt;Esc&gt;f &lt;Esc&gt;:tabn&lt;cr&gt; map &lt;Esc&gt;b &lt;Esc&gt;:tabp&lt;cr&gt;<p>Now you can switch between tabs using option + arrow keys
评论 #17423379 未加载
mohitmunalmost 7 years ago
you can find most on this thread <a href="https:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;726894&#x2F;what-are-the-dark-corners-of-vim-your-mom-never-told-you-about" rel="nofollow">https:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;726894&#x2F;what-are-the-dark...</a>
vbstevenalmost 7 years ago
`gv` for reselecting the last selection<p>Assigning different functions to the arrow keys helps when learning vim navigation
评论 #17425658 未加载
ifoundthetaoalmost 7 years ago
If you&#x27;re in Insert Mode, and you want to paste the thing you last yanked, without leaving insert mode, you can do the following:&lt;C-r&gt;0<p>That pastes the contents of the 0 register into the buffer. Very useful. That&#x27;ll work for all registers too.
评论 #17424831 未加载
mihaifmalmost 7 years ago
Better keyboard scrolling with this simple mapping:<p>nmap &lt;C-j&gt; 3j3&lt;C-e&gt;<p>nmap &lt;C-k&gt; 3k3&lt;C-y&gt;<p>This maps the &lt;C-j&gt; and &lt;C-k&gt; keys to a scroll action similar to mouse scrolling: the cursors stays in a fixed position while the whole screen moves up or down 3 rows.
评论 #17425876 未加载
ssayolsalmost 7 years ago
Diff mode, delicious! Just edit the 2 files with vim -O file1 file2, and in vim do &lt;ESC&gt;:diffthis&lt;enter&gt;C-w&lt;right&gt;:diffthis&lt;enter&gt;, and voila!<p>Vimgrep and the quickfix window are also pretty cool.
评论 #17428584 未加载
afarrellalmost 7 years ago
Turning off the ability to enter recording mode. I still occasionally type :Q instead of :q and I’d like that to also quit...or even to just no-op.<p>Any tips on how to just rip recoding mode completely out of my vim?
评论 #17423264 未加载
评论 #17433931 未加载
Pete_Dalmost 7 years ago
If you like these, it&#x27;s worth skimming `:help index` and `:help option-summary` occasionally. You&#x27;re likely to find at least one interesting new keybinding or setting.
teiloalmost 7 years ago
&#x27;ct&#x27; followed by a character later in the line. Erases all text from the cursor up to that character, and enters insert mode.
megousalmost 7 years ago
Writing some vimscript is actually not that bad and opens plenty of possibilities for advanced, context aware code editing.
stephenralmost 7 years ago
How to quit is probably what most people don’t know, when it’s been invoked by something looking for $EDITOR
tpictalmost 7 years ago
`:compiler` for parsing lines in the quickfix window - great for jumping directly to unit test failures
voltooidalmost 7 years ago
:vsplit filename<p>to open file called &quot;filename&quot; in a vertical split of the console window. Also<p>:split filename<p>will open it in a horizontal split
aabbatealmost 7 years ago
&#x27;Shift+zz&#x27; to save and exit
评论 #17423274 未加载
67_45almost 7 years ago
A lot of people seem to be unaware of switching files within vim instead of exiting and reinvoking.
评论 #17422785 未加载
评论 #17423249 未加载
评论 #17423275 未加载
eddygalmost 7 years ago
zz - scroll the line with the cursor to the center of the screen<p>zt - scroll the line with the cursor to the top<p>zb - scroll the line with the cursor to the bottom<p>also:<p>Ctrl-O - jump to last (think &quot;o&quot;lder) cursor positions<p>Ctrl-I - jump to next cursor positions (after using Ctrl-O)
arc_of_descentalmost 7 years ago
Ctrl-e and Ctrl-y to scroll with the cursor on the same line.
dllthomasalmost 7 years ago
<p><pre><code> git grep -n foo | vim - -c &#x27;cbuf!&#x27;</code></pre>
hesselinkalmost 7 years ago
Ctrl+V for making and operating on block selections.
peter_retiefalmost 7 years ago
I know i am going to get downvoted for this but :q!
diehundealmost 7 years ago
Ctrl-a increases a number in visual model
arunmpalmost 7 years ago
copy a line using &lt;Esc&gt;1 yy and past simply &lt;Esc&gt; p
评论 #17423333 未加载
alexandernstalmost 7 years ago
How to quit it
评论 #17423728 未加载
vram22almost 7 years ago
One of my favorite Vim tricks (which also works in vi) is the general technique of piping a specified range of text through a command (a Unix-style filter that reads from standard input and writes to standard output), with the output of that command then replacing that range of text.<p>The text can be anything from a single line, some arbitrary range of lines (defined via vim marks like a and b or other letters, some paragraphs (see example below), to the entire file - and the filter command can be anything at all, too. The Cartesian product of those two things makes it a very powerful technique.<p>An example is this key mapping of the character v to a command that format paragraphs of text using the fmt command [1]:<p>map v !}fmt -w 64&lt;CR&gt;<p>This says: map the letter v to the command following it (after the space), which is:<p><pre><code> !}fmt -w 64&lt;CR&gt; </code></pre> which runs&#x2F;pipes (!) the current paragraph (when at top of a para, the cursor movement } defines a para) through the fmt -w 64 command, which formats its input text into lines of not more than 64 characters each.<p>(I just used v because it was one of the characters that Vim does not already assign a command to - any other available character will work.)<p>I have the mapping in my _vimrc on Windows and .vimrc on Unix.<p>With that mapping, I can reformat any paragraph (where paragraphs in vim are defined by a blank line either above and below them or both), just by going to the top of the paragraph (with { ) and typing v (no Enter needed).<p>For example, if I have this (single) line in a file in vim:<p><pre><code> the quick brown fox jumped the quick brown fox jumped the quick brown fox jumped the quick brown fox jumped the quick brown fox jumped </code></pre> and I press v when at the start of that line, it gets reformatted into this para:<p><pre><code> the quick brown fox jumped the quick brown fox jumped the quick brown fox jumped the quick brown fox jumped the quick brown fox jumped </code></pre> I typically use this technique when writing text content (non-programming content, such as documentation, tech articles, blog posts, etc.) or sometimes even for block comments of text in programs.<p>You can also create a different version of that formatting command to filter the entire text file through fmt in one shot (that would be &quot;!Gfmt -w 64&quot; when at top of file, where G moves to the last line), but I tend not to use that approach much, because it sometimes messes up some paragraphs which should not be reformatted. So I do it a para at a time.<p>If you use the sort command instead of fmt, that will sort the range of lines you select, and the sorted lines replace the original lines. This is of course useful in text files and (text format) data files, but is also sometimes useful in source code, e.g. to sort lines of import statements (say in Python or Go - although Go has commands for that), ranges of variable declarations into alphabetical order, array literals, etc.).<p>[1] (Fmt is a Unix command, but you can probably get it in many of the Unix subsystems that run on Windows too, like Cygwin, UWin, Windows Subsystem for Linux too). c The command that you run the selected text through, can also be a Unix or Windows pipeline, which makes the technique even more powerful.
dvfjsdhgfvalmost 7 years ago
ZZ
评论 #17423277 未加载