7 ms·
Rather than temporarily suspend vim to use the terminal you can get vim to suspend and resume itself with the exclamation mark command. This has the benefit of
by Normal_gaussian 10y ago
Rather than temporarily suspend vim to use the terminal you can get vim to suspend and resume itself with the exclamation mark command. This has the benefit of not wreaking havoc on your vim session and allowing you to read data into vim by prefixing with r.
For example
:!ls
will execute ls and show you the result (press enter to return)
:r!ls
will read the result of ls in for you
More usefully
:r!sed -n5,10p that/other/file
will read lines 5-10 from that other file.
However you will most often want to
:!make
:!up build
:!git status
:!git commit -am "Fixed #23"
- TAForObvReasons 10y agoYou can also give a range before the ! and it remove the lines in the specified range and pass it to the command. For example, when reading xml I use xmllint to clean it up: :%!xmllint --format -
- makecheck 10y agoYou can also install a plug-in like ConqueTerm to devote an entire in-vim buffer to something like a shell session.
- skierscott 10y agoOr you can use tmux (which can be have vim bindings) to run vim in one pane.
- blockloop 10y agoTmux is great. I have another writing in progress on my tmux workflow.
- Sir_Cmpwn 10y agoFYI :make also works
- justinmk 10y agoYou can also use the ":terminal" command (":te" for short) in Neovim.
- blockloop 10y agoI didn't know about :r in combination with !. That's neat. I typically use :r for reading a file into the current buffer (instead of cp <file> <new>). Thanks for sharing. I don't find myself using ! commands when I know there are multiple commands that I need to run (like your last example). That's usually when I'll suspend and then use the command line. Could you elaborate on "wreak havoc on your vim session?" I wasn't aware that suspending the process and bringing it back into the fg would harm anything.
- u801e 10y ago> I didn't know about :r in combination with ! You can even use :w in combination with !. For example :w !xclip to write the vim buffer contents to the clipboard. You could visually highlight part of the buffer and do the same thing for just the text you want.
- EvilTerran 10y agoThis is also useful for iterating on a throw-away script - something too long to type on one line in the shell, but not significant enough to be worth saving to a file. Put it together in an unnamed buffer, and ":w !" it to the relevant interpreter (eg ":w !perl") whenever you want to run it.
- deleted 10y ago[deleted]
- ves 10y agofor osx peeps: `:w !pbcopy`
- dotancohen 10y agoThough I understand that this is just an example of the power of `!r`, readers should know that VIM in fact can copy to the system clipboards natively. "*y "+y
- 10y ago
- JetSpiegel 10y agoIf you want to use git from within vim, go no further than Fugitive.
- deleted 10y ago[deleted]
- deleted 10y ago[deleted]
- u801e 10y agoRegarding the last git command you listed, I actually find it better to do :r !git status -v And at the to of the result, type my formatted git commit message, visually highlight the message I just typed, and then use the following vim command :r !git commit -F - Which reads the commit message from standard input.
- fragmede 10y agoThat's interesting, why not just do `git commit` at the command line?
- u801e 10y agoIt's more of a way to be able to commit without having to exit or suspend vim. I typically type the commit message while checking the diff. If I find an error in the diff, I'll switch to the vim window with the file, correct the issue and run :!git add %, delete the original output of the git status -v command I ran earlier and I re-run that command (which I can pull up by using q: to bring up the vim command history window).
- branchless 10y agoVim users are breaking out to the shell to use git? Try emacs with magit. It's really great.
- hueving 10y agoIt's easier to embrace the Unix philosophy when your text editor doesn't take a half hour to start.
- branchless 10y agoIt has a daemon mode, so now you know. And from a flat start emacs takes less than 5 seconds on my pc which isn't that beefy.
- 10y ago
- EvilTerran 10y agoMy personal favourite trick with ":r!" is, in a new buffer, running :0r! grep -rn $pattern $path_to_directory or any other command that outputs lines containing "$path_to_file:$line_no" (eg most linters). With that output read into a buffer, placing the cursor anywhere in the "path:line" part and hitting ^wF will open that file in a new split window, and jump the cursor straight to the indicated line - or you can do ^wgF to do the same in a new tab page. It's a very handy way to work through a big list of work items spread across many files. You do need to coax the right format for it out of your tools, but fortunately "path:line" is pretty de-facto standard. (Incidentally, I do ":0r" so it inserts the read-in lines at the very top of the file - just ":r" in an empty buffer leads to a leading blank line, which I find irrationally annoying.)
- u801e 10y agoI know that vim has the built in :grep command that does what you describe. The results are listed in the preview window and pressing enter will open the corresponding file and place the cursor at the desired line.
- EvilTerran 10y agoWell, yes... okay, maybe that was a poor choice of example, :grep is probably more ergonomic for that particular use case. The trick I described is more for when you're dealing with some unusual program that wouldn't work with :grep - at least not without fiddling with 'grepprg' to invoke it, and/or 'grepformat' to correctly interpret its output.
- blockloop 10y agoThere is also a silver searcher plugin for vim (ag).
- JdeBP 10y agoWith the likes of :cfile :cbuffer one can turn that file/buffer directly into a quickfix list, where simply enter will take one to the relevant position. Messages with column positions are recognized in quickfix lists (:help errorformat).
- vram22 10y agoYes, good ones. Running ! commands can also be done in command mode, without using last line mode (those who don't know the difference can see https://gumroad.com/products/vi_quick https://gumroad.com/products/vi_quick ). I use variations of commands like this a lot, as needed: 1G (to go to top of file if not already there) !Gsort (to filter all the lines in the file through the sort command, and replace the file contents with the sorted lines) which says: filter all the lines would be traversed over as a result of running the G command (which goes to the last line in the file) through the sort command, and use the output of the sort to replace all the lines that were filtered; sort can be replaced with any other command that is a filter, e.g. fmt, grep, sed, many more; (note: no need of colon in the above command, just have to be in command mode, not input mode, which you get into by pressing Esc). Very powerful, just that feature, particularly in programming.
- eikenberry 10y agoYou can also use :sh which just starts a new subshell in the terminal and puts you back in vim when you exit it.
- vlunkr 10y agoDidn't know that! That's great.
- RandomInteger4 10y agoGod, I really need to learn more of these little tricks. I've been using Vim for a while now, but not taking advantage of so many of it's features.
- matt_wulfeck 10y agoI personally still prefer to background the process. Rarely is it ever just 1 command you want to run. For example, you want to git commit, push, oops need to rebase, oops don't have all of my files added... etc etc. For that reason I just jump out with a ctrl+z and when I'm done I'm back in with a fg!
- cyberpunk 10y agoHmm, I always used :.!ls instead of :r!ls... Is there a difference?
- rbonvall 10y ago:.!ls means "filter the current line through ls". Since ls ignores stdin, the effect is that the current line gets replaced by its output. :r!ls just inserts the output, without replacing anything.
- palunon 10y ago> :!make IMO, :make is (usually) better, because it adds errors to the quickfix list, and put your cursor on the first error location.
- Normal_gaussian 10y agoIn all honesty, I've never actually done :!make (or :make) as I nearly always have it wrapped behind my own build scripts
- jmcphers 10y agoYou can set :make to execute your own build script instead of make, and you can also tell vim how to interpret the output. See e.g.: https://vi.stackexchange.com/questions/6679/terminal-setup-when-running-make-from-vim https://vi.stackexchange.com/questions/6679/terminal-setup-w...
- aithoughts 10y agoInteresting. Your use of `git commit -am` resounded with me. I can't stand doing `git add .` ; `git commit -m "message"`; `git push`. I aliased "commit" to "ci" in git-config, and then do `git ci -am "message" && git push`. I'm sure at some point I will add a hook to have the push done automatically (though sometimes I prefer to do it manually, so I will have to make a decision.) I am an avid emacs user though. I really should check out vim.
- juki 10y agoAs an Emacs user, why not use magit? Staging, committing and pushing would be just a few key presses ('C-x g S c c <write message and C-c C-c> p P' assuming magit bound to 'C-x g'). You also get to view the status and diffs along the way.
- DCoder 10y agoYou can also look at https://github.com/scmbreeze/scm_breeze https://github.com/scmbreeze/scm_breeze
- frahs 10y agoYou actually don't have to put ! In front of make. Vim has makefile integration. If all you're doing is typing make, you can do :make. For a custom build command, you can set the variable makeprg, and :make will execute that instead. On my phone so it's hard to type examples, this blog post gives more info: https://jbernard.io/2011/09/30/vim-makeprg.html https://jbernard.io/2011/09/30/vim-makeprg.html
- 77pt77 10y agonot only that but it also interprets the errors and goes to the correct line have a look at :cw[indow]
- renlo 10y agotmux is a good alternative to the whole paradigm of 'running shell commands within a text editor like vim'. Why run a shell command within vim when one can run the shell command in an actual shell? It seems to me that running a shell command in vim is using vim outside of its intended scope. It might be able to do it but it won't be able to do it well, which is where something like tmux comes in. I might be in the minority with this viewpoint though.
- antman 10y agoIf only tmux loaded my environment and not purify it by design
- JepZ 10y agoI use vim and tmux too. I use this combination, because I can also use 'watch' and 'tail -f' commands to deploy files or watch logs while I code.
- oftenwrong 10y ago(pedantic note: When you run shell commands in vim, they are running in an actual shell.) vim can be thought of as a visual and interactive helper for your text-processing utilities. The "intended scope" of vim is text editing, and while you're editing you may want to run commands on the text in your buffer, or just parts of it, and have the results apply immediately without writing and reloading the file, or exiting. For example, in vim, you may be writing a list somewhere in your document, and you realize you want it sorted alphabetically. You can visually select the range of lines ('<,'>) that comprise the list and then sort them by calling the sort utility [1]: :'<,'>!sort And then you can continue adding to the list. What would your workflow be for the above? Some like this...? 1. write the file you are working on in your EDITOR 2. make a mental note of the absolute line numbers for the list in the document 3. open new terminal 4. call an awk command that uses `sort`, or some other script, passing in your line numbers, and the file name 5. switch back to EDITOR and reload file ---- [1] Of course vim has its own `sort` command, so you can leave out the '!' in that example to do the same. vi did not have its own `sort`, so people used the external `sort`. vi also didn't have the visual selection, so people could pick lines using ranges: :.,+10!sort i.e. sort from the current line to the line 10 lines down, inclusive
- JdeBP 10y agoI more often :make nowadays rather than :!make