首页
32 条评论
drtz将近 2 年前
I once did a lunch and learn at work where I tried to teach coworkers how to use vim efficiently. It turned out to be the absolute worst lunch and learn I ever did.<p>It's just so unintuitive and trying to teach it in a "here are some good shortcuts that will save you time" way was a near-total waste.<p>vim is the closest any editor comes to interfacing directly with my brain. Once you learn to make your neurons fire in the right way to move the cursor to the right spot, it's easy and there's no better way to edit code. Describing the neuron firing order to someone else, though, is futile.<p>It's like learning to to use your fingers to pick something up for the first time as a kid: you just have to try enough times until it sticks.
评论 #37018818 未加载
评论 #37019086 未加载
评论 #37019640 未加载
评论 #37019320 未加载
评论 #37019077 未加载
评论 #37020430 未加载
评论 #37019415 未加载
评论 #37022991 未加载
评论 #37023751 未加载
评论 #37019082 未加载
评论 #37020465 未加载
kzrdude将近 2 年前
One of the innovations in the Vim space that I've appreciated a lot is which-key by folke for Neovim: <a href="https://github.com/folke/which-key.nvim">https://github.com/folke/which-key.nvim</a><p>It makes keybindings in vim discoverable, it's quite magical. For example, press g and get a table of all the various commands that follow from there. Press mapleader and get a table of various commands from there, etc.<p>Edit: On closer look, I've learned that maybe vim-which-key came before that, and guide-key before that etc, there's a long lineage of this too!
agileAlligator将近 2 年前
> simple vim one liner which allows you to count the words in the file<p>> :%s/\w\+/\=submatch(0)+1/g | echo line('$')<p>Yeah, no. Isn't there a plugin to just display word count in the corner somewhere automatically?
评论 #37018276 未加载
评论 #37018810 未加载
评论 #37018156 未加载
评论 #37018177 未加载
评论 #37018079 未加载
评论 #37019892 未加载
评论 #37019047 未加载
评论 #37019222 未加载
maccard将近 2 年前
The comments in this post remind me why I use an IDE. Breakpoints, debuggers, auto formatting aren't things I make decisions on every day, they're just built into the tool I use. (I recently moved from visual studio to intellij and friends, and the biggest feature I miss are <a href="https://learn.microsoft.com/en-us/visualstudio/debugger/using-tracepoints?view=vs-2022" rel="nofollow noreferrer">https://learn.microsoft.com/en-us/visualstudio/debugger/usin...</a> tracepoints. They're like runtime configured logging for compiled languages)<p>That said, I do use vi bindings for navigation. Stuff like "replace all instances of X with Y" are semantic, and my editor handles them for me. Doing "insert at the end of this quoted string is something I _do_ use. I have gripes with Vi's defaults (hjkl shouldn't be the default because the author had a keyboard with those defaults half a century ago), but overall it's an improvement on arrow keys and a mouse. The best comparison to me is it's like touch typing Vs hunt and peck typing for text editing. I'm not wildly more productive because I can use a handful of shortcuts, but it does remove friction.<p>What I <i>do</i> want is semantic vim. I spend my day writing code, and despite the fact that I edit text I think in semantics. No, I probably don't care what the next word is, and I don't care to guess how vim is going to interpret `w` when I give it:<p><pre><code> macro_rules! foo {
($l:tt) => { bar!($l); }
}
macro_rules! bar {
(3) => {}
}
foo!(3);
</code></pre>
What I really want is something that interacts with tree sitter (or the likes) and navigates my code. I want to delete this current block, or this function. I want to jump to the next argument, not the comma in a hard coded string. I want change the last argument of this function, not up until the closing bracket of the first function call in foo(bar(), baz(), 3);
评论 #37020946 未加载
评论 #37021128 未加载
评论 #37028275 未加载
评论 #37022160 未加载
fuzztester将近 2 年前
A classic:<p>"Your problem with vim is that you don't grok vi:"<p><a href="https://news.ycombinator.com/item?id=2911930">https://news.ycombinator.com/item?id=2911930</a>
ozim将近 2 年前
Weird because most of it is just regex not vim. Running python tool from inside of vim also doesn’t feel like “vim one liner”.
评论 #37020103 未加载
jacobgorm将近 2 年前
Select a block of text with V, press colon and then type:<p>s/^$/printf("@%d\\n", __LINE__);<p>To place a line printf on every empty line in the program, to quickly find out where your code crashes on the next run.<p>Obviates most of my need for external debuggers, and works over serial lines etc where setting up a debugger is going to be a hassle or simply not possible.
评论 #37019038 未加载
评论 #37017698 未加载
评论 #37017788 未加载
eviks将近 2 年前
It's not a sign of "unparalleled efficiency" having to type these little monstrosities by hand, let alone frequently<p>:g/\(\d\{1,3}\.\)\{3}\d\{1,3\}/y A<p>Is there a way to type "ip" and select from a helper list with regex and description sorted by frecency?
评论 #37017968 未加载
评论 #37017859 未加载
评论 #37018947 未加载
评论 #37017997 未加载
ChuckMcM将近 2 年前
My favorite one liner is :!}fmt which on Linux and UNIX systems runs from where the cursor is, through to the end of the paragraph though the fmt(1) command (which wraps words etc.) When I edit a file that has no line endings this can create a readable document for me.
评论 #37019355 未加载
评论 #37022216 未加载
AdmiralAsshat将近 2 年前
> :g/^\s*$/d<p>I believe I've used a similar 'delete empty lines command' in the past, but it was just:<p><pre><code> :g/^$/d
</code></pre>
Can someone explain what the OP's regex is doing differently?
评论 #37017655 未加载
评论 #37018101 未加载
spoonfeeder006将近 2 年前
> A simple vim one liner...<p>> :%s/\w\+/\=submatch(0)+1/g | echo line('$')<p>Uh, thats..... <i>simple</i>???
评论 #37019574 未加载
评论 #37018246 未加载
MrVandemar将近 2 年前
Here's my favourite:<p><pre><code> vi"
</code></pre>
If your cursor is within two quotes on a line, it will select everything inside those quotes to (c)hange, (y)ank or (d)elete or whatever you want. Great if you're the sort of person who writes HTML by hand all the time.
评论 #37017877 未加载
评论 #37017897 未加载
评论 #37018460 未加载
评论 #37017889 未加载
fuzztester将近 2 年前
For programming in languages where indentation means a nested block:<p>>><p>and<p><<<p>indent the current line right and left by one indent position respectively, typically a tab or 4 spaces, though I need to check whether it works for spaces.<p>Prefixing those with a number n will indent n lines, starting with the current one.
评论 #37018644 未加载
teo_zero将近 2 年前
I don't understand the first one:<p><pre><code> :%s/\w\+/\=submatch(0)+1/g | echo line('$')
</code></pre>
It doesn't seem to work -- except it says "xxx substitutions", so you can assume it found exactly xxx words. Besides, it changes the original text so you need to undo. Lastly, why is it so complicated? What is the purpose of submatch()? And why echoing the number of the last line?
评论 #37020416 未加载
Lio将近 2 年前
Vim oneliners are useful thing to know even when Vim provides other ways to achieve the same thing. e.g. g ctrl-g for word count.<p>What's useful about learning and thinking about Vim's command line is the overlap with tools such as sed, which also descends from the ed command.<p>Being about to use sed is REALLY useful even if it's just for simple Unix pipelines like renaming multiple files or branches in git.
mr_o47将近 2 年前
Hello HN,<p>I'm the author of this post, and I hope you enjoyed reading it.<p>I wanted to share this one-liner for fun:
:%s/\w\+/\=submatch(0)+1/g | echo line('$')<p>Although I know there are more efficient ways to count words in a file using Vim, I wanted to be a little creative.<p>Lastly, I want to express my deep sadness over the passing of Bram Moolenaar. He created an extraordinary text editor that had a profound impact.<p>Rest in Peace Bram Moolenaar
评论 #37019247 未加载
scop将近 2 年前
One of the beautiful things about vim is that they have multiple ways to skin the cat and, as such, you develop your own mode of using it.<p>For example to navigate within a single line you can:<p>- f[character] (jump to character)<p>- w/e (jump word by word)<p>- A (jump to end, navigate from there)<p>- / (search!)<p>- h/l (move left/right)<p>- even more ways!<p>What’s funny is that, theoretically, some of these movements are more “efficient”, for example jumping to a specific character is a lot faster than going character by character. However, I found that I often go character by character or searching instead of f[] just because my brain finds it easier to move that way than to think “what character is that”. I use massive jumps when finding a line; but once on that line, I use very slow movements. Now, it’s totally less efficient, but it’s how my brain/fingers navigate most naturally even after repeated attempts at to train myself to use “better” methods. Perhaps my habits will change over time, but regardless it is remarkable how “personal” vim can be.<p>Thank you Bram!<p>(Edited to fix list format)
评论 #37018424 未加载
评论 #37019721 未加载
jiannengli将近 2 年前
Vim's primitives are quite powerful for digging through logs too. Here are some of my favorite one-liners:<p><a href="https://swordandsignals.com/2021/01/10/vim-read-logs.html" rel="nofollow noreferrer">https://swordandsignals.com/2021/01/10/vim-read-logs.html</a>
user3939382将近 2 年前
I tried to switch to it as my PHP IDE from PhpStorm, replicating each feature with config and plugins. Finally I realized I couldn’t get EA Inspections working in vim, the interface just doesn’t exist. JetBrains has a new CI tool for their inspections so that may provide a way with enough effort.
schaefer将近 2 年前
in command mode: gqq<p>Reformats the current long line into a paragraph of the specified width. If a word overruns the column width limit, the entire word is moved to the next line.
评论 #37017762 未加载
评论 #37017767 未加载
评论 #37020015 未加载
kagevf将近 2 年前
I like using `q:` to get at the command history, and recently I realized `ctrl-w _` also works there to get a nearly full screen view of the history!
评论 #37017721 未加载
aulin将近 2 年前
Pretty confused about this post and comments. Seems everyone is just using custom regexps or piping buffers to external commands. That's how I as an emacs user occasionally use vim. And that's because I don't know vim at all and this way I can work around my ignorance.<p>I refuse to believe vim power users use it like this.
评论 #37020885 未加载
评论 #37020477 未加载
评论 #37020125 未加载
gsuuon将近 2 年前
This is a neat one-liner if you don't want to install vim-fugitive on a temporary machine just for 'Gvdiff': set diff crb | vnew | exec "r !git show HEAD:#" | set diff crb<p>Shows git diff with head - modify git show command to taste.
tyingq将近 2 年前
%s/ \s\+$// comes up for me a fair amount (trim trailing whitespace)
评论 #37017566 未加载
评论 #37017452 未加载
tyingq将近 2 年前
Also handy is this to run the current buffer through an external command:<p><pre><code> %! somecommand
</code></pre>
So, for example, using perl to make everything uppercase:<p><pre><code> %! perl -pe '$_=uc'</code></pre>
评论 #37025400 未加载
评论 #37020064 未加载
Aperocky将近 2 年前
Shouldn't these condensed in the vimrc with a suitable shortcut to avoid needing to remember regex like strings?
评论 #37020063 未加载
评论 #37020053 未加载
IshKebab将近 2 年前
Pretty sure I could do all of these easier in VSCode, except.maybe adding up all the numbers in a file.
评论 #37020498 未加载
评论 #37019830 未加载
Zardoz84将近 2 年前
My little one liners :<p>removing trailing spaces :
:%s/\s+$//e<p>replacing tabs for spaces :
:%s/\t/ /g
评论 #37020819 未加载
评论 #37020198 未加载
mvanbaak将近 2 年前
> :g/\(\d\{1,3}\.\)\{3}\d\{1,3\}/y A<p>This is IPv4 only. Have one for v6 as well?
评论 #37030416 未加载
cycomanic将近 2 年前
Im confused by the first one, what's wrong with g CTRL-g?
meindnoch将近 2 年前
<p><pre><code> :%!python -m json.tool
</code></pre>
I prefer<p><pre><code> :%!jq .</code></pre>
paulsutter将近 2 年前
vi and vim are atrociously bad. y’all got stockholm syndrome<p>vim may be 10% less awful, but it’s still garbage<p>> When using Vi in insert mode, you cannot move around without first hitting Esc then again i or a. Vim fixes this by allowing you to use the arrow keys to move around in a file while in insert mode.
评论 #37020936 未加载