??xml version="1.0" encoding="utf-8" standalone="yes"?>日韩porn,亚洲先锋成人,高清在线观看avhttp://www.aygfsteel.com/lukewange-hit1983/category/35727.htmlpresentzh-cnWed, 12 Nov 2008 19:17:58 GMTWed, 12 Nov 2008 19:17:58 GMT60linux讑֤模型http://www.aygfsteel.com/lukewange-hit1983/archive/2008/11/12/240185.htmlLukeWLukeWWed, 12 Nov 2008 15:14:00 GMThttp://www.aygfsteel.com/lukewange-hit1983/archive/2008/11/12/240185.htmlhttp://www.aygfsteel.com/lukewange-hit1983/comments/240185.htmlhttp://www.aygfsteel.com/lukewange-hit1983/archive/2008/11/12/240185.html#Feedback0http://www.aygfsteel.com/lukewange-hit1983/comments/commentRss/240185.htmlhttp://www.aygfsteel.com/lukewange-hit1983/services/trackbacks/240185.html Q. Sysfs文gpȝ
Sysfs文gpȝ是一个类gproc文gpȝ的特D文件系l,用于系l中的设备组l成层次l构Qƈ向用h式程序提供详l的内核数据l构信息。其层目录主要有:
Block目录Q包含所有的块设?br /> Devices目录Q包含系l所有的讑֤QƈҎ讑֤挂接的ȝcdl织成层ơ结?br /> Bus目录Q包含系l中所有的ȝcd
Drivers目录Q包括内怸所有已注册的设备驱动程?br /> Class目录Q系l中的设备类型(如网卡设备,声卡讑֤{)
Q. 内核对象机制关键数据l构
2.1 kobject内核对象
Kobject 是Linux 2.6引入的新的设备管理机Ӟ在内怸由struct kobject表示。通过q个数据l构使所有设备在底层都具有统一的接口,kobject提供基本的对象管理,是构成Linux 2.6讑֤模型的核心结构,它与sysfs文gpȝ紧密兌Q每个在内核中注册的kobject对象都对应于sysfs文gpȝ中的一个目录?br /> Kobjectl构定义为:
struct kobject {
char * k_name;    // 指向讑֤名称的指?/span>
char name[KOBJ_NAME_LEN];   // 讑֤名称
struct kref kref;    // 对象引用计数
struct list_head entry;   // 挂接到所在kset中去的单?/span>
struct kobject * parent; // 指向父对象的指针
struct kset * kset;    // 所属kset的指?/span>
struct kobj_type * ktype;   // 指向其对象类型描q符的指?/span>
struct dentry * dentry; // sysfs文gpȝ中与该对象对应的文g节点路径指针
};

其中的kref域表C对象引用的计敎ͼ内核通过kref实现对象引用计数理Q内核提供两个函数kobject_get()、kobject_put()分别用于增加和减引用计敎ͼ当引用计Cؓ0Ӟ所有该对象使用的资源将被释放?br /> Ktype 域是一个指向kobj_typel构的指针,表示该对象的cd。Kobj_type数据l构包含三个域:一个releaseҎ用于释放kobject? 用的资源Q一个sysfs_ops指针指向sysfs操作表和一个sysfs文gpȝ~省属性列表。Sysfs操作表包括两个函数store()? show()。当用户态读取属性时Qshow()函数被调用,该函数编码指定属性值存入buffer中返回给用户态;而store()函数用于存储用户? 传入的属性倹{?br /> 2.2 kset内核对象集合
Kobject通常通过ksetl织成层ơ化的结构,kset是具有相同类型的kobject的集合,在内怸用kset数据l构表示Q定义ؓQ?br />
struct kset {
struct subsystem * subsys;   // 所在的subsystem的指?/span>
struct kobj_type * ktype;   // 指向该kset对象cd描述W的指针
struct list_head list;      // 用于q接该kset中所有kobject的链表头
struct kobject kobj;    // 嵌入的kobject
struct kset_hotplug_ops * hotplug_ops; // 指向热插拔操作表的指?/span>
};

?含在kset中的所有kobject被组l成一个双向@环链表,list域正是该链表的头。Ktype域指向一个kobj_typel构Q被? kset中的所有kobject׃nQ表C些对象的cd。Kset数据l构q内嵌了一个kobject对象Q由kobj域表C)Q所有属于这个kset 的kobject对象的parent域均指向q个内嵌的对象。此外,ksetq依赖于kobjl护引用计数Qkset的引用计数实际上是内嵌? kobject对象的引用计数?br /> 2.3 subsystem内核对象子系l?br /> Subsystem是一pdkset的集合,描述pȝ中某一 c设备子pȝQ如block_subsys表示所有的块设备,对应于sysfs文gpȝ中的block目录。类似的Qdevices_subsys对应? sysfs中的devices目录Q描q系l中所有的讑֤。Subsystem由struct subsystem数据l构描述Q定义ؓQ?br />
struct subsystem {
struct kset kset;       // 内嵌的kset对象
struct rw_semaphore rwsem; // 互斥讉K信号?/span>
};

?个kset必须属于某个subsystemQ通过讄ksetl构中的subsys域指向指定的subsystem可以一个kset加入到该 subsystem。所有挂接到同一subsystem的kset׃n同一个rwsem信号量,用于同步讉Kkset中的链表?br />
Q. 内核对象机制主要相关函数
针对内核对象不同层次的数据结构,linux 2.6内核定义了一pd操作函数Q定义于lib/kobject.c文g中?br /> 3.1 kobject相关函数
void kobject_init(struct kobject * kobj)Q?/span>// kobject初始化函数。设|kobject引用计数?Qentry域指向自w,其所属kset引用计数加1

int kobject_set_name(struct kobject *kobj, const char *format, )Q?/span>// 讄指定kobject的名U?/span>

void kobject_cleanup(struct kobject * kobj);
void kobject_release(struct kref *kref)Q?/span>// kobject清除函数。当其引用计CؓQ时Q释攑֯象占用的资源?/span>

struct kobject *kobject_get(struct kobject *kobj)Q?/span>// kobj 对象的引用计数加1Q同时返回该对象的指针?/span>

void kobject_put(struct kobject * kobj)Q?/span>// kobj对象的引用计数减1Q如果引用计数降?Q则调用kobject_release()释放该kobject对象?/span>

int kobject_add(struct kobject * kobj)Q?/span>// kobj对象加入Linux讑֤层次。挂接该kobject对象到kset的list链中Q增加父目录各kobject的引用计敎ͼ在其parent指向的目录下创徏文g节点Qƈ启动该类型内核对象的hotplug函数?/span>

int kobject_register(struct kobject * kobj)Q?/span>// kobject注册函数。通过调用kobject_init()初始化kobjQ再调用kobject_add()完成该内核对象的注册?/span>

void kobject_del(struct kobject * kobj)Q?/span>// 从Linux讑֤层次(hierarchy)中删除kobj对象?/span>

void kobject_unregister(struct kobject * kobj)Q?/span>// kobject注销函数。与kobject_register()相反Q它首先调用kobject_del从设备层ơ中删除该对象,再调用kobject_put()减少该对象的引用计数Q如果引用计数降?Q则释放该kobject对象?/span>

3.2 kset相关函数
与kobject 怼Qkset_init()完成指定kset的初始化Qkset_get()和kset_put()分别增加和减kset对象的引用计数? Kset_add()和kset_del()函数分别实现指定keset对象加入讑֤层次和从其中删除Qkset_register()函数完成 kset的注册而kset_unregister()函数则完成kset的注销?br /> 3.3 subsystem相关函数
subsystem有一l完成类似的函数Q分别是Q?br />
void subsystem_init(struct subsystem *subsys);
int subsystem_register(struct subsystem *subsys);
void subsystem_unregister(struct subsystem *subsys);
struct subsystem *subsys_get(struct subsystem *subsys)
void subsys_put(struct subsystem *subsys);

Q. 讑֤模型lg
在上q内核对象机制的基础上,Linux的设备模型徏立在几个关键lg的基上,下面我们详细阐述q些lg?br /> 4.1 devices
pȝ中的M讑֤在设备模型中都由一个device对象描述Q其对应的数据结构struct device定义为:
struct device {
struct list_head g_list;
struct list_head node;
    
struct list_head bus_list;
    
struct list_head driver_list;
    
struct list_head children;
    
struct device *parent;
    
struct kobject kobj;
    
char bus_id[BUS_ID_SIZE];
    
struct bus_type *bus;
    
struct device_driver *driver;
    
void *driver_data;
    
/* Several fields omitted */
};

g_list 该device对象挂接到全局讑֤链表中,所有的device对象都包含在devices_subsys中,q组l成层次l构。Node域将该对象挂? 到其兄弟对象的链表中Q而bus_list则用于将q接到相同ȝ上的讑֤l织成链表,driver_list则将同一驱动E序理的所有设备组lؓ? 表。此外,children域指向该device对象子对象链表头Qparent域则指向父对象。Device对象q内嵌一个kobject对象Q用于引 用计数管理ƈ通过它实现设备层ơ结构。Driver域指向管理该讑֤的驱动程序对象,而driver_data则是提供l驱动程序的数据。Bus域描q设 备所q接的ȝcd?br /> 内核提供了相应的函数用于操作device对象。其中Device_register()函数一个新的device对象? 入设备模型,q自动在/sys/devices下创Z个对应的目录。Device_unregister()完成相反的操作,注销讑֤对象? Get_device()和put_device()分别增加与减设备对象的引用计数。通常devicel构不单独用,而是包含在更大的l构中作Z 个子l构使用Q比如描qPCI讑֤的struct pci_devQ其中的dev域就是一个device对象?br /> 4.2 drivers
pȝ中的每个驱动E序׃个device_driver对象描述Q对应的数据l构定义为:
struct device_driver {
    
char *name;   // 讑֤驱动E序的名U?/span>
    struct bus_type *bus; // 该驱动所理的设备挂接的ȝcd
    struct kobject kobj;    // 内嵌kobject对象
    struct list_head devices;  // 该驱动所理的设备链表头
    int (*probe)(struct device *dev); // 指向讑֤探测函数Q用于探设备是否可以被该驱动程序管?/span>
int (*remove)(struct device *dev); // 用于删除讑֤的函?/span>
/*
 some fields omitted*/
}Q?/span>

与device l构cMQdevice_driver对象依靠内嵌的kobject对象实现引用计数理和层ơ结构组l。内核提供类似的函数用于操作 device_driver对象Q如get_driver()增加引用计数Qdriver_register()用于向设备模型插入新的driver? 象,同时在sysfs文gpȝ中创建对应的目录。Device_driver()l构q包括几个函敎ͼ用于处理热拔插、即插即用和甉|理事g?br /> 4.3   buses
pȝ中ȝ由struct bus_type描述Q定义ؓQ?br />
struct bus_type {
char   * name; // ȝcd的名U?/span>
struct subsystem subsys; // 与该ȝ相关的subsystem
struct kset drivers; // 所有与该ȝ相关的驱动程序集?/span>
struct kset devices; // 所有挂接在该ȝ上的讑֤集合
struct bus_attribute * bus_attrs; // ȝ属?/span>
struct device_attribute * dev_attrs; // 讑֤属?/span>
struct driver_attribute * drv_attrs;   // 驱动E序属?/span>
int (*match)(struct device * dev, struct device_driver * drv);
int (*hotplug) (struct device *dev, char **envp, int num_envp, char *buffer, int buffer_size);
int (*suspend)(struct device * dev, u32 state);
int (*resume)(struct device * dev);
}Q?/span>

?个bus_type对象都内嵌一个subsystem对象Qbus_subsys对象理pȝ中所有ȝcd的subsystem对象。每? bus_type对象都对?sys/bus目录下的一个子目录Q如PCIȝcd对应?sys/bus/pci。在每个q样的目录下都存在两个子? 录:devices和driversQ分别对应于bus_typel构中的devices和drivers域)。其中devices子目录描q连接在该? U上的所有设备,而drivers目录则描qC该ȝ兌的所有驱动程序。与device_driver对象cMQbus_typel构q包含几个函? Qmatch()、hotplug(){)处理相应的热插拔、即插即拔和甉|理事g?br /> 4.4 classes
pȝ中的讑֤cȝ struct class描述Q表C某一c设备。所有的class对象都属于class_subsys子系l,对应于sysfs文gpȝ中的/sys/class目录? 每个class对象包括一个class_device链表Q每个class_device对象表示一个逻辑讑֤Qƈ通过struct class_device中的dev域(一个指向struct device的指针)兌一个物理设备。这P一个逻辑讑֤L对应于一个物理设备,但是一个物理设备却可能对应于多个逻辑讑֤。此外,classl构? q包括用于处理热插拔、即插即拔和甉|理事g的函敎ͼq与device对象和driver对象怼?br />

LukeW 2008-11-12 23:14 发表评论
]]>
位运?/title><link>http://www.aygfsteel.com/lukewange-hit1983/archive/2008/11/12/240082.html</link><dc:creator>LukeW</dc:creator><author>LukeW</author><pubDate>Wed, 12 Nov 2008 05:53:00 GMT</pubDate><guid>http://www.aygfsteel.com/lukewange-hit1983/archive/2008/11/12/240082.html</guid><wfw:comment>http://www.aygfsteel.com/lukewange-hit1983/comments/240082.html</wfw:comment><comments>http://www.aygfsteel.com/lukewange-hit1983/archive/2008/11/12/240082.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/lukewange-hit1983/comments/commentRss/240082.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/lukewange-hit1983/services/trackbacks/240082.html</trackback:ping><description><![CDATA[<strong>C中的位运?/strong><br /> 能够q用CQ何整形的数据cd?包括char, int), 无论有没有short, long, unsignedq样的限定词.<br /> <br /> <br /> 位运的应用<br /> <div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> 交换指针变量x,y所指向的存储位|处存放的?br /> </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> 优势是不需要第三个位置来时存储另一个?br /> </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> 但是q个Ҏq没有明昄性能优势,只是一个智力上的消?/span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> inplace_swap(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">x, </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">y)<br /> {<br />  </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">x </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">x </span><span style="color: rgb(0, 0, 0);">^</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">y;<br />  </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">x </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">x </span><span style="color: rgb(0, 0, 0);">^</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">y;<br />  </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">x </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">x </span><span style="color: rgb(0, 0, 0);">^</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">y;<br /> }<br /> </span></div> <br /> <strong>位运常见用?</strong><br /> 实现掩码q算<br /> <br /> <br /> <br /> -----------------------------------<br /> <strong>Java中的位运?/strong><br /> <br /> <br /> <img src ="http://www.aygfsteel.com/lukewange-hit1983/aggbug/240082.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/lukewange-hit1983/" target="_blank">LukeW</a> 2008-11-12 13:53 <a href="http://www.aygfsteel.com/lukewange-hit1983/archive/2008/11/12/240082.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>大端端 -- 各系l及机器的信息表C?/title><link>http://www.aygfsteel.com/lukewange-hit1983/archive/2008/11/12/240053.html</link><dc:creator>LukeW</dc:creator><author>LukeW</author><pubDate>Wed, 12 Nov 2008 03:58:00 GMT</pubDate><guid>http://www.aygfsteel.com/lukewange-hit1983/archive/2008/11/12/240053.html</guid><wfw:comment>http://www.aygfsteel.com/lukewange-hit1983/comments/240053.html</wfw:comment><comments>http://www.aygfsteel.com/lukewange-hit1983/archive/2008/11/12/240053.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/lukewange-hit1983/comments/commentRss/240053.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/lukewange-hit1983/services/trackbacks/240053.html</trackback:ping><description><![CDATA[因ؓ现行的计机都是以八位一个字节ؓ存储单位,那么一?6位的整数,也就是C语言中的short,在内存中可能有两U存储顺序big- <br /> <br /> endian和litte-endian.考虑一个short整数0x3132(0x32是低?0x31是高?,把它赋值给一个short变量,那么它在内存中的存储?br /> <br /> 能有如下两种情况:<br /> 大端字节(Big-endian):<br /> <br /> short变量地址<br />        0x1000                  0x1001<br /> ___________________________________<br /> |                 |<br /> |         0x31    |       0x32<br /> |________________ | ________________<br /> 高位字节在低位字节的前面,也就是高位在内存地址低的一?可以q样C(大端->高位->在前->正常的逻辑序)<br />  <br /> 端字节(little-endian):<br /> <br /> short变量地址<br />        0x1000                  0x1001<br /> _____________________________________<br /> |                 |<br /> |         0x32    |       0x31<br /> |________________ | __________________<br /> 低位字节在高位字节的前面,也就是低位在内存地址低的一?可以q样C(端->低位->在前->与正帔R辑序相反)<br />  <br /> 可以做个实验<br /> 在windows上下如下E序<br /> <div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: rgb(0, 0, 0);">#include </span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">stdio.h</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"><br /> #include </span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">assert.h</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"><br />  <br /> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> main( </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> )<br /> {<br />         </span><span style="color: rgb(0, 0, 255);">short</span><span style="color: rgb(0, 0, 0);"> test;<br />         FILE</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);"> fp;<br />         <br />         test </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0x3132</span><span style="color: rgb(0, 0, 0);">;  </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">(31ASIIC码的’1’,32ASIIC码的’2’)</span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 0);">        </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> ((fp </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> fopen (</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">c:</span><span style="color: rgb(0, 0, 0);">""</span><span style="color: rgb(0, 0, 0);">test.txt</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">wb</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">)) </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> NULL)<br />               assert(</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">);<br />         fwrite(</span><span style="color: rgb(0, 0, 0);">&</span><span style="color: rgb(0, 0, 0);">test, </span><span style="color: rgb(0, 0, 255);">sizeof</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">short</span><span style="color: rgb(0, 0, 0);">), </span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">, fp);<br />         fclose(fp);<br /> }</span></div> <br />     然后在C盘下打开test.txt文g,可以看见内容?1,而test{于0x3132,可以明显的看出来x86的字节顺序是低位在前.如果我们<br /> 把这D同L代码攑ֈ(big-endian)的机器上执行,那么打出来的文g是12.q在本机中用是没有问题?但当你把q个文g从一<br /> 个big- endian机器复制C个little-endian机器上时出现问题了.<br /> <br />     如上qC?我们在big-endian的机器上创徏了这个test文g,把其复制到little-endian的机器上再用freadd一?short?br /> ?我们得到的就不再?x3132而是0x3231?q样d的数据就是错误的,所以在两个字节序不一L机器上传输数据时需要特?br /> 心字节序,理解了字节顺序在可以帮助我们写出UL行更高的代码.<br /> <br /> 正因为有字节序的差?所以在|络传输的时候定义了所有字节顺序相关的数据都用big-endian,BSD的代码中定义了四个宏来处<br /> ?<br /> <div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> ntohs(n)     </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">|络字节序C机字节顺?nbsp;n代表net, h代表host, s代表short</span><span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> htons(n)     </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">L字节序到网l字节顺?nbsp;n代表net, h代表host, s代表short</span><span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> ntohl(n)      </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">|络字节序C机字节顺?nbsp;n代表net, h代表host, s代表 long</span><span style="color: rgb(0, 0, 0);"><br /> </span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> htonl(n)      </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">L字节序到网l字节顺?nbsp;n代表net, h代表host, s代表 long</span></div> <br /> 举例说明下这其中一个宏的实?<br /> <div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> sw16(x) "</span><span style="color: rgb(0, 0, 0);"><br />     ((</span><span style="color: rgb(0, 0, 255);">short</span><span style="color: rgb(0, 0, 0);">)( </span><span style="color: rgb(0, 0, 0);">"<br /> </span><span style="color: rgb(0, 0, 0);">        (((</span><span style="color: rgb(0, 0, 255);">short</span><span style="color: rgb(0, 0, 0);">)(x) </span><span style="color: rgb(0, 0, 0);">&</span><span style="color: rgb(0, 0, 0);"> (</span><span style="color: rgb(0, 0, 255);">short</span><span style="color: rgb(0, 0, 0);">)</span><span style="color: rgb(0, 0, 0);">0x00ffU</span><span style="color: rgb(0, 0, 0);">) </span><span style="color: rgb(0, 0, 0);"><<</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">8</span><span style="color: rgb(0, 0, 0);">) </span><span style="color: rgb(0, 0, 0);">|</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">"<br /> </span><span style="color: rgb(0, 0, 0);">        (((</span><span style="color: rgb(0, 0, 255);">short</span><span style="color: rgb(0, 0, 0);">)(x) </span><span style="color: rgb(0, 0, 0);">&</span><span style="color: rgb(0, 0, 0);"> (</span><span style="color: rgb(0, 0, 255);">short</span><span style="color: rgb(0, 0, 0);">)</span><span style="color: rgb(0, 0, 0);">0xff00U</span><span style="color: rgb(0, 0, 0);">) </span><span style="color: rgb(0, 0, 0);">>></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">8</span><span style="color: rgb(0, 0, 0);">) ))</span></div> <br /> q里实现的是一个交换两个字节顺?其他几个宏类?<br /> <br /> 我们改写一下上面的E序<br /> <div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: rgb(0, 0, 0);">#include </span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">stdio.h</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"><br /> #include </span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">assert.h</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"><br /> <br /> </span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> sw16(x) "</span><span style="color: rgb(0, 0, 0);"><br />     ((</span><span style="color: rgb(0, 0, 255);">short</span><span style="color: rgb(0, 0, 0);">)( </span><span style="color: rgb(0, 0, 0);">"<br /> </span><span style="color: rgb(0, 0, 0);">        (((</span><span style="color: rgb(0, 0, 255);">short</span><span style="color: rgb(0, 0, 0);">)(x) </span><span style="color: rgb(0, 0, 0);">&</span><span style="color: rgb(0, 0, 0);"> (</span><span style="color: rgb(0, 0, 255);">short</span><span style="color: rgb(0, 0, 0);">)</span><span style="color: rgb(0, 0, 0);">0x00ffU</span><span style="color: rgb(0, 0, 0);">) </span><span style="color: rgb(0, 0, 0);"><<</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">8</span><span style="color: rgb(0, 0, 0);">) </span><span style="color: rgb(0, 0, 0);">|</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">"<br /> </span><span style="color: rgb(0, 0, 0);">        (((</span><span style="color: rgb(0, 0, 255);">short</span><span style="color: rgb(0, 0, 0);">)(x) </span><span style="color: rgb(0, 0, 0);">&</span><span style="color: rgb(0, 0, 0);"> (</span><span style="color: rgb(0, 0, 255);">short</span><span style="color: rgb(0, 0, 0);">)</span><span style="color: rgb(0, 0, 0);">0xff00U</span><span style="color: rgb(0, 0, 0);">) </span><span style="color: rgb(0, 0, 0);">>></span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">8</span><span style="color: rgb(0, 0, 0);">) ))<br /> <br /> </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> 因ؓx86下面是低位在?需要交换一下变成网l字节顺?/span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 255);">#define</span><span style="color: rgb(0, 0, 0);"> htons(x) sw16(x)</span><span style="color: rgb(0, 0, 0);"><br />  <br /> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> main( </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> )<br /> {<br />         </span><span style="color: rgb(0, 0, 255);">short</span><span style="color: rgb(0, 0, 0);"> test;<br />         FILE</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);"> fp;<br />         <br />         test </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> htons(</span><span style="color: rgb(0, 0, 0);">0x3132</span><span style="color: rgb(0, 0, 0);">); </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">(31ASIIC码的’1’,32ASIIC码的’2’)</span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 0);">        </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);"> ((fp </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> fopen (</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">c:</span><span style="color: rgb(0, 0, 0);">""</span><span style="color: rgb(0, 0, 0);">test.txt</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, </span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">wb</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">)) </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> NULL)<br />               assert(</span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">);<br />         fwrite(</span><span style="color: rgb(0, 0, 0);">&</span><span style="color: rgb(0, 0, 0);">test, </span><span style="color: rgb(0, 0, 255);">sizeof</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">short</span><span style="color: rgb(0, 0, 0);">), </span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">, fp);<br />         fclose(fp);<br /> }</span></div> <br />  <br />     如果在高字节在前的机器上,׃与网l字节顺序一?所以我们什么都不干可以了,只需要把#define htons(x) sw16(x)宏替<br /> <br /> 换ؓ #define htons(x) (x).<br />     一开始我在理解这个问题时,d想ؓ什么其他数据不用交换字节顺?比如说我们write一块buffer到文?最后终于想明白?<br /> <br /> 因ؓ都是unsigned charcd一个字节一个字节的写进?q个序是固定的,不存在字节顺序的问题.<br /> <p><strong><font color="#0000ff">【用函数判断pȝ是Big Endianq是Little Endian?/font></strong><br /> </p> <br /> <div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: rgb(0, 0, 255);">bool</span><span style="color: rgb(0, 0, 0);"> IsBig_Endian()<br /> </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">如果字节序ؓbig-endianQ返回true;<br /> </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">反之?nbsp;  little-endianQ返回false</span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 0);">{<br />     unsigned </span><span style="color: rgb(0, 0, 255);">short</span><span style="color: rgb(0, 0, 0);"> test </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0x1122</span><span style="color: rgb(0, 0, 0);">;<br />     </span><span style="color: rgb(0, 0, 255);">if</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">( (unsigned </span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">) </span><span style="color: rgb(0, 0, 0);">&</span><span style="color: rgb(0, 0, 0);">test ) </span><span style="color: rgb(0, 0, 0);">==</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0x11</span><span style="color: rgb(0, 0, 0);">)<br />        </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> TRUE;<br /> </span><span style="color: rgb(0, 0, 255);">else</span><span style="color: rgb(0, 0, 0);"><br />     </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> FALSE;<br /> <br /> }</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">IsBig_Endian()<br /> </span></div> <br /> <strong><font color="#0000ff">【打印程序对象的字节表示?/font></strong><font color="#0000ff"><br /> </font> <div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> 可在不同q_与硬件架构的机器中测试运行这D代?理解大端表示和小端表C的不同.<br /> </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> q段代码使用强制cd转换规避cdpȝ</span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 0);">#incluede </span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);">stdio.h</span><span style="color: rgb(0, 0, 0);">></span><span style="color: rgb(0, 0, 0);"><br /> <br /> </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> 假设每个字节都是非负整数</span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 0);">typedef unsigned </span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">byte_pointer;<br /> <br /> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> show_bytes(byte_pointer start, </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> len)<br /> {<br />  </span><span style="color: rgb(0, 0, 255);">for</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">; i </span><span style="color: rgb(0, 0, 0);"><</span><span style="color: rgb(0, 0, 0);"> len; i</span><span style="color: rgb(0, 0, 0);">++</span><span style="color: rgb(0, 0, 0);">)<br />   printf(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> %.2x</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">, start[i]);<br />  printf(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">\n</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br /> }<br /> <br /> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> show_int(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> x)<br /> {<br />  show_bytes((byte_pointer) </span><span style="color: rgb(0, 0, 0);">&</span><span style="color: rgb(0, 0, 0);">x, </span><span style="color: rgb(0, 0, 255);">sizeof</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);">));<br /> }<br /> <br /> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> show_float(</span><span style="color: rgb(0, 0, 255);">float</span><span style="color: rgb(0, 0, 0);"> x)<br /> {<br />  show_bytes((byte_pointer) </span><span style="color: rgb(0, 0, 0);">&</span><span style="color: rgb(0, 0, 0);">x, </span><span style="color: rgb(0, 0, 255);">sizeof</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">float</span><span style="color: rgb(0, 0, 0);">));<br /> }<br /> <br /> </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> 在用相同编?如ASCII~码)的系l中,字符串字节表C得到的l果一般是相同?所以文本数据比二进制数据具有更强的q_无关?/span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> show_string(</span><span style="color: rgb(0, 0, 255);">char</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">x)<br /> {<br />  show_bytes((byte_pointer) x, strlen(x));<br /> }<br /> <br /> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> show_pointer(</span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">x)<br /> {<br />  show_bytes((byte_pointer) </span><span style="color: rgb(0, 0, 0);">&</span><span style="color: rgb(0, 0, 0);">x, </span><span style="color: rgb(0, 0, 255);">sizeof</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">));<br /> }<br /> <br /> </span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);"> test_show_bytes(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> val)<br /> {<br />  </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> ival </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> val;<br />  </span><span style="color: rgb(0, 0, 255);">float</span><span style="color: rgb(0, 0, 0);"> fval </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> (</span><span style="color: rgb(0, 0, 255);">float</span><span style="color: rgb(0, 0, 0);">)ival;<br />  </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 0);">pval </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">&</span><span style="color: rgb(0, 0, 0);">ival;<br />  <br />  show_int(ival); </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> 各个机器因ؓ大端表示和小端表C的不同,从而只是字节顺序不?/span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 0);"> show_float(fval); </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> 各个机器因ؓ大端表示和小端表C的不同,从而只是字节顺序不?/span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 0);"> show_pointer(pval); </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);"> 指针值是与机器相关的(linux,sun使用4字节地址, 而alpha使用八字节地址)</span><span style="color: rgb(0, 128, 0);"><br /> </span><span style="color: rgb(0, 0, 0);">}</span></div> <br /> ---------------------------------------------<br /> 对于如数?2345在int型和float型时的编码表C?br /> <img src ="http://www.aygfsteel.com/lukewange-hit1983/aggbug/240053.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/lukewange-hit1983/" target="_blank">LukeW</a> 2008-11-12 11:58 <a href="http://www.aygfsteel.com/lukewange-hit1983/archive/2008/11/12/240053.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>VIM讄http://www.aygfsteel.com/lukewange-hit1983/archive/2008/11/03/238318.htmlLukeWLukeWMon, 03 Nov 2008 05:02:00 GMThttp://www.aygfsteel.com/lukewange-hit1983/archive/2008/11/03/238318.htmlhttp://www.aygfsteel.com/lukewange-hit1983/comments/238318.htmlhttp://www.aygfsteel.com/lukewange-hit1983/archive/2008/11/03/238318.html#Feedback0http://www.aygfsteel.com/lukewange-hit1983/comments/commentRss/238318.htmlhttp://www.aygfsteel.com/lukewange-hit1983/services/trackbacks/238318.html  
VimQVi Improved) 是一个类gvi 的文本编辑器Q在Vi的基上增加了很多新的Ҏ和功能。Vim以其强大的功能和可定制能?br />
Q成为Linux/Unix环境下开源的最重要的编辑器之一Q另一个是 EmacsQ,被众多开发者所喜爱。笔者此时所用的是最新的7.1版本

?br /> 与大部分其它~辑器不同,q入 Vim 后,~省状态下键入的字Wƈ不会插入到所~辑的文件之中。Vim 的模式(modeQ可以简单地

理解?#8220;状?#8221;Q概念非帔R要。需要知道,Vim 有以下几个模式:
1Q?nbsp;  正常QnormalQ模式,~省的编辑模式;下面如果不加Ҏ说明Q提到的命o都直接在正常模式下输入;M其它模式中都

可以通过键盘上的 Esc 键回到正常模式?br /> 2Q?nbsp;  命oQcommandQ模式,用于执行较长、较复杂的命令;在正常模式下输入“:”Q一般命令)?#8220;/”Q正向搜索)?#8220;?”

Q反向搜索)卛_q入该模式;命o模式下的命o要输入回车键QEnterQ才完成?br /> 3Q?nbsp;  插入QinsertQ模式,输入文本时用;在正常模式下键入“i”QinsertQ或“a”QappendQ即可进入插入模式(也有?br />
外一些命令,?#8220;c”Q也可以q入插入模式Q但q些命o有其它的作用Q?br /> 4Q?nbsp;  可视QvisualQ模式,用于选定文本块;可以在正常模式下输入“v”Q小写)来按字符选定Q输?#8220;V”Q大写)来按行?br />
定,或输?#8220;Ctrl-V”来按方块选定?br /> 5Q?nbsp;  选择QselectQ模式,与普通的 Windows ~辑器较为接q的选择文本块的方式Q在以可视模式和选择模式之一选定文本块之

后,可以使用“Ctrl-G”切换到另一模式——该模式很少?Linux 上用,本文中就不再介绍了?
------------------------------------------
首先 vim ~/.vimrc 打开~辑文g

[转]
   1、VI或VIM的配|文件的路径

    发现/usr/share/vim/vimrc?etc/vim/vimrc指向是同一个文Ӟ即vimrcQؓvi和vim的配|文Ӟ修改q个文g卛_。这个\径在不同的LINUX版本中可能会不同?/p>

    2、配|颜?/p>

    配轩VI和VIM的颜色显C,使它能够高亮度显CZ些特别的单词Q这对编写程序很有用。后来打开文g发现里面其实已经有一行了Q只是用引号注释掉了Q只需 syntax on 所在行前面的引号去掉即可。或者另外独立添加一行:syntax on 也行Q另外编?etc/profile 增加一行alias vi="vim"p了?/p>

    3、设|鼠?/p>

    使用VI~辑文本Ӟ如果想修文g中改d标较q的位置Q这时候想用鼠标定位,可默认情况下Q鼠标是不可用的。如果你想用鼠标,只需另v一行,写上Qset mouse=a 卛_

    4、设|自动羃q?/p>

    默认情况下,VI和VIM都没有羃q的Q每换一行,光标均定位在格Q如果你惌动对齐,请将 set autoindent所在行前面的引号去掉,或者另外添加一行:set autoindent也可。这与配|颜色类伹{这L讄的结果是按回车后新行与上一行自动对齐?/p>

    5、设|tab的羃q量

    如果用python~写E序Q那么行~进量是一个极光要的概念Q同一个块的羃q量必须相同。你可能喜欢在行前加I格来表C羃q,但每ơ必L多次I格 键,如果你喜Ƣ用按TAB键来表示~进Q你可能觉得写的文本或程序不太好看,因ؓ默认情况下,VI和VIM的TAB~进量比较大Q至六Q七个字W)。设 |TAB键羃q量的方法:set shiftwidth=3 你也可以选一 个你自己喜欢的羃q量Q比?Q或4.

----------------------------------------------

首先从视觉方?

W一个要做的是羃q?/strong>,修改你的配置文g_vimrc,在最后加?span style="color: rgb(0, 128, 0);">set cindent,q样p|了c风格的羃q?在这里羃q的大小?span style="color: rgb(51, 153, 102);">shiftwidth的倹{?/p>

W二个要做的是语法高?/strong>Q这个是必须的,在中_vimrc加入syntax enable?/p>

W三个要做的字体的设|?/strong>Q设|一个舒服的字体可以让你~程的时候舒服好多,用editplus的时候我q的ConsolasQ在中我q是用的q种字体Q在_vimrc中加?/p>

if has(”gui_running”)
set guifont=Consolas:h9
endif

表示q行界面的时候就用这U字体?/p>

W四Q设|配色方?/strong>Q可以到点击q儿下蝲Q然后放?span style="color: rgb(0, 128, 0);">$"vimfiles"colorsq个目录下,然后在中加入如下配置

if has(”gui_running”)
set guifont=Consolas:h9
” set color schema
colorscheme
colorscheme_name
endif

colorscheme_nameZ需要设|的配色Ҏ的名U?/span>

接下来是在运行程序中用到的:

W一Q用ctag

中已l带了Ctagsq个E序。尽ctags也可以支持其它编辑器Q但是它正式支持的只?a class="st_tag internal_tag" rel="tag" title="Posts tagged with vim">VIM。Ctags可以帮助E序员很Ҏ地浏览源代码。用下面的命令可以在源代码的根目录下创徏“tags”文gQ?/p>

[/home/brimmer/src]$ ctags -R

“-R”表示递归创徏Q也包括源代码根目录下的所有子目录下的源程序?#8220;tags”文g中包括这些对象的列表Q?/p>

l        ?define定义的宏

l        枚D型变量的?/p>

l        函数的定义、原型和声明

l        名字I间QnamespaceQ?/p>

l        cd定义QtypedefsQ?/p>

l        变量Q包括定义和声明Q?/p>

l        c(classQ、结构(structQ、枚丄型(enumQ和联合QunionQ?/p>

l        cR结构和联合中成员变量或函数

用这?#8220;tags”文g来定位上面这些做了标记的对象Q下面介l一下定位这些对象的ҎQ?/p>

1)        用命令行。在q行的时候加?#8220;-t”参数Q例如:

[/home/brimmer/src]$   -t  foo_bar

q个命o打开定义“foo_bar”Q变量或函数或其它)的文Ӟq把光标定位到这一行?/p>

2)        ?a class="st_tag internal_tag" rel="tag" title="Posts tagged with vim">vim~辑器内?#8220;:ta”命oQ例如:

:ta foo_bar

3)        最方便的方法是把光标移到变量名或函数名上,然后按下“Ctrl-]”。用“Ctrl-o”退回原来的地方?/p>

注意Q运?a class="st_tag internal_tag" rel="tag" title="Posts tagged with vim">vim的时候,必须?#8220;tags”文g所在的目录下运行。否则,q行的时候还要用“:set tags=”命o讑֮“tags”文g的\径,q样才能扑ֈ“tags”文g?/p>

你还可以选择使用taglistq个插gQ这个插件可以在右侧昄函数Q变量等的列?/p>

W二Q改正程序中的错?/strong>

?a class="st_tag internal_tag" rel="tag" title="Posts tagged with vim">VIM~辑器的环境下用“:make”Qmake工具的用已l在我昨天的文章? 提到的云风的几篇文章中详l介l到Q就可以~译E序Q当然其前提是在当前目录下有Makefile文g。运行完“:make”之后Q如果程序中有错误,? 会显C出来。这时候,光标会自动指向第一个出现错误的地方Q而且你还可以看到错误的提C。然后,你就可以Ҏ错误Q而不用手工找到出错的那一行。记住下? 几个有用的命令:

l        “:cl”列出错误

l        “:cn”让光标指向下一个错?/p>

l        “:cp”让光标指向上一个错?/p>

l        “:cnew”从头开?/p>

你甚臛_以让识别出其它编译器而不是gcc的错误提C。这对一些开发嵌入式pȝ的程序员q很有用Q因Z们很可能用的不是gcc而是其它~译器。通过讄“errorformat”的|可以?a class="st_tag internal_tag" rel="tag" title="Posts tagged with vim">VIM识别出编译器的出错提C。因Z同的~译器的出错提示是不同的Q所以如果用的不是gccp重新讄?/p>

errorformat”的值是一个字W串Q它的格式和C语言的scanf的字W串格式相识?/p>

gcc?#8220;errorformat”的gؓQ?f:%l:"%m。其?#8220;%f”表示文g名,“%l”表示行号Q?#8220;%m”表示出错信息?/p>

?#8220;:h errorformat”查看详细的帮助信息?/p>

?span style="color: rgb(0, 128, 0);">“:h quickfix”?#8220;:h make”?#8220;:h makeprg”?#8220;:h errorfile”查看其它的信息?/p>

W三Q用快捷键

下面的这些快捷键对程序员很有帮助Q?br /> 在函CUd光标

[[  转到上一个位于第一列的“{”

]]  转到下一个位于第一列的“{”

{   转到上一个空?/p>

}   转到下一个空?/p>

gd  转到当前光标所指的局部变量的定义

*   转到当前光标所指的单词下一ơ出现的地方

#   转到当前光标所指的单词上一ơ出现的地方
括号的匹?/p>

%   用来q行括受中括号和大括号的匹配。这要看当前光标指向的是什么符号了?/p>

----------------------------------------------

一个ubuntu 下的vimrc配置例子:

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" 一般设?br /> """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" 讑֮默认解码
set fenc
=utf-8
set fencs
=utf-8,usc-bom,euc-jp,gb18030,gbk,gb2312,cp936

"讑֮搜烦是的高亮昄
set hlsearch

" 不要使用vi的键盘模式,而是vim自己?br /> set nocompatible

" history文g中需要记录的行数
set history=100

" 在处理未保存或只L件的时候,弹出认
set confirm

" 与windows׃n剪脓?br /> set clipboard+=unnamed

" 侦测文gcd
filetype on

" 载入文gcd插g
filetype plugin on

" 为特定文件类型蝲入相关羃q文?br /> filetype indent on

" 保存全局变量
set viminfo+=!

" 带有如下W号的单词不要被换行分割
set iskeyword
+=_,$,@,%,#,-

" 语法高亮
syntax on

" 高亮字符Q让其不?00列限?br /> :highlight OverLength ctermbg=red ctermfg=white guibg=red guifg=white
":match OverLength '"%101v.*'

" 状态行颜色
highlight StatusLine guifg=SlateBlue guibg=Yellow
highlight StatusLineNC guifg=Gray guibg=White

"高亮当前?br /> set cursorline

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" 文g讄
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" 不要备䆾文gQ根据自己需要取舍)
set nobackup

" 不要生成swap文gQ当buffer被丢弃的时候隐藏它
setlocal noswapfile
set bufhidden=hide

" 字符间插入的像素行数?br /> set linespace=0

" 增强模式中的命o行自动完成操?br /> set wildmenu

" 在状态行上显C光标所在位|的行号和列?br /> set ruler
set rulerformat
=%20(%2*%<%f%=" %m%r" %3l" %c" %p%%%)

" 命o行(在状态行下)的高度,默认?Q这里是2
set cmdheight=2

" 使回格键QbackspaceQ正常处理indent, eol, start{?br /> set backspace=2

" 允许backspace和光标键跨越行边?br /> set whichwrap+=<,>,h,l

" 可以在buffer的Q何地方用鼠标(cMoffice中在工作区双击鼠标定位)
set mouse
=a
set selection
=exclusive
set selectmode
=mouse,key

" 启动的时候不昄那个援助索马里儿童的提示
set shortmess=atI

" 通过使用: commands命oQ告诉我们文件的哪一行被改变q?br /> set report=0

" 不让vim发出讨厌的滴滴声
set noerrorbells

" 在被分割的窗口间昄I白Q便于阅?br /> set fillchars=vert:" ,stl:" ,stlnc:"

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" 搜烦和匹?br /> """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" 高亮昄匚w的括?br /> set showmatch

" 匚w括号高亮的时_单位是十分之一U)
set matchtime
=5

" 在搜索的时候不忽略大小?br /> set noignorecase

" 不要高亮被搜索的句子QphrasesQ?br /> "set nohlsearch

" 在搜索时Q输入的词句的逐字W高亮(cMfirefox的搜索)
set incsearch

" 输入:set list命o是应该显CZ啥?
set listchars=tab:
"|" ,trail:.,extends:>,precedes:<,eol:$

" 光标Ud到buffer的顶部和底部时保?行距?br /> set scrolloff=3

" 不要闪烁
set novisualbell

" 我的状态行昄的内容(包括文gcd和解码)
set statusline
=%F%m%r%h%w" [FORMAT=%{&ff}]" [TYPE=%Y]" [POS=%l,%v][%p%%]" %{strftime(""%d/%m/%y" -" %H:%M"")}

" L昄状态行
set laststatus=2

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" 文本格式和排?br /> """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" 自动格式?br /> set formatoptions=tcrqn

" l承前一行的~进方式Q特别适用于多行注?br /> set autoindent

" 为CE序提供自动~进
set smartindent

" 使用C样式的羃q?br /> set cindent

" 制表Wؓ4
set tabstop=4

" l一~进?
set softtabstop
=4
set shiftwidth
=4

" 不要用空g替制表符
set noexpandtab

" 不要换行
"set nowrap

"讄每行80个字W自动换?br /> set textwidth=80

" 在行和段开始处使用制表W?br /> set smarttab

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" CTags的设?br /> """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" 按照名称排序
let Tlist_Sort_Type = 
"name"

" 在右侧显C窗?br /> let Tlist_Use_Right_Window = 1

" 压羃方式
let Tlist_Compart_Format = 1

" 如果只有一个bufferQkillH口也kill掉buffer
let Tlist_Exist_OnlyWindow 
= 1

" 不要关闭其他文g的tags
let Tlist_File_Fold_Auto_Close = 0

" 不要昄折叠?br /> let Tlist_Enable_Fold_Column = 1

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Autocommands
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" 只在下列文gcd被侦到的时候显CP普通文本文件不昄

if has("autocmd")
   autocmd FileType xml,html,c,cs,java,perl,shell,bash,cpp,python,vim,php,ruby set number
   autocmd FileType xml,html vmap 
<C-o> <ESC>'<i<!--<ESC>o<ESC>'>o-->
   autocmd FileType java,c,cpp,cs vmap 
<C-o> <ESC>'<o/*<ESC>'>o*/
   autocmd FileType html,text,php,vim,c,java,xml,bash,shell,perl,python setlocal textwidth
=80
   autocmd Filetype html,xml,xsl source $VIMRUNTIME
/plugin/closetag.vim
   autocmd BufReadPost 
*
      
" if line("'""") > 0 && line("'""") <= line("$") |
      
"   exe "normal g`""" |
      
" endif
endif 
" has("autocmd")

" C/C++的编译和q行
map <F5> :call CompileRunGcc()<CR>
func! CompileRunGcc()
exec 
"w"
exec 
"!make"
exec 
"! ./%<"
endfunc

" shell scriptq行
map 
<F6> :call CompileRunSH()<CR>
func
! CompileRunSH()
exec 
"w"
exec 
"!chmod a+x %"
exec 
"!./%"
endfunc

" pythonq行
map <F7> :call CompileRunPyhton()<CR>
func! CompileRunPyhton()
exec 
"w"
exec 
"!chmod a+%"
exec 
"!./%"
endfunc

" 能够漂亮地显C?NFO文g
set encoding
=utf-8
function! SetFileEncodings(encodings)
    let b:myfileencodingsbak
=&fileencodings
    let 
&fileencodings=a:encodings
endfunction
function! RestoreFileEncodings()
    let 
&fileencodings=b:myfileencodingsbak
    unlet b:myfileencodingsbak
endfunction

au BufReadPre 
*.nfo call SetFileEncodings('cp437')|set ambiwidth=single
au BufReadPost 
*.nfo call RestoreFileEncodings()

" 高亮昄普通txt文gQ需要txt.vim脚本Q?br /> au BufRead,BufNewFile *  setfiletype txt

" 用空格键来开x?br /> set foldenable
set foldlevel
=0
set foldmethod
=indent
nnoremap 
<space> @=((foldclosed(line('.')) < 0? 'zc' : 'zo')<CR>

" minibufexpl插g的一般设|?br /> let g:miniBufExplMapWindowNavVim = 1
let g:miniBufExplMapWindowNavArrows = 1
let g:miniBufExplMapCTabSwitchBufs = 1
let g:miniBufExplModSelTarget = 1

----------------------
一个相兛_?br /> http://forum.ubuntu.org.cn/viewtopic.php?f=68&t=138212&st=0&sk=t&sd=a

 



LukeW 2008-11-03 13:02 发表评论
]]>
վ֩ģ壺 ˮ| | Ϫ| ¡Ң| | | | Ϫ| | ۶| | ߱| | | | | | Ԫı| μԴ| ʯ| ʼ| | | ʯ| | | ̩| ԭ| Ͻ| | ˮ| | | ̨| | ̨ʡ| | | ɽ| | |