Assume that you have a text file named test.txt
with a list of numbers:
5
6
1
3
4
5
1
To calculate the sum in terminal, you can use:
cat test.txt | awk '{sum += $1} END {print sum}'
For more, refer to StackExchange Answer,
Assume that you have a text file named test.txt
with a list of numbers:
5
6
1
3
4
5
1
To calculate the sum in terminal, you can use:
cat test.txt | awk '{sum += $1} END {print sum}'
For more, refer to StackExchange Answer,
Aside from the usual :help
command, you can also use :helpgrep
to search for lines containing a certain pattern.
See :help helpgrep
to read up on how to navigate through all matches.
Tags: vim
If you’re writing a lot of vanilla JavaScript like me, there will be time you need to get the url query. For instance, for the below url:
www.google.com?query=apple
We want to get the value of query
like this:
urlParams.get("query")
//=> "apple"
URLSearchParams
URLSearchParams
is a native interface to work with query string. It also has a good browser support.
var urlQueryString = window.location.search
// => "?query=apple"
var urlParams = new URLSearchParams(urlQueryString)
// Now you can get `query` by
urlParams.get("query")
// => "apple"
I learned about this from this Stackoverflow question.
For more detail, refer to MDN Web Docs.
Tags: javascript js
In some occasion, after making some changes to different files, we might want to apply some operations only on the changed files.
Assuming you’re using git
for version control, we can use git diff --name-only
to get a list of changed files.
In shell, we can use $(<command)
to run a command and capture its output.
Combining these two, we can apply an operation on changed files by:
$ <command> $(git diff --name-only)
Previously I am not using any code formatter on my client project. Even though, I can run the formatter directly on the whole project, I am afraid that some code might break (since I don’t have a full test coverage).
Hence, I plan to slowly format my code whenever I make any changes with this command:
bundle exec standardrb $(git diff --name-only) --fix
Assuming that you love using VIM
and the terminal, here’s a cool mode that can be activated on Zsh or any shell rather. (I am using Zsh, so, I will talk about it)
Enable vim mode
on Zsh by adding this line into your .zshrc
.
bindkey -v
Basically this means to enable vim mode
When this is enabled, refresh your shell and you can insert texts after pressing i
and if you want to run any text-object related moves, just press Esc
and the normal hjkl, b, w, ciw, diw, ^, 0, $
will work! Exactly like in vim!