News for hackers. Stuff that matters.
Re:Mastering Bash and Terminal by Normal_gaussian on Saturday January 14, 2017 @04:09PM (#13400569)
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"
Re:Mastering Bash and Terminal by EvilTerran on Saturday January 14, 2017 @05:15PM (#13400892)
My 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.)

Re:Mastering Bash and Terminal by u801e on Saturday January 14, 2017 @05:49PM (#13401057)
I 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.
Re:Mastering Bash and Terminal by EvilTerran on Saturday January 14, 2017 @06:25PM (#13401246)
Well, 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.

Re:Mastering Bash and Terminal by blockloop on Saturday January 14, 2017 @06:27PM (#13401255)
There is also a silver searcher plugin for vim (ag).
Re:Mastering Bash and Terminal by JdeBP on Sunday January 15, 2017 @05:40AM (#13403413)
With 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).
Re:Mastering Bash and Terminal by akavel on Sunday January 15, 2017 @07:55AM (#13403765)
For any commands or pipelines, you can use:

    :cex system('git grep -n foo | grep bar') | copen
then :cn and :cp to step through the list easily.
Re:Mastering Bash and Terminal by blockloop on Saturday January 14, 2017 @04:26PM (#13400654)
I 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.

Re:Mastering Bash and Terminal by u801e on Saturday January 14, 2017 @05:10PM (#13400863)
> 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.
Re:Mastering Bash and Terminal by EvilTerran on Saturday January 14, 2017 @05:49PM (#13401052)
This 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.
Re:Mastering Bash and Terminal by dotancohen on Sunday January 15, 2017 @02:10AM (#13402788)
Though 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
Re:Mastering Bash and Terminal by jacobmischka on Sunday January 15, 2017 @02:31AM (#13402851)
Only if it's compiled with clipboard support. I don't know about in other places, but in the default Arch package it's not. So this command is a perfect solution for me.
Re:Mastering Bash and Terminal by Kubuxu on Sunday January 15, 2017 @07:23AM (#13403700)
You need to install `gvim` which provides `vim` with that feature.
Re:Mastering Bash and Terminal by u801e on Sunday January 15, 2017 @02:36AM (#13402868)
That requires that vim be compiled with the xterm-clipboard feature enabled. On my Fedora 24 desktop, vim doesn't have it enabled, but gvim does (according to the output of the :version command).

One advantage of the method you describe is that it can be limited to just part of a line. Piping output to xclip is limited to complete lines of text due to the nature of :w !.

Re:Mastering Bash and Terminal by ves on Saturday January 14, 2017 @07:51PM (#13401625)
for osx peeps: `:w !pbcopy`
Re:Mastering Bash and Terminal by leni536 on Saturday January 14, 2017 @05:54PM (#13401088)
> That's usually when I'll suspend and then use the command line.

That's when I run !bash, enter my list of commands, then exit.

Re:Mastering Bash and Terminal by JdeBP on Sunday January 15, 2017 @05:43AM (#13403420)
If one wants something that isn't limited to one shell, and invokes the interactive shell that the user has chosen, there is of course

    :shell
Re:Mastering Bash and Terminal by vram22 on Saturday January 14, 2017 @06:22PM (#13401235)
Yes, 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 ). 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.

Re:Mastering Bash and Terminal by frahs on Saturday January 14, 2017 @09:55PM (#13402048)
You 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

Re:Mastering Bash and Terminal by 77pt77 on Sunday January 15, 2017 @07:04AM (#13403643)
not only that but it also interprets the errors and goes to the correct line

have a look at :cw[indow]

Re:Mastering Bash and Terminal by u801e on Saturday January 14, 2017 @05:04PM (#13400833)
Regarding 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.
Re:Mastering Bash and Terminal by lenkite on Sunday January 15, 2017 @04:40AM (#13403255)
With tpope's fugitive.vim this is just :Gstatus and :Gcommit
Re:Mastering Bash and Terminal by fragmede on Saturday January 14, 2017 @07:52PM (#13401632)
That's interesting, why not just do `git commit` at the command line?
Re:Mastering Bash and Terminal by u801e on Saturday January 14, 2017 @08:55PM (#13401889)
It'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).
Re:Mastering Bash and Terminal by branchless on Saturday January 14, 2017 @09:18PM (#13401960)
Vim users are breaking out to the shell to use git?

Try emacs with magit. It's really great.

Re:Mastering Bash and Terminal by hueving on Sunday January 15, 2017 @10:19AM (#13404348)
It's easier to embrace the Unix philosophy when your text editor doesn't take a half hour to start.
Re:Mastering Bash and Terminal by branchless on Sunday January 15, 2017 @10:38AM (#13404447)
It 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.
Re:Mastering Bash and Terminal by hueving on Monday January 16, 2017 @05:55PM (#13413767)
I was replying with a glib remark because the suggestion to use emacs instead was stupid to begin with.
Re:Mastering Bash and Terminal by branchless on Monday January 16, 2017 @08:22PM (#13414478)
Vim users should check it out, even if only for inspiration for a clone.
Re:Mastering Bash and Terminal by eikenberry on Saturday January 14, 2017 @06:38PM (#13401312)
You can also use :sh which just starts a new subshell in the terminal and puts you back in vim when you exit it.
Re:Mastering Bash and Terminal by vlunkr on Saturday January 14, 2017 @08:57PM (#13401897)
Didn't know that! That's great.
Re:Mastering Bash and Terminal by renlo on Sunday January 15, 2017 @12:41AM (#13402543)
tmux 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.
Re:Mastering Bash and Terminal by oftenwrong on Sunday January 15, 2017 @11:01AM (#13404539)
(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
Re:Mastering Bash and Terminal by u801e on Sunday January 15, 2017 @01:42PM (#13405557)
It's interesting to read up on the relationship between ed, sed, ex, and vi. In fact, all 4 of those editors have similar ways of addressing line ranges (1 for the first line, $ for the last line, etc).

One interesting feature many people may not know about is that it's possible to address lines using a regular expression.

For instance, if you had the following text in a vim buffer:

    one
    two
    three
    four
    five
and you wanted to delete the lines between "two" and "four" inclusive, you could run the following command in vim:

    :/two/,/four/d
You could also do the same thing in sed

    sed '/two/,/four/d' filename
Re:Mastering Bash and Terminal by JepZ on Sunday January 15, 2017 @06:39AM (#13403587)
I 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.
Re:Mastering Bash and Terminal by antman on Sunday January 15, 2017 @04:07AM (#13403153)
If only tmux loaded my environment and not purify it by design
Re:Mastering Bash and Terminal by palunon on Saturday January 14, 2017 @08:54PM (#13401884)
> :!make

IMO, :make is (usually) better, because it adds errors to the quickfix list, and put your cursor on the first error location.

Re:Mastering Bash and Terminal by Normal_gaussian on Saturday January 14, 2017 @09:11PM (#13401946)
In all honesty, I've never actually done :!make (or :make) as I nearly always have it wrapped behind my own build scripts
Re:Mastering Bash and Terminal by jmcphers on Saturday January 14, 2017 @10:13PM (#13402093)
You 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-w...

Re:Mastering Bash and Terminal by justinmk on Saturday January 14, 2017 @04:21PM (#13400628)
You can also use the ":terminal" command (":te" for short) in Neovim.
Re:Mastering Bash and Terminal by TAForObvReasons on Saturday January 14, 2017 @04:18PM (#13400608)
You 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 -
Re:Mastering Bash and Terminal by aithoughts on Saturday January 14, 2017 @09:29PM (#13401986)
Interesting. 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.

Re:Mastering Bash and Terminal by juki on Sunday January 15, 2017 @03:19AM (#13402986)
As 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.
Re:Mastering Bash and Terminal by DCoder on Sunday January 15, 2017 @09:28AM (#13404100)
Re:Mastering Bash and Terminal by Sir_Cmpwn on Saturday January 14, 2017 @04:16PM (#13400594)
FYI :make also works
Re:Mastering Bash and Terminal by JetSpiegel on Saturday January 14, 2017 @04:30PM (#13400676)
If you want to use git from within vim, go no further than Fugitive.
Re:Mastering Bash and Terminal by RandomInteger4 on Saturday January 14, 2017 @07:34PM (#13401556)
God, 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.
Re:Mastering Bash and Terminal by matt_wulfeck on Saturday January 14, 2017 @07:39PM (#13401573)
I 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!

Re:Mastering Bash and Terminal by makecheck on Saturday January 14, 2017 @04:15PM (#13400590)
You can also install a plug-in like ConqueTerm to devote an entire in-vim buffer to something like a shell session.
Re:Mastering Bash and Terminal by skierscott on Saturday January 14, 2017 @04:17PM (#13400602)
Or you can use tmux (which can be have vim bindings) to run vim in one pane.
Re:Mastering Bash and Terminal by blockloop on Saturday January 14, 2017 @04:22PM (#13400632)
Tmux is great. I have another writing in progress on my tmux workflow.
Re:Mastering Bash and Terminal by JdeBP on Sunday January 15, 2017 @05:31AM (#13403386)
I more often

    :make
nowadays rather than

    :!make
Re:Mastering Bash and Terminal by cyberpunk on Saturday January 14, 2017 @08:18PM (#13401756)
Hmm, I always used :.!ls instead of :r!ls...

Is there a difference?

Re:Mastering Bash and Terminal by rbonvall on Saturday January 14, 2017 @08:37PM (#13401828)
:.!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.