Live data from Hacker News

Mastering Bash and Terminal

blockloop.io

41–50 of 186 posts

Re: Mastering Bash and Terminal

#41

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/ot…

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 ). 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 th…

> 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

#42
post #14

The article is nice but a small part of it rubs me the wrong way: > I know there are some cool newcomers out there like zsh and fish, but after trying others out I always found that some of my utilities were missing or ill-replaced. First of all bash was first released in 1989 and zsh arrived just 1 year later so zsh is in no way a newcomer. Secondly zsh is almost strictly a bash superset so I don't know what he was…

I wasn't so much commenting on the release date when I said "newcomers." Mostly commenting on my personal experience. I've been using bash for a long time and only recently have I seen zsh get so popular. It's entirely possible that only the people I know have only recently taken an interest in it. I would have to run zsh again to remember the pieces that I didn't like. I might have been able to configure my way arou…

When I started using Unix in the early '90s, zsh was more popular than bash by a long way. And tcsh was more popular still. You had to be a GPL purist or something to want to use bash. Its success is really down to its status as the Linux default. If you try zsh again, you'll find the mailing list and IRC are full of helpful people if some things aren't as you expect.

Re: Mastering Bash and Terminal

#43
post #25
post #14

The article is nice but a small part of it rubs me the wrong way: > I know there are some cool newcomers out there like zsh and fish, but after trying others out I always found that some of my utilities were missing or ill-replaced. First of all bash was first released in 1989 and zsh arrived just 1 year later so zsh is in no way a newcomer. Secondly zsh is almost strictly a bash superset so I don't know what he was…

On my version of zsh I was not able to do the following which is available on bash: http://unix.stackexchange.com/a/203075

Binding the vi widget in emacs mode works:

    bindkey '^]' vi-find-next-char

Re: Mastering Bash and Terminal

#45

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/ot…

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

#46

For changing directories, you can also use something like z ( https://github.com/rupa/z ) to jump around: Tracks your most used directories, based on 'frecency'. After a short learning phase, z will take you to the most 'frecent' directory that matches ALL of the regexes given on the command line, in order. For example, z foo bar would match /foo/bar but not /bar/foo.

Also fasd, which has a z-compatible interface and does a little more: https://github.com/clvv/fasd

Re: Mastering Bash and Terminal

#47
post #39

Earlier quoted context omitted.

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…

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.

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

#48
post #39

Earlier quoted context omitted.

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…

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.

There is also a silver searcher plugin for vim (ag).
Post reply on HN