linux中vi编辑器出错怎么改,为什么用vi编辑器 修改文件 会变成这样

gogo11 于 2011-07-20 23:44:06发表:

修改/etc/vimrc ,给set backup前边加一个"

附上我的设置:[code]if v:lang =~ "utf8$" || v:lang =~ "UTF-8$"

set fileencodings=utf-8,latin1

endif

set nocompatible " Use Vim defaults (much better!)

set bs=indent,eol,start " allow backspacing over everything in insert mode

"set ai " always set autoindenting on

"set backup " keep a backup file

set viminfo='20,\"50 " read/write a .viminfo file, don't store more

" than 50 lines of registers

set history=50 " keep 50 lines of command line history

set ruler " show the cursor position all the time

" Only do this part when compiled with support for autocommands

if has("autocmd")

augroup redhat

" In text files, always limit the width of text to 78 characters

autocmd BufRead *.txt set tw=78

" When editing a file, always jump to the last cursor position

autocmd BufReadPost *

\ if line("'\"") > 0 && line ("'\"") <= line("$") |

\ exe "normal! g'\"" |

\ endif

augroup END

endif

if has("cscope") && filereadable("/usr/bin/cscope")

set csprg=/usr/bin/cscope

set csto=0

set cst

set nocsverb

" add any database in current directory

if filereadable("cscope.out")

cs add cscope.out

" else add database pointed to by environment

elseif $CSCOPE_DB != ""

cs add $CSCOPE_DB

endif

set csverb

endif

" Switch syntax highlighting on, when the terminal has colors

" Also switch on highlighting the last used search pattern.

if &t_Co > 2 || has("gui_running")

syntax on

set hlsearch

endif

if &term=="xterm"

set t_Co=8

set t_Sb=[4%dm

set t_Sf=[3%dm

endif

[/code]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
是的,因为 vi 编辑器在保存文件时并非直接修改文件,而是先将修改后的内容写入一个临时文件,然后再将临时文件重命名为原文件名,这样触发 inotify 的 IN_MOVED_FROM 和 IN_MOVED_TO 事件,而不是 IN_MODIFY 事件,因此需要在代码增加对 IN_MOVED_FROM 和 IN_MOVED_TO 事件的处理。 以下是修改后的 C 代码示例,可以监控 vi 编辑器修改文件: ```c #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/inotify.h> #define EVENT_SIZE (sizeof(struct inotify_event)) #define EVENT_BUF_LEN (1024 * (EVENT_SIZE + 16)) int main() { int fd, wd, length, i = 0; char buffer[EVENT_BUF_LEN]; // 监控的文件路径 char *file_path = "/path/to/file"; // 初始化 inotify 实例 fd = inotify_init(); if (fd < 0) { perror("inotify_init"); exit(EXIT_FAILURE); } // 添加要监控的文件路径并指定监控事件类型 wd = inotify_add_watch(fd, file_path, IN_MODIFY | IN_MOVED_FROM | IN_MOVED_TO); if (wd < 0) { perror("inotify_add_watch"); exit(EXIT_FAILURE); } // 监控循环 while (1) { i = 0; length = read(fd, buffer, EVENT_BUF_LEN); if (length < 0) { perror("read"); exit(EXIT_FAILURE); } while (i < length) { struct inotify_event *event = (struct inotify_event *) &buffer[i]; if (event->mask & IN_MODIFY) { printf("File %s has been modified.\n", event->name); } else if (event->mask & IN_MOVED_FROM) { printf("File %s has been moved from.\n", event->name); } else if (event->mask & IN_MOVED_TO) { printf("File %s has been moved to.\n", event->name); } i += EVENT_SIZE + event->len; } } // 关闭 inotify 实例 inotify_rm_watch(fd, wd); close(fd); return 0; } ``` 在修改后的代码,将 inotify_add_watch() 的参数修改为 IN_MODIFY | IN_MOVED_FROM | IN_MOVED_TO,增加了对 IN_MOVED_FROM 和 IN_MOVED_TO 事件的处理。如果事件类型为 IN_MOVED_FROM,则输出文件已被移动的消息;如果事件类型为 IN_MOVED_TO,则输出文件已被移动到的消息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值