TIL
Switch configs in neovim
08.11.2023Neovim recently got a feature for selecting different neovim configs based on an environment variable. The variable is NVIM_APPNAME
and it is set to “nvim"
as default. It is used in the path when looking up configuration like this ~/.config/{NVIM_APPNAME}
. Thus, if it is set to lazyvim the config will be loaded from ~/.config/lazyvim
.
To use it with vim, prefix the nvim command with the environment variable.
NVIM_APPNAME=lazyvim nvim
This is super useful when testing out different premade configs, but it can also be useful when making changes to the config without disrupting the daily workflow by having two clones of your own config into ~/.config/nvim
and ~/.config/nvim-test
zshenv
24.10.2023zshenv
is similar to zshrc
, but it is loaded for all kinds of shells. zshrc
on the other hand is only sourced for interactive shells. I discovered this while trying to use mosh-server on a mac. mosh-server is installed with brew so the binary is in /opt/homebrew/bin/
and since I had only setup the path for homebrew in zshrc
it was not accessible through non-interactive ssh connections that the mosh clients use to start the mosh-server. Now my zshenv
looks like this:
PATH=/opt/homebrew/bin/:$PATH
Always use os clipboard in neovim
23.10.2023In neovim it is possible to use the os clipboard manually, but it is also possible to make it the default behaviour with:
Viml
set clipboard+=unnamedplus
Lua
vim.opt.clipboard = 'unnamedplus'
See :help clipboard
for more.
Pipe in neovim
10.10.2023I wish I new about this way sooner. It is possible to chain tasks with pipe(|
) in neovim. It can be superuseful for running tasks on the current file. I often use it for running tests in a python project now:
:w | !pytest %
This will first write the file and then run tests on the current path.
sudo -k and sudo -K
06.10.2023I recently found something pretty useful in sudo that I wish I new about ages ago. This is especially useful for dotfiles scripts and similar things where you need sudo to do that one thing, like disable the sound effect on boot of an apple device. The problem with regular sudo is that evrything that happens after that will have a sudo session so if some thing that is downloaded from the internet like a package tries sudo in its install script it will just work.
From sudo --help
the following is printed:
-K, --remove-timestamp remove timestamp file completely-k, --reset-timestamp invalidate timestamp file
sudo -K
This will completely remove the timestamp of the current sudo session. Thus, it is not possible to use together with a command since the command will not be able to run before the timestamp is deleted.
sudo -k
This will invalidate the timestamp by changing it to the finish time of the last command. This means that it can both be used standalone sudo -k
and with a command like sudo -k apt install sl
.
To globally mock functions in python
05.10.2023In one of the python projects I am working on we have this central method to publish events to pubsub. We wanted to have a global mock of pubsub to avoid having to explicitly mocking it many places. Turns out this is possible with a wrapper function and pytest fixtures:
def _publish_pubsub_event(topic: str, message: PubSubMessage) -> None: PubSubPublisher(settings.GCLOUD_PROJECT, topic).publish(message)
def publish_pubsub_event(topic: str, message: PubSubMessage) -> None: return _publish_pubsub_event(topic, message)
The publish_pubsub_event
gets imported everywhere so to mock it we need to mock it in the context is being used, but the _publish_pubsub_event
is not being used anywhere else than here so we can mock it in the context of this file.
In the module level conftest.py
there is a fixture with autouse=True
and it mocks the _publish_pubsub_event
and returns the mock so that the fixture can be loaded explicitly to assert that the mock was called in the cases we want to verify the messages sent.
@pytest.fixture(autouse=True)def mock_pubsub_publish(mocker: MockerFixture): return mocker.patch("app.pubsub._publish_pubsub_event")
Switch to previous session in tmux
14.02.2023I recently setup <prefix> +
to switch to the previous tmux session. Super handy to switch fast between two projects, for example backend and frontend.
bind + switch-client -l
tx
03.02.2023I have recently been trying to do a proper setup of tmux and as a part of it i made this little shell function to make re-attaching sessions a bit easier. It will give fzf prompt with available sessions if called without a session name. On the other hand when called with a session name it will check if it exists and attach or create based on the result.
tx() { if [ -z ${1+x} ]; then session=$(tmux list-sessions | fzf | awk '{print $1}' | sed 's/:$//' ) tmux a -t $session else if tmux list-sessions | grep $1 2>&1 > /dev/null; then tmux a -t $1 else tmux new -s $1 fi fi}
_tx() { sessions=$(tmux list-sessions 2>/dev/null) if [[ -z "$1" ]]; then echo $sessions | awk '{split($0,a,":"); print a[1]}' else echo $sessions | awk '{split($0,a,":"); print a[1]}' | grep --color=never $1 fi}
complete -C _tx tx
Alias filetypes
29.01.2023Creating aliases with -s
enables aliasing commands too files types. Thus, with the alias alias -s txt=less
turns ./file.txt
into less ./file.txt
Other examples:
alias -s md=glowalias -s html=openalias -s txt=lessalias -s json=jq
Recent branches with fzf
21.01.2023Not something I learned today, but i recently configured a alias for recent branches with fuzzy search with fzf
. It will show the branches sorted by the date of the head commit when doing git recent
. Selecting one with fzf will then do a git checkout of that branch.
Add the following under aliases in gitconfig:
recent = "!git branch --sort=-committerdate --format=\"%(committerdate:relative)%09%(refname:short)\" | fzf --ansi | awk 'BEGIN{FS=\"\t\"} {print $2}' | xargs git checkout"
zz in vim centers line on screen
15.01.2023Learned today that in normal mode in vim typing zz
will center the active line in the middle of the screen.