In this post, we will use nvim
with the power of git
to make our development
faster.
Open git
changed files with nvim
Often times I find myself working in a git project where I have to (pretty much
always) open modified (relevant) files for working. I use NeoVim
as my
primary editor, which is why I have aliased the nvim
command which opens only
relevant files in horizontal splits:
# This command opens untracked, modified and deleted files in horizontal splits
alias nvimch='nvim -o $(git status --porcelain | sed s/^...//)'
If you prefer vertical splits you can use this command:
# This command opens untracked, modified and deleted files in vertical splits
alias nvimch='nvim -O $(git status --porcelain | sed s/^...//)'
You can also open these files in nvim tabs using this command:
# This command opens untracked, modified and deleted files in tabs
alias nvimch='nvim -p $(git status --porcelain | sed s/^...//)'
Smart Splits
When the number of relevant files reaches 5-6, it starts to get bad when having only one kind of split. Which is why I wrote a (kind of) smart split command.
The premiss of this command is to get nvim
to open a limited number of
vertical splits, say 2 and delegate next 4 to horizontal splits. In this way,
we will have only 6 splits at a time: two vertical and three horizontal.
This can be done using the following command:
# This command opens untracked, modified and deleted files in smart splits
alias nvimch='nvim -O2 $(git status --porcelain | sed s/^...// | head -2) \
-c "args $(git status --porcelain | sed "s/^.../.\//" | tail -n +3 | \
head -4 | tr '\n' ' ') | sp"'
Now let’s make this more dynamic with environment variables:
export NVIM_VSPLIT_LIMIT=2
export NVIM_SPLIT_LIMIT=3
# This command opens untracked, modified and deleted files in smart splits
alias nvimch='nvim -O${NVIM_VSPLIT_LIMIT} $(git status --porcelain | \
sed s/^...// | head -${NVIM_VSPLIT_LIMIT}) -c "args $(git status --porcelain \
| sed "s/^.../.\//" | tail -n +${NVIM_VSPLIT_LIMIT + 1} | \
head -${NVIM_SPLIT_LIMIT * NVIM_VSPLIT_LIMIT - NVIM_VSPLIT_LIMIT} | \
tr '\n' ' ') | sp"'
This command should allow you to smartly open relevant git files using nvim
splits.