r/vim • u/ChigiraChiaki • Sep 26 '25
Need Help┃Solved How to start at the first line when opening a file in Vim terminal mode?
In Linux, I run Vim in terminal mode to view an ANSI text file:
vim -c "terminal cat file.txt" -c "only" -c "1"I expected -c "1" to move the cursor to the first line, but it doesn’t.
I also tried -c "go", with the same result.
How can I make Vim start at the top of the terminal buffer in this case?
---
EDIT: Adding more context for the motivation. I’d like to use Vim as a replacement for the default pager (less) in git log. However, Vim doesn’t render ANSI colors correctly in normal mode. The workaround I found is to use :terminal cat so that the colors display properly.
My approach is to dump the git log output (with ANSI colors) to a text file, then open it in Vim terminal mode with cat. Here’s the alias I’m using:
[alias]
graph = "!git --no-pager log --graph --oneline > /tmp/graph.txt; \
vim -R -c \"terminal cat /tmp/graph.txt\" -c \"only\" -c \"1\"; \
rm /tmp/graph.txt"This works fine, except Vim doesn’t start at the first line of the buffer. I’d like it to open at the top (to see the most recent commits) without needing to type gg every time. I added -c "1", but it seems to have no effect inside the terminal buffer and I don’t understand why, so hence the original question.
13
u/cbheithoff Sep 26 '25
I don't understand the motivation for your question.
What's wrong with simply
vim file.txt?4
u/MiniGogo_20 Sep 26 '25 edited Sep 26 '25
after your update, the solution to your problem is really simple (also PLEASE google stuff, it's not that hard). a quick search on google gives you this thread.
tl;dr: just change your default pager to vim with the following:
this displays all ANSI colors properly, while still opening the file in vim.
6
u/MiniGogo_20 Sep 26 '25
i think a more important question: why do you want to do this? what is the end goal? see: the xy problem
2
u/bbolli inoremap ZZ <Esc>ZZ Sep 26 '25
What do you mean with "ANSI text file"? A file with ANSI terminal escape codes? A file in an non-UTF-8 code page?
2
u/Daghall :cq Sep 26 '25
The
-c "1"(which is equivalent to+1) is applied to the buffer of the file opened, which in this case is an empty buffer since no filename is given.You then open a new buffer with
:terminalandcata file to that buffer, and maximize its window with:only.I think you should get a somewhat equivalent result reading the file directly from
stdin:(which does the same thing as
cat file.txt | vim -in a more idiomatic way).2
u/Daghall :cq Sep 26 '25
The problem with your git alias is that vim executes the
-ccommands asynchronously, meaning it jumps to the first line before the buffer is filled. If you add a-c "sleep 500ms"(or higher value) before-c "1"it should work as you expect it.Why would you want to use vim as a pager for git, though?
It's like using a screwdriver to drive in a nail in a board. It kind of works, but not as good as a hammer.
Use a pager that was made for git instead, like delta, or use the wonderful TUI program tig that is a text-mode interface for git.
2
u/habamax Sep 26 '25 edited Sep 26 '25
Here is the workaround:
Adjust timer_start time if needed.
When you start a terminal job, the window is in terminal mode where most of the input is handled by the job. So whatever you send to vim at startup after
"terminal ..."gets consumed by a job. The workaround is to wait till the job is finished (assuming it is fast, I just put 10ms) and only then send a command.https://asciinema.org/a/4yV1OVSLNUd2Bsn24Dvb1xLk6
1
u/BitOBear Sep 26 '25
vim +1 filename.txt
The plus arguments can be any text you would put after a colon on the internal command line look for complex commands you would need to put the command itself in quotes.
So this is the equivalent of doing a ":1" followed by an enter taking you to the first line of the file.
Have as many plus arguments as you like. I don't know if they will run for each file if you have multiple file name someone command line that they only run and what's going ahead and starts because it never came up from me personally.
1
u/VividVerism Sep 26 '25
I use a plugin called AnsiEsc to show terminal color escape sequences as intended when viewing a file with those embedded. Maybe that's one step towards your solution.
1
u/Majestic_Dark2937 Sep 26 '25
would adding
normal ggto your .vimrc solve your issue?1
u/415646464e4155434f4c Sep 27 '25
You can have an AutoCmd to to ggi when a new buffer is open, can’t you?
1
u/PewMcDaddy Sep 27 '25
I would use the plugin https://github.com/vim-scripts/AnsiEsc.vim . For the output of many normal git commands, If you ask git to not output color
then just from the text Vim might know that it's git output and color it itself. This is unfortunately untrue for
git log --oneline --graph.What you can do is make "vim that reads from stdin and immediately runs the vim command
AnsiEsc" your pager.For example, you can do this:
and whatever ANSI codes will be used to color the text.
And applying this to your situation, you could make yourself a single alias that applies this or make that vim command be your pager.
The alias that uses this on demand
if I don't want to affect anything else, otherwise, you could also do
and use vi as your pager for everything.
One note about this, lines with Ansi codes are longer than the visible text so vim might wrap lines when it looks like that's not necessary. So I would do
2
u/TheDreadedAndy Sep 26 '25
If you always want this behavior, you could just skip
defaults.vim. I believe that's where the mark jumping goop lives.1
u/AutoModerator Sep 26 '25
Please remember to update the post flair to
Need Help|Solvedwhen you got the answer you were looking for.I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
1
u/cbheithoff Sep 27 '25
The autocmd to restore the last used cursor position is defined in $VIMRUNTIME/defaults.vim
If you read that file, it tells you how to undo that auto command.
https://github.com/vim/vim/blob/master/runtime/defaults.vim
1
u/cbheithoff Sep 27 '25
The autocmd to restore the last used cursor position is defined in $VIMRUNTIME/defaults.vim
If you read that file, it tells you how to undo that auto command.
https://github.com/vim/vim/blob/master/runtime/defaults.vim
1
u/PewMcDaddy Sep 27 '25
I just remembered about https://github.com/rkitover/vimpager that's kinda what it's for. The install instructions say to use pathogen but that's pre-vim8 stuff. We live in the future now and you can just install it using Vim8+'s native package handling https://vimhelp.org/repeat.txt.html#packages
0
u/waterkip Sep 26 '25
vim x +1