refresh changed content of file opened in vi(m)
Clash Royale CLAN TAG#URR8PPP
I have a config-file that I keep open in vim, but that sometimes gets changed on disk, without these changes being reflected on the terminal. Can I refresh the content on the screen without closing and re-opening the file? If so, how?
vim
add a comment |
I have a config-file that I keep open in vim, but that sometimes gets changed on disk, without these changes being reflected on the terminal. Can I refresh the content on the screen without closing and re-opening the file? If so, how?
vim
1
Related: How can I reload all buffers at once? at Vim SE
– kenorb
Apr 18 '15 at 21:06
add a comment |
I have a config-file that I keep open in vim, but that sometimes gets changed on disk, without these changes being reflected on the terminal. Can I refresh the content on the screen without closing and re-opening the file? If so, how?
vim
I have a config-file that I keep open in vim, but that sometimes gets changed on disk, without these changes being reflected on the terminal. Can I refresh the content on the screen without closing and re-opening the file? If so, how?
vim
vim
edited Aug 8 '14 at 11:40
twan163
asked Aug 8 '14 at 11:28
twan163twan163
2,5203915
2,5203915
1
Related: How can I reload all buffers at once? at Vim SE
– kenorb
Apr 18 '15 at 21:06
add a comment |
1
Related: How can I reload all buffers at once? at Vim SE
– kenorb
Apr 18 '15 at 21:06
1
1
Related: How can I reload all buffers at once? at Vim SE
– kenorb
Apr 18 '15 at 21:06
Related: How can I reload all buffers at once? at Vim SE
– kenorb
Apr 18 '15 at 21:06
add a comment |
3 Answers
3
active
oldest
votes
You can use the :edit
command, without specifying a file name, to reload
the current file. If you have made modifications to the file, you can use:edit!
to force the reload of the current file (you will lose your
modifications).
The command :edit
can be abbreviated by :e
. The force-edit can thus be done by :e!
2
The "!" was tripping me, but now it's clear.
– twan163
Aug 8 '14 at 11:41
106
Note: that's typically abbreviated to:e!
.
– Stéphane Chazelas
Aug 8 '14 at 12:07
31
@StephaneChazelas: Yeah,That's correct :) @twan163: Instead of:edit
and:edit!
you can use:e
and:e!
respectively.
– Thushi
Aug 8 '14 at 12:08
10
Lose modifications, yes and no. Crucially, you can stillu
ndo the:e!
.
– gerrit
Sep 10 '15 at 9:22
5
Also, if you want to reload all your buffers, run::bufdo e
– pkout
Nov 4 '16 at 21:43
|
show 3 more comments
In addition to manually refreshing the file with :edit
, you can put into your ~/.vimrc
:set autoread
to make Vim automatically refresh any files that haven't been edited by Vim. Also see :checktime
.
2
Buyer beware -- If you're working on fresh code andgit pull
be aware you could lose your unsaved changes on screen rather unintentionally.
– azatar
Jun 16 '16 at 17:36
4
@toszter No, Vim will only refresh unchanged buffers. In case of changes, there will still be a query: Keep, or load?
– Ingo Karkat
Jun 17 '16 at 7:28
7
N.B. autoread doesn't exactly work automatically. You either have to usegvim
, or run external commands.
– Sparhawk
Sep 30 '16 at 1:37
3
Those using vim inside of tmux can get focus events by using github.com/tmux-plugins/vim-tmux-focus-events . Otherwise autoread won't help in the terminal unless you somehow call:checktime
– Karl Bartel
Jan 3 '17 at 15:50
3
autoread
can be auto-triggered X seconds after the cursor stops moving, see this answer.
– Tom Hale
Aug 1 '17 at 6:16
|
show 3 more comments
TL;DR
Skip to the Wrap-up
heading for the vimrc
lines to add to do make your life better.
Manually
Run :checktime
Check if any buffers were changed outside of Vim.
This checks and warns you if you would end up with two
versions of a file.
Automatically
To do automatically load changes, add in your vimrc
:
set autoread
When a file has been detected to have been changed outside of Vim and
it has not been changed inside of Vim, automatically read it again.
When the file has been deleted this is not done.
This answer adds a caveat:
Autoread does not reload file unless you do something like run external command (like
!ls
or!sh
etc)
Read on for solutions.
Trigger when cursor stops moving
Add to your vimrc
:
au CursorHold,CursorHoldI * checktime
By default, CursorHold is triggered after the cursor remains still for 4 seconds, and is configurable via updatetime.
Trigger on buffer change or terminal focus
Add the following to your vimrc
to trigger autoread
when changing buffers while inside vim:
au FocusGained,BufEnter * :checktime
Catching terminal window focus inside plain vim
To have FocusGained
(see above) work in plain vim, inside a terminal emulator (Xterm, tmux, etc) install the plugin:
vim-tmux-focus-events
On tmux versions > 1.9, you'll need to add in .tmux.conf
:
set -g focus-events on
Wrap-up
Notifications when autoread
triggers are also possible.
Here are my vimrc
lines to implement all the above:
" Triger `autoread` when files changes on disk
" https://unix.stackexchange.com/questions/149209/refresh-changed-content-of-file-opened-in-vim/383044#383044
" https://vi.stackexchange.com/questions/13692/prevent-focusgained-autocmd-running-in-command-line-editing-mode
autocmd FocusGained,BufEnter,CursorHold,CursorHoldI * if mode() != 'c' | checktime | endif
" Notification after file change
" https://vi.stackexchange.com/questions/13091/autocmd-event-for-autoread
autocmd FileChangedShellPost *
echohl WarningMsg | echo "File changed on disk. Buffer reloaded." | echohl None
Thanks to ErichBSchulz for pointing me in the right direction with au CursorHold
.
excellent post! Feels like one of those things where this should be the default behaviour
– JonnyRaa
Nov 13 '17 at 10:30
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f149209%2frefresh-changed-content-of-file-opened-in-vim%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use the :edit
command, without specifying a file name, to reload
the current file. If you have made modifications to the file, you can use:edit!
to force the reload of the current file (you will lose your
modifications).
The command :edit
can be abbreviated by :e
. The force-edit can thus be done by :e!
2
The "!" was tripping me, but now it's clear.
– twan163
Aug 8 '14 at 11:41
106
Note: that's typically abbreviated to:e!
.
– Stéphane Chazelas
Aug 8 '14 at 12:07
31
@StephaneChazelas: Yeah,That's correct :) @twan163: Instead of:edit
and:edit!
you can use:e
and:e!
respectively.
– Thushi
Aug 8 '14 at 12:08
10
Lose modifications, yes and no. Crucially, you can stillu
ndo the:e!
.
– gerrit
Sep 10 '15 at 9:22
5
Also, if you want to reload all your buffers, run::bufdo e
– pkout
Nov 4 '16 at 21:43
|
show 3 more comments
You can use the :edit
command, without specifying a file name, to reload
the current file. If you have made modifications to the file, you can use:edit!
to force the reload of the current file (you will lose your
modifications).
The command :edit
can be abbreviated by :e
. The force-edit can thus be done by :e!
2
The "!" was tripping me, but now it's clear.
– twan163
Aug 8 '14 at 11:41
106
Note: that's typically abbreviated to:e!
.
– Stéphane Chazelas
Aug 8 '14 at 12:07
31
@StephaneChazelas: Yeah,That's correct :) @twan163: Instead of:edit
and:edit!
you can use:e
and:e!
respectively.
– Thushi
Aug 8 '14 at 12:08
10
Lose modifications, yes and no. Crucially, you can stillu
ndo the:e!
.
– gerrit
Sep 10 '15 at 9:22
5
Also, if you want to reload all your buffers, run::bufdo e
– pkout
Nov 4 '16 at 21:43
|
show 3 more comments
You can use the :edit
command, without specifying a file name, to reload
the current file. If you have made modifications to the file, you can use:edit!
to force the reload of the current file (you will lose your
modifications).
The command :edit
can be abbreviated by :e
. The force-edit can thus be done by :e!
You can use the :edit
command, without specifying a file name, to reload
the current file. If you have made modifications to the file, you can use:edit!
to force the reload of the current file (you will lose your
modifications).
The command :edit
can be abbreviated by :e
. The force-edit can thus be done by :e!
edited Feb 28 at 15:45
Karlo
9412820
9412820
answered Aug 8 '14 at 11:31
ThushiThushi
6,53931439
6,53931439
2
The "!" was tripping me, but now it's clear.
– twan163
Aug 8 '14 at 11:41
106
Note: that's typically abbreviated to:e!
.
– Stéphane Chazelas
Aug 8 '14 at 12:07
31
@StephaneChazelas: Yeah,That's correct :) @twan163: Instead of:edit
and:edit!
you can use:e
and:e!
respectively.
– Thushi
Aug 8 '14 at 12:08
10
Lose modifications, yes and no. Crucially, you can stillu
ndo the:e!
.
– gerrit
Sep 10 '15 at 9:22
5
Also, if you want to reload all your buffers, run::bufdo e
– pkout
Nov 4 '16 at 21:43
|
show 3 more comments
2
The "!" was tripping me, but now it's clear.
– twan163
Aug 8 '14 at 11:41
106
Note: that's typically abbreviated to:e!
.
– Stéphane Chazelas
Aug 8 '14 at 12:07
31
@StephaneChazelas: Yeah,That's correct :) @twan163: Instead of:edit
and:edit!
you can use:e
and:e!
respectively.
– Thushi
Aug 8 '14 at 12:08
10
Lose modifications, yes and no. Crucially, you can stillu
ndo the:e!
.
– gerrit
Sep 10 '15 at 9:22
5
Also, if you want to reload all your buffers, run::bufdo e
– pkout
Nov 4 '16 at 21:43
2
2
The "!" was tripping me, but now it's clear.
– twan163
Aug 8 '14 at 11:41
The "!" was tripping me, but now it's clear.
– twan163
Aug 8 '14 at 11:41
106
106
Note: that's typically abbreviated to
:e!
.– Stéphane Chazelas
Aug 8 '14 at 12:07
Note: that's typically abbreviated to
:e!
.– Stéphane Chazelas
Aug 8 '14 at 12:07
31
31
@StephaneChazelas: Yeah,That's correct :) @twan163: Instead of
:edit
and :edit!
you can use :e
and :e!
respectively.– Thushi
Aug 8 '14 at 12:08
@StephaneChazelas: Yeah,That's correct :) @twan163: Instead of
:edit
and :edit!
you can use :e
and :e!
respectively.– Thushi
Aug 8 '14 at 12:08
10
10
Lose modifications, yes and no. Crucially, you can still
u
ndo the :e!
.– gerrit
Sep 10 '15 at 9:22
Lose modifications, yes and no. Crucially, you can still
u
ndo the :e!
.– gerrit
Sep 10 '15 at 9:22
5
5
Also, if you want to reload all your buffers, run:
:bufdo e
– pkout
Nov 4 '16 at 21:43
Also, if you want to reload all your buffers, run:
:bufdo e
– pkout
Nov 4 '16 at 21:43
|
show 3 more comments
In addition to manually refreshing the file with :edit
, you can put into your ~/.vimrc
:set autoread
to make Vim automatically refresh any files that haven't been edited by Vim. Also see :checktime
.
2
Buyer beware -- If you're working on fresh code andgit pull
be aware you could lose your unsaved changes on screen rather unintentionally.
– azatar
Jun 16 '16 at 17:36
4
@toszter No, Vim will only refresh unchanged buffers. In case of changes, there will still be a query: Keep, or load?
– Ingo Karkat
Jun 17 '16 at 7:28
7
N.B. autoread doesn't exactly work automatically. You either have to usegvim
, or run external commands.
– Sparhawk
Sep 30 '16 at 1:37
3
Those using vim inside of tmux can get focus events by using github.com/tmux-plugins/vim-tmux-focus-events . Otherwise autoread won't help in the terminal unless you somehow call:checktime
– Karl Bartel
Jan 3 '17 at 15:50
3
autoread
can be auto-triggered X seconds after the cursor stops moving, see this answer.
– Tom Hale
Aug 1 '17 at 6:16
|
show 3 more comments
In addition to manually refreshing the file with :edit
, you can put into your ~/.vimrc
:set autoread
to make Vim automatically refresh any files that haven't been edited by Vim. Also see :checktime
.
2
Buyer beware -- If you're working on fresh code andgit pull
be aware you could lose your unsaved changes on screen rather unintentionally.
– azatar
Jun 16 '16 at 17:36
4
@toszter No, Vim will only refresh unchanged buffers. In case of changes, there will still be a query: Keep, or load?
– Ingo Karkat
Jun 17 '16 at 7:28
7
N.B. autoread doesn't exactly work automatically. You either have to usegvim
, or run external commands.
– Sparhawk
Sep 30 '16 at 1:37
3
Those using vim inside of tmux can get focus events by using github.com/tmux-plugins/vim-tmux-focus-events . Otherwise autoread won't help in the terminal unless you somehow call:checktime
– Karl Bartel
Jan 3 '17 at 15:50
3
autoread
can be auto-triggered X seconds after the cursor stops moving, see this answer.
– Tom Hale
Aug 1 '17 at 6:16
|
show 3 more comments
In addition to manually refreshing the file with :edit
, you can put into your ~/.vimrc
:set autoread
to make Vim automatically refresh any files that haven't been edited by Vim. Also see :checktime
.
In addition to manually refreshing the file with :edit
, you can put into your ~/.vimrc
:set autoread
to make Vim automatically refresh any files that haven't been edited by Vim. Also see :checktime
.
edited Sep 30 '16 at 1:30
Sparhawk
10.2k744101
10.2k744101
answered Aug 8 '14 at 11:51
Ingo KarkatIngo Karkat
8,88712033
8,88712033
2
Buyer beware -- If you're working on fresh code andgit pull
be aware you could lose your unsaved changes on screen rather unintentionally.
– azatar
Jun 16 '16 at 17:36
4
@toszter No, Vim will only refresh unchanged buffers. In case of changes, there will still be a query: Keep, or load?
– Ingo Karkat
Jun 17 '16 at 7:28
7
N.B. autoread doesn't exactly work automatically. You either have to usegvim
, or run external commands.
– Sparhawk
Sep 30 '16 at 1:37
3
Those using vim inside of tmux can get focus events by using github.com/tmux-plugins/vim-tmux-focus-events . Otherwise autoread won't help in the terminal unless you somehow call:checktime
– Karl Bartel
Jan 3 '17 at 15:50
3
autoread
can be auto-triggered X seconds after the cursor stops moving, see this answer.
– Tom Hale
Aug 1 '17 at 6:16
|
show 3 more comments
2
Buyer beware -- If you're working on fresh code andgit pull
be aware you could lose your unsaved changes on screen rather unintentionally.
– azatar
Jun 16 '16 at 17:36
4
@toszter No, Vim will only refresh unchanged buffers. In case of changes, there will still be a query: Keep, or load?
– Ingo Karkat
Jun 17 '16 at 7:28
7
N.B. autoread doesn't exactly work automatically. You either have to usegvim
, or run external commands.
– Sparhawk
Sep 30 '16 at 1:37
3
Those using vim inside of tmux can get focus events by using github.com/tmux-plugins/vim-tmux-focus-events . Otherwise autoread won't help in the terminal unless you somehow call:checktime
– Karl Bartel
Jan 3 '17 at 15:50
3
autoread
can be auto-triggered X seconds after the cursor stops moving, see this answer.
– Tom Hale
Aug 1 '17 at 6:16
2
2
Buyer beware -- If you're working on fresh code and
git pull
be aware you could lose your unsaved changes on screen rather unintentionally.– azatar
Jun 16 '16 at 17:36
Buyer beware -- If you're working on fresh code and
git pull
be aware you could lose your unsaved changes on screen rather unintentionally.– azatar
Jun 16 '16 at 17:36
4
4
@toszter No, Vim will only refresh unchanged buffers. In case of changes, there will still be a query: Keep, or load?
– Ingo Karkat
Jun 17 '16 at 7:28
@toszter No, Vim will only refresh unchanged buffers. In case of changes, there will still be a query: Keep, or load?
– Ingo Karkat
Jun 17 '16 at 7:28
7
7
N.B. autoread doesn't exactly work automatically. You either have to use
gvim
, or run external commands.– Sparhawk
Sep 30 '16 at 1:37
N.B. autoread doesn't exactly work automatically. You either have to use
gvim
, or run external commands.– Sparhawk
Sep 30 '16 at 1:37
3
3
Those using vim inside of tmux can get focus events by using github.com/tmux-plugins/vim-tmux-focus-events . Otherwise autoread won't help in the terminal unless you somehow call
:checktime
– Karl Bartel
Jan 3 '17 at 15:50
Those using vim inside of tmux can get focus events by using github.com/tmux-plugins/vim-tmux-focus-events . Otherwise autoread won't help in the terminal unless you somehow call
:checktime
– Karl Bartel
Jan 3 '17 at 15:50
3
3
autoread
can be auto-triggered X seconds after the cursor stops moving, see this answer.– Tom Hale
Aug 1 '17 at 6:16
autoread
can be auto-triggered X seconds after the cursor stops moving, see this answer.– Tom Hale
Aug 1 '17 at 6:16
|
show 3 more comments
TL;DR
Skip to the Wrap-up
heading for the vimrc
lines to add to do make your life better.
Manually
Run :checktime
Check if any buffers were changed outside of Vim.
This checks and warns you if you would end up with two
versions of a file.
Automatically
To do automatically load changes, add in your vimrc
:
set autoread
When a file has been detected to have been changed outside of Vim and
it has not been changed inside of Vim, automatically read it again.
When the file has been deleted this is not done.
This answer adds a caveat:
Autoread does not reload file unless you do something like run external command (like
!ls
or!sh
etc)
Read on for solutions.
Trigger when cursor stops moving
Add to your vimrc
:
au CursorHold,CursorHoldI * checktime
By default, CursorHold is triggered after the cursor remains still for 4 seconds, and is configurable via updatetime.
Trigger on buffer change or terminal focus
Add the following to your vimrc
to trigger autoread
when changing buffers while inside vim:
au FocusGained,BufEnter * :checktime
Catching terminal window focus inside plain vim
To have FocusGained
(see above) work in plain vim, inside a terminal emulator (Xterm, tmux, etc) install the plugin:
vim-tmux-focus-events
On tmux versions > 1.9, you'll need to add in .tmux.conf
:
set -g focus-events on
Wrap-up
Notifications when autoread
triggers are also possible.
Here are my vimrc
lines to implement all the above:
" Triger `autoread` when files changes on disk
" https://unix.stackexchange.com/questions/149209/refresh-changed-content-of-file-opened-in-vim/383044#383044
" https://vi.stackexchange.com/questions/13692/prevent-focusgained-autocmd-running-in-command-line-editing-mode
autocmd FocusGained,BufEnter,CursorHold,CursorHoldI * if mode() != 'c' | checktime | endif
" Notification after file change
" https://vi.stackexchange.com/questions/13091/autocmd-event-for-autoread
autocmd FileChangedShellPost *
echohl WarningMsg | echo "File changed on disk. Buffer reloaded." | echohl None
Thanks to ErichBSchulz for pointing me in the right direction with au CursorHold
.
excellent post! Feels like one of those things where this should be the default behaviour
– JonnyRaa
Nov 13 '17 at 10:30
add a comment |
TL;DR
Skip to the Wrap-up
heading for the vimrc
lines to add to do make your life better.
Manually
Run :checktime
Check if any buffers were changed outside of Vim.
This checks and warns you if you would end up with two
versions of a file.
Automatically
To do automatically load changes, add in your vimrc
:
set autoread
When a file has been detected to have been changed outside of Vim and
it has not been changed inside of Vim, automatically read it again.
When the file has been deleted this is not done.
This answer adds a caveat:
Autoread does not reload file unless you do something like run external command (like
!ls
or!sh
etc)
Read on for solutions.
Trigger when cursor stops moving
Add to your vimrc
:
au CursorHold,CursorHoldI * checktime
By default, CursorHold is triggered after the cursor remains still for 4 seconds, and is configurable via updatetime.
Trigger on buffer change or terminal focus
Add the following to your vimrc
to trigger autoread
when changing buffers while inside vim:
au FocusGained,BufEnter * :checktime
Catching terminal window focus inside plain vim
To have FocusGained
(see above) work in plain vim, inside a terminal emulator (Xterm, tmux, etc) install the plugin:
vim-tmux-focus-events
On tmux versions > 1.9, you'll need to add in .tmux.conf
:
set -g focus-events on
Wrap-up
Notifications when autoread
triggers are also possible.
Here are my vimrc
lines to implement all the above:
" Triger `autoread` when files changes on disk
" https://unix.stackexchange.com/questions/149209/refresh-changed-content-of-file-opened-in-vim/383044#383044
" https://vi.stackexchange.com/questions/13692/prevent-focusgained-autocmd-running-in-command-line-editing-mode
autocmd FocusGained,BufEnter,CursorHold,CursorHoldI * if mode() != 'c' | checktime | endif
" Notification after file change
" https://vi.stackexchange.com/questions/13091/autocmd-event-for-autoread
autocmd FileChangedShellPost *
echohl WarningMsg | echo "File changed on disk. Buffer reloaded." | echohl None
Thanks to ErichBSchulz for pointing me in the right direction with au CursorHold
.
excellent post! Feels like one of those things where this should be the default behaviour
– JonnyRaa
Nov 13 '17 at 10:30
add a comment |
TL;DR
Skip to the Wrap-up
heading for the vimrc
lines to add to do make your life better.
Manually
Run :checktime
Check if any buffers were changed outside of Vim.
This checks and warns you if you would end up with two
versions of a file.
Automatically
To do automatically load changes, add in your vimrc
:
set autoread
When a file has been detected to have been changed outside of Vim and
it has not been changed inside of Vim, automatically read it again.
When the file has been deleted this is not done.
This answer adds a caveat:
Autoread does not reload file unless you do something like run external command (like
!ls
or!sh
etc)
Read on for solutions.
Trigger when cursor stops moving
Add to your vimrc
:
au CursorHold,CursorHoldI * checktime
By default, CursorHold is triggered after the cursor remains still for 4 seconds, and is configurable via updatetime.
Trigger on buffer change or terminal focus
Add the following to your vimrc
to trigger autoread
when changing buffers while inside vim:
au FocusGained,BufEnter * :checktime
Catching terminal window focus inside plain vim
To have FocusGained
(see above) work in plain vim, inside a terminal emulator (Xterm, tmux, etc) install the plugin:
vim-tmux-focus-events
On tmux versions > 1.9, you'll need to add in .tmux.conf
:
set -g focus-events on
Wrap-up
Notifications when autoread
triggers are also possible.
Here are my vimrc
lines to implement all the above:
" Triger `autoread` when files changes on disk
" https://unix.stackexchange.com/questions/149209/refresh-changed-content-of-file-opened-in-vim/383044#383044
" https://vi.stackexchange.com/questions/13692/prevent-focusgained-autocmd-running-in-command-line-editing-mode
autocmd FocusGained,BufEnter,CursorHold,CursorHoldI * if mode() != 'c' | checktime | endif
" Notification after file change
" https://vi.stackexchange.com/questions/13091/autocmd-event-for-autoread
autocmd FileChangedShellPost *
echohl WarningMsg | echo "File changed on disk. Buffer reloaded." | echohl None
Thanks to ErichBSchulz for pointing me in the right direction with au CursorHold
.
TL;DR
Skip to the Wrap-up
heading for the vimrc
lines to add to do make your life better.
Manually
Run :checktime
Check if any buffers were changed outside of Vim.
This checks and warns you if you would end up with two
versions of a file.
Automatically
To do automatically load changes, add in your vimrc
:
set autoread
When a file has been detected to have been changed outside of Vim and
it has not been changed inside of Vim, automatically read it again.
When the file has been deleted this is not done.
This answer adds a caveat:
Autoread does not reload file unless you do something like run external command (like
!ls
or!sh
etc)
Read on for solutions.
Trigger when cursor stops moving
Add to your vimrc
:
au CursorHold,CursorHoldI * checktime
By default, CursorHold is triggered after the cursor remains still for 4 seconds, and is configurable via updatetime.
Trigger on buffer change or terminal focus
Add the following to your vimrc
to trigger autoread
when changing buffers while inside vim:
au FocusGained,BufEnter * :checktime
Catching terminal window focus inside plain vim
To have FocusGained
(see above) work in plain vim, inside a terminal emulator (Xterm, tmux, etc) install the plugin:
vim-tmux-focus-events
On tmux versions > 1.9, you'll need to add in .tmux.conf
:
set -g focus-events on
Wrap-up
Notifications when autoread
triggers are also possible.
Here are my vimrc
lines to implement all the above:
" Triger `autoread` when files changes on disk
" https://unix.stackexchange.com/questions/149209/refresh-changed-content-of-file-opened-in-vim/383044#383044
" https://vi.stackexchange.com/questions/13692/prevent-focusgained-autocmd-running-in-command-line-editing-mode
autocmd FocusGained,BufEnter,CursorHold,CursorHoldI * if mode() != 'c' | checktime | endif
" Notification after file change
" https://vi.stackexchange.com/questions/13091/autocmd-event-for-autoread
autocmd FileChangedShellPost *
echohl WarningMsg | echo "File changed on disk. Buffer reloaded." | echohl None
Thanks to ErichBSchulz for pointing me in the right direction with au CursorHold
.
edited Sep 26 '17 at 10:14
answered Aug 1 '17 at 3:42
Tom HaleTom Hale
7,420342101
7,420342101
excellent post! Feels like one of those things where this should be the default behaviour
– JonnyRaa
Nov 13 '17 at 10:30
add a comment |
excellent post! Feels like one of those things where this should be the default behaviour
– JonnyRaa
Nov 13 '17 at 10:30
excellent post! Feels like one of those things where this should be the default behaviour
– JonnyRaa
Nov 13 '17 at 10:30
excellent post! Feels like one of those things where this should be the default behaviour
– JonnyRaa
Nov 13 '17 at 10:30
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f149209%2frefresh-changed-content-of-file-opened-in-vim%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Related: How can I reload all buffers at once? at Vim SE
– kenorb
Apr 18 '15 at 21:06