Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: vim, custom commands

  1. #1
    Join Date
    Dec 2007
    Location
    Idaho
    Beans
    4,976
    Distro
    Ubuntu 20.04 Focal Fossa

    vim, custom commands

    So I am taking a programming with C course, because I don't want to use a full featured IDE which wants me to create a project and etc... I am using Vim to edit my source, currently when I want to compile I'll either send vim to the background with ctrl+z, then compile and test my program or I'll run it with :! gcc etc.... I understand vim allows you to write custom commands and I was hoping to make one to run my compile, so I could do something like :

    :compile

    and have it compile the file I am currently editing (everything will be single file programs)

    The command would be something like

    Code:
    gcc -std=c99 -Wall src/myfile.c -o bin/myfile.bin
    Now I get how to write that command except the output directory, I'm unsure how to specify the filename so that it's automatically the same file name but .bin instead of .c and in the bin subdirectory instead of in the src subdirectory.
    "You can't expect to hold supreme executive power just because some watery tart lobbed a sword at you"

    "Don't let your mind wander -- it's too little to be let out alone."

  2. #2
    Join Date
    Mar 2009
    Beans
    1,982

    Re: vim, custom commands

    It all depends on what you're trying to accomplish.

    Me, I'd use make instead of gcc directly. Your above command would then become something like:
    Code:
    :!make myfile.bin
    You can also compile a C app that does something you want, and run that from the command line.

    To execute an external shell command, you do this:
    <esc>:! <command>

    To read the results of that into the file, you do this:
    <esc>:r! <command>

    To make a macro of vim commands, you do this:
    <esc>qa<sequence of vim commands>q
    which would define macro 'a' with whatever sequence of commands you typed. To use the command you would type:
    @a

    There's a lot more to this, and while I've been using vim constantly for over 14 years I'm by no means an expert.

  3. #3
    Join Date
    Dec 2007
    Location
    Idaho
    Beans
    4,976
    Distro
    Ubuntu 20.04 Focal Fossa

    Re: vim, custom commands

    I basically don't want to have to rewrite my compile command when I switch computers etc..., the make file sounds promising, trying to understand a tutorial right now.
    "You can't expect to hold supreme executive power just because some watery tart lobbed a sword at you"

    "Don't let your mind wander -- it's too little to be let out alone."

  4. #4
    Join Date
    Mar 2009
    Beans
    1,982

    Re: vim, custom commands

    It's been years since I did any significant c programming, but I know that make has gone through some changes since then. It has a lot of standard functionality, for example if you type make something.o and there's a something.c in your current directory, it will know what to do without a makefile.

    For that matter the c programmers might be using some other build tool now. I'd look for some threads about c and see what they use. Or download some source for an active project and see what they use.

    Given what you just said, I would strongly recommend that you use a standard, currently used build tool, whatever that is. That ensures that current methodology is going to be applied to your project, and will also help you understand or participate in Open Source at some point. Or commercial software.

    If you start switching computers I would suggest using some sort of source code control like git.

  5. #5
    Join Date
    Dec 2007
    Location
    Idaho
    Beans
    4,976
    Distro
    Ubuntu 20.04 Focal Fossa

    Re: vim, custom commands

    makefiles were just the ticket!
    "You can't expect to hold supreme executive power just because some watery tart lobbed a sword at you"

    "Don't let your mind wander -- it's too little to be let out alone."

  6. #6
    Join Date
    Feb 2009
    Beans
    1,469

    Re: vim, custom commands

    N.B. Vim's :make command will run make for you, or you can change the makeprg option to change what it does. To be fair, it only saves one character over :!make

    I write mostly small programs and rarely use real makefiles; I just set the following environment variable in .zshrc so that I get all the warnings I like running make.

    Code:
    export CFLAGS="-std=c11 -Wall -Wextra -Wwrite-strings"
    You'd have to write an explicit makefile to tell it to add .bin to the filename, though -- that's not a typical thing to do.

  7. #7
    Join Date
    Mar 2009
    Beans
    1,982

    Re: vim, custom commands

    Last I checked, the final binary was just the name without extension. Not sure if you have a reason for adding the .bin, but if you want to it would work.

    So it would be something like this:
    hello.c -> hello.o -> hello

  8. #8
    Join Date
    Dec 2007
    Location
    Idaho
    Beans
    4,976
    Distro
    Ubuntu 20.04 Focal Fossa

    Re: vim, custom commands

    Well it's just a bunch of small programs, so I wrote one makefile with targets for each different assignment I do, so I can type :!make thisprog and it'll compile, I'm adding the .bin because I want to, I've seen binaries in linux with no extension, with .run, .bin and others, it doesn't really matter. I just like .bin so I can see from the name it's a binary.

    Ie when I start a new assignment I just add a new line for the new assignment.

    The makefile looks like this right now, I'm sure I'm not doing this the standard way, but it's working well for my intended use.
    Code:
    test: test.c
    	gcc -std=c99 -ggdb -Wall test.c -o bin/test.bin
    
    Ex5.16main: Ex5.16main.c Ex5.16function.c
    	gcc -std=c99 -ggdb -Wall Ex5.16main.c Ex5.16function.c -o bin/Ex5.16main.bin
    
    FirstCprogram: FirstCprogram.c
    	gcc -std=c99 -ggdb -Wall FirstCprogram.c -o bin/FirstCprogram.bin
    
    reverseDigits: reverseDigits.c
    	gcc -std=c99 -ggdb -Wall reverseDigits.c -o bin/reverseDigits.c
    
    dice: dice.c
    	gcc -std=c99 -ggdb -Wall dice.c -o bin/dice.c
    
    processImage: processImage.c
    	gcc -std=c99 -ggdb -Wall processImage.c -o bin/processImage.bin -L. -lbitmap
    
    clean:
    	rm bin/*
    "You can't expect to hold supreme executive power just because some watery tart lobbed a sword at you"

    "Don't let your mind wander -- it's too little to be let out alone."

  9. #9
    Join Date
    Mar 2009
    Beans
    1,982

    Re: vim, custom commands

    Do me a favor:

    Move your makefile to another directory, and then type:

    make test
    make FirstCprogram
    ...
    Also, you should be able to just make a rule with a wildcard filename, based on suffixes. Don't remember what the syntax is exactly but for example the Linux kernel makefile is only about suffixes, almost nothing is specific to a single file.

  10. #10
    Join Date
    Dec 2007
    Location
    Idaho
    Beans
    4,976
    Distro
    Ubuntu 20.04 Focal Fossa

    Re: vim, custom commands

    if I moved my make file and ran 'make test' inside the directory where test.c resides, It runs a generic 'cc test.c -o test' which doesn't compile of course because I have c99 specific code in there. (I guess c11 is the new standard, I'll start using that)

    If you are suggesting there's a more dynamic way to write the make file I'm all for it, I'm beginning to slap assignments (or projects I suppose) into their own sub directories as they are becoming multi-file assignments. I really only grasped enough of makefiles to figure out something that worked for me.
    "You can't expect to hold supreme executive power just because some watery tart lobbed a sword at you"

    "Don't let your mind wander -- it's too little to be let out alone."

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •